Mysql主从同步,1032错误的快速解决方案

632 阅读2分钟

Mysql主从同步,1032错误是会经常碰到的 Could not execute Update_rows event on table fn.s_order; Can't find record in 's_order', Error_code: 1032; handler error HA_ERR_KEY_NOT_FOUND; the event's master log mysql-bin.000106, end_log_pos 31641325

1、通过end_log_pos到logbin中查询log

到主库中执行 mysqlbinlog --no-defaults -v -v --base64-output=decode-rows /var/lib/mysql/mysql-bin.000106 |grep -A '10' 31641325

#200730 15:15:03 server id 1  end_log_pos 31641325 CRC32 0x53437719     Update_rows: table id 1103 flags: STMT_END_F
### UPDATE `fn`.`s_order`
### WHERE
###   @1=1596079195076660554 /* LONGINT meta=0 nullable=0 is_null=0 */
###   @2='1596079195076660554' /* VARSTRING(765) meta=765 nullable=1 is_null=0 */
###   @3=NULL /* VARSTRING(300) meta=300 nullable=1 is_null=1 */
###   @4='2020-07-30 11:19:55' /* DATETIME(0) meta=0 nullable=1 is_null=0 */
###   @5=50.00 /* DECIMAL(10,2) meta=2562 nullable=1 is_null=0 */
###   @6=50.00 /* DECIMAL(10,2) meta=2562 nullable=1 is_null=0 */
###   @7=1 /* INT meta=0 nullable=1 is_null=0 */
###   @8='2' /* VARSTRING(765) meta=765 nullable=1 is_null=0 */
--
# at 31641325
#200730 15:15:03 server id 1  end_log_pos 31641403 CRC32 0xc409d226     Query   thread_id=1023  exec_time=0   error_code=0
SET TIMESTAMP=1596093303/*!*/;
COMMIT
/*!*/;
# at 31641403
#200730 15:15:03 server id 1  end_log_pos 31641468 CRC32 0xbd82529a     Anonymous_GTID  last_committed=2180   sequence_number=2181     rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 31641468
#200730 15:15:03 server id 1  end_log_pos 31641553 CRC32 0x57be09fd     Query   thread_id=1021  exec_time=0   error_code=0

2、根据查询到的log,到主库中查询对应的记录

根据上面的log,获得对应的表fn.s_order记录的id为1596079195076660554, 到主库查询到这记录 select * from fn.s_order where id=1596079195076660554;

3、根据获得记录写出对应的insert语句,有一个快速的获得insert语句的办法,通过navicat工具,右键点击查询到这条记录,选择'复制为=>insert语句' ,既可获得这条语句对应的insert语句。

4、到从库执行这条语句

5、再stop slave; start slave;

6、搞定。