IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    MySQL5.6配置同步复制的新方法以及常见问题的解决方法

    李鹏发表于 2016-01-21 07:21:12
    love 0

    MySQL5.6新增一种不用设置log_bin文件和log位置的方法,之前我们都需要查看master机器信息(show master status),然后在slave设置

    master_host=’172.31.20.175′,master_user=’sync’,master_password=’lp123456′,master_log_file=’mysql-bin.000005′,master_log_pos=120;

    使用mysql5.6后可以不用这种方法配置了,只要设置my.cnf文件

    log-bin=mysql-bin

    binlog_format=row

    log_slave_updates

    gtid-mode=ON

    enforce-gtid-consistency=ON

    然后在slave中设置:

    change master to

    master_host=’masterip’,master_user=’replicationuser’,master_password=’password’,master_AUTO_POSITION=1;

    其中masterip、replicationuser和password分别对应你master机的信息

    常见问题:

    1、show slave status中Slave_IO_State: Waiting to reconnect after a failed registration on master

    解决方法:

    在master上执行

    grant replication slave on *.* to ‘repl’@’%’ identified by ‘password';

    FLUSH PRIVILEGES;

    然后重新stop slave 和start slave就可以

    2、show slave status中Slave_IO_Running: No

    Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.

    解决办法:每个库的uuid应该是不一样的,修改auto.cnf文件(在mysql 的data目录下)的uuid:

    [auto]

    server-uuid=6dcee5be-8cdb-11e2-9408-90e2ba2e2ea6

    按照这个16进制格式,随便改下,重启mysql即可。

    3、从库中slave_sql_running为NO

    一般在 Last_SQL_Error:项中可以看到报错的语句

    Slave_IO_Running:连接到主库,并读取主库的日志到本地,生成本地日志文件

    Slave_SQL_Running:读取本地日志文件,并执行日志里的SQL命令。

    这时,网上很多会教使用SET global sql_slave_skip_counter=n; 设置,但其实执行这个语句都会报错,因为启动了gtid-mode=ON

    解决方法:跳过执行报错的语句

    查看show slave status的最后两行:

    Retrieved_Gtid_Set表示已经从master中拉取过来的事务;

    Executed_Gtid_Set表示已经执行的事务

    找到Executed_Gtid_Set中与Retrieved_Gtid_Set ID一致的记录,如上图ca83d308-2ea5-11e4-b85f-00163e042f50:1-5,表示只执行到第五个事务,就是这个事务报错了,因此跳过这个事务即可。依次执行以下语句:

    stop slave;

    set gtid_next=’ca83d308-2ea5-11e4-b85f-00163e042f50:6′;

    begin;commit;

    set gtid_next=”AUTOMATIC”;

    start slave;

    然后再查看show slave status\G

    如果看到以下状态即可以了

    PS:启动主从备份时,一定要确保主从的数据一致,因为从库遇到任何报错,即使执行update语句时,主库中的一条记录在从库中找不到,都会报导致问题2的出现,从而停止执行slave更新



沪ICP备19023445号-2号
友情链接