(14)Linux_Mysql主从配置

1. 开启mysql bin-log 主从配置:vim /etc/my.cnf 中添加bin-log文件:log-bin=mysql-bin

     my.cnf 增加内容:主

                                log-bin=mysql-bin

                                server-id = 1

     my.cnf 增加内容:从

                                server-id = 2

    主数据库执行:grant replication slave on *.* to 'tongbu'@'%' identified by '123456';

    查看主库的标记位:show master status

    从数据库执行:(1) change master to master_host='主库IP',master_user='同步的账号',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=325;

                              (2) 标记slave:slave start;

                              (3) 查看slave是否同步成功:show slave status\G

2. MySQL数据库还原:cd /var/lib/mysql/

    (1) cat 先查看bin-log 文件:mysqlbinlog mysql-bin.000001,找到要还原的 开始点 到 结束点

    (2) 还原:mysqlbinlog --start-position-106 --stop-position=433 mysql-bin.000001 > xxxx.sql

3. 查看服务器表bin log文件名:show master status;

4. 主从恢复:

                    方案一:会丢数据

                    Master 执行:flush tables with read lock;

                    Slave上执行:stop slave;

                                           set global sql_slave_skip_counter = 1;   // 表示跳过一步错误,后面的数字可变

                                           start slave;

                     方案二:企业常用

                                  Master 执行:flush tables with read lock;

                                   把主库备份,还原到从库上去,从库再开启start slave 同步,然后再解除 主库的锁定

你可能感兴趣的:((14)Linux_Mysql主从配置)