MySQL 主从复制 ERROR-1062

21:27:  [ty]> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.14.165
                  Master_User: bak
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-binlog.000004
          Read_Master_Log_Pos: 2306
               Relay_Log_File: ty14166-relay-bin.000011
                Relay_Log_Pos: 1947
        Relay_Master_Log_File: mysql-binlog.000004
             Slave_IO_Running: Yes
            Slave_SQL_Running: No
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 1062
                   Last_Error: Could not execute Write_rows event on table ty.pritab; Duplicate entry ‘5‘ for key ‘PRIMARY‘, Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event‘s master log mysql-binlog.000004, end_log_pos 2275
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 2042
              Relay_Log_Space: 2893
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 1062
               Last_SQL_Error: Could not execute Write_rows event on table ty.pritab; Duplicate entry ‘5‘ for key ‘PRIMARY‘, Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event‘s master log mysql-binlog.000004, end_log_pos 2275
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 14165
                  Master_UUID: ce856f95-5aa9-11ea-87f4-0050569348f5
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: 
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 200304 21:27:44
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: ce856f95-5aa9-11ea-87f4-0050569348f5:31-39
            Executed_Gtid_Set: 829493f0-4ef5-11ea-9b90-0050569348f5:1,
ce856f95-5aa9-11ea-87f4-0050569348f5:1-17:27-38,
f1f07d96-5af0-11ea-9ae7-005056933d92:1-9
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

数据库主从处于GTID-MODE=ON状态,通过show slave status\G看到同步异常1062报错

解决方法一:

mysql> stop slave;
mysql> set gtid_next=‘ce856f95-5aa9-11ea-87f4-0050569348f5:39‘;
mysql> begin;
mysql> commit;
mysql> set GTID_NEXT=‘AUTOMATIC‘;
mysql> start slave;
 
再查案Slave_IO_Running和Slave_SQL_Running都是Yes状态,恢复正常(此方法要注意确认GTID,避免跳过需要的事务,具体方法在ERROR-1032文中说明)
解决方法二:
利用percona-toolkit工具集中的pt-slave-restart命令(跳过错误)
[ ~]# pt-slave-restart -uroot -p******
2020-03-04T21:16:42 p=...,u=root ty14166-relay-bin.000011         891 1062 
后台日志:
[Note] Slave SQL thread for channel ‘‘ initialized, starting replication in log ‘mysql-binlog.000004‘ at position 986, relay log ‘./ty14166-relay-bin.000011‘ position: 891

再查案Slave_IO_Running和Slave_SQL_Running都是Yes状态,恢复正常
 
GTID-MODE=OFF状态的处理方法:
mysql> stop slave;
mysql> SET GLOBAL SQL_SLAVE_SKIP_COUNTER = n;  
mysql> start slave;