Mysql主从同步失败排查思路及解决办法

1、查看同步信息

        登录进从数据库后查询同步状态

show slave status \G

2、查看同步失败出现的日志

Coordinator stopped because there were error(s) in the worker(s).  The most r
ecent failure being: Worker 1 failed executing transaction '55b49392-fdcd-11ec-83b2-fa163ed1ceff:38158079'
at master log mysql-bin. 000114, end_log_pos 27826272.  See error log and/or performance_schema.replication_a
pplier_status_by_worker table for more details about this failure or others, if any.

3、查看同步失败详情

select * from performance_schema.replication_applier_status_by_worker\G
Worker 1 failed executing transaction '55b49392-fd
cd-11ec-83b2-fa163ed1ceff:38158079' at master log mysql-bin. 000114, end_log_pos 27826272;  Could not execute
Update_rows event on table gscloud.babapdataset;  Can't find record in 'babapdataset', Error_code: 1032;  ha
ndler error HA_ERR_KEY_NOT_FOUND;  the event's master log mysql-bin. 000114, end_log_pos 27826272

        通过日志可以看到 '55b49392-fdcd-11ec-83b2-fa163ed1ceff:38158079' 事务阻塞了同步,继续往下看,该事务中涉及到了表 'gscloud.babapdataset' 的Update操作。

        该日志还提供了主数据库binlog日志的名字和事务报错的位置:master log mysql-bin. 000114, end_log_pos 27826272

4、切换到主数据库,查看binlog日志

        日志一般存在/mysql/binlogs下

mysqlbinlog --no-defaults -v -v --base64-output=DECODE-ROWS mysql-bin.000114 | grep -A '10' 27826272 // 查看binlog日志

        填入自己的log日志名称和 end_log_pos,为了方便查看将输出转存储,并通过传输工具下载到本地。

mysqlbinlog --no-defaults -v -v --base64-output=DECODE-ROWS mysql-bin.000114 | grep -A '10' 27826272 > 20230630.log

        可以看到发生错误的数据id为:d82c9522-a1e9-bc9f-e1cc-4e2c0dcde425

Mysql主从同步失败排查思路及解决办法_第1张图片

         这样就找到了缺失的数据,因为从数据库中没有此数据,所以update时发生了错误,需要现将这条数据插入到从数据库中。

5、查询数据,插入到从数据库中

        我通过 navicat 来获取数据,来获取插入的SQL语句

Mysql主从同步失败排查思路及解决办法_第2张图片

         将获取到的SQL插入到从数据库中。

6、跳过失败的事务继续运行

stop slave; //停止同步线程
Set @@SESSION.GTID_NEXT='55b49392-fdcd-11ec-83b2-fa163ed1ceff:38158079'; //跳过事务
Begin;
Commit;
Set @@SESSION.GTID_NEXT = AUTOMATIC; //设置事务继续向下一个运行
start slave;

最后查看下运行状态:

Mysql主从同步失败排查思路及解决办法_第3张图片

OK 下班

你可能感兴趣的:(mysql,数据库)