mysql5.6 GTID 同步模式
环境说明:centos7,mysql5.6,GTID同步模式
起因
磁盘阵列存储异常,导致虚拟机脱盘,分区挂载只读,主机修复重启后,从库无法同步主库,提示relay log读取到错误。
show slave status\G
Last_Errno: 1594
Last_Error: Relay log read failure: Could not parse relay log event entry. The possible reasons are: the master's binary log is corrupted (you can check this by running 'mysqlbinlog' on the binary log), the slave's relay log is corrupted (you can check this by running 'mysqlbinlog' on the relay log), a network problem, or a bug in the master's or slave's MySQL code. If you want to check the master's binary log or slave's relay log, you will be able to know their names by issuing 'SHOW SLAVE STATUS' on this slave.
解决思路:重置同步信息,重新读取主库binlog,重新写入从库。
记录从库最后同步信息
1、show slave status\G
Relay_Master_Log_File: mysql-binlog.000249
Exec_Master_Log_Pos: 92240744
Executed_Gtid_Set: 03b8b27c-f9b8-11e8-b9e5-000c29cc1e94:396662759-397477758
2、提取信息
主库UUID:03b8b27c-f9b8-11e8-b9e5-000c29cc1e94
主库binlog位置: File =》mysql-binlog.000249 Pos=》 92240744
主库可以验证对应文件位置的binlog信息(可忽略,主要验证主库binlog信息是否匹配上面提取的数据)
mysql -uroot -p -e 'show binlog events in "mysql-binlog.000254"' >> 254.log
开始恢复
1、从库执行,停止同步
stop slave;
2、关闭自动匹配GTID
change master to master_auto_position=0;
3、重置同步信息
reset slave;
reset master;
change master to master_host='master_ip', master_user='repl', master_password='rep_123456', master_port=3306, master_log_file='mysql-binlog.000249', master_log_pos=92240744;
** 文件位置跟之前记录的要保持一致。
4、启动同步线程
start slave;
5、查看状态:
show slave status\G
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
出现双YES,就看到希望了!
补充:
如果从库日志刷盘机制为非双1 配置,可能会出现主键冲突情况,如果数据不是特别重要可以使用跳过GTID的方式修复
方法一:设置空事务跳过错误
1、show slave status \G
Executed_Gtid_Set: 03b8b27c-f9b8-11e8-b9e5-000c29cc1e94:396662759-397477758
确定异常GTID事务号:03b8b27c-f9b8-11e8-b9e5-000c29cc1e94:397477759
2、跳过GTID
stop slave;
set gtid_next='03b8b27c-f9b8-11e8-b9e5-000c29cc1e94:397477759'; #(Executed_Gtid_Set + 1)
begin;
commit;
set gtid_next='AUTOMATIC';
start slave;
方法二:重置master方法跳过错误
mysql> STOP SLAVE;
mysql> RESET MASTER;
mysql> SET @@GLOBAL.GTID_PURGED =’03b8b27c-f9b8-11e8-b9e5-000c29cc1e94:396662759-397477759'
mysql> START SLAVE;
3、检验状态
show slave status\G
可重复上面跳过步骤。
如果数据比较重要,建议重做从库。