Mysql-GTID复制跳过错误的方法

gtid复制出现错误是个比较麻烦的问题,需要人工处理,如果直接跳过可能导致数据丢失或者数据不一致的情况,所以建议人工处理。下面我来介绍Mysql gtid复制出现错误的情况,按照下来方式处理前必需找出错误的sql和错误的数据,处理完成前将数据修改成为一致数据。

1、数据库版本

mysql >select version()
+-------------------------------------------+
| version()                                 |
+-------------------------------------------+
| 5.7.17                                  |
+-------------------------------------------+
1 row in set (0.00 sec)

2、产生的错误

‘Duplicate key name ‘i_index” on query. Default database: ‘test’. Query: ‘create unique index i_index on t(id)’

3、查找错误sql的gtid事务id

mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.10.6.87
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000028
          Read_Master_Log_Pos: 1113
               Relay_Log_File: mysql-bin.000007
                Relay_Log_Pos: 1151
        Relay_Master_Log_File: mysql-bin.000028
             Slave_IO_Running: Yes
            Slave_SQL_Running: No
              Replicate_Do_DB: 
          Replicate_Ignore_DB: mysql,information_schema
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 1061
                   Last_Error: Error 'Duplicate key name 'i_index'' on query. Default database: 'test'. Query: 'create unique index i_index on t(id)'
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 938
              Relay_Log_Space: 2301
              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: 1061
               Last_SQL_Error: Error 'Duplicate key name 'i_index'' on query. Default database: 'test'. Query: 'create unique index i_index on t(id)'
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 2
                  Master_UUID: 8f9e146f-0a18-11e7-810a-0050568833c8
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0                                            #SQL延迟同步
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: 
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 170421 15:44:05
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 8f9e146f-0a18-11e7-810a-0050568833c8:1-4      # 重试出现错误的事务
            Executed_Gtid_Set: 8f9e146f-0a18-11e7-810a-0050568833c8:1-3,     # 当前执行的事务
f7c86e19-24fe-11e7-a66c-005056884f03:1-9
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

4、找出错误的事务Id

4.1 解决方法一、根据第三步找出

Retrieved_Gtid_Set: 8f9e146f-0a18-11e7-810a-0050568833c8:1-4 # 重试出现错误的事务
Executed_Gtid_Set: 8f9e146f-0a18-11e7-810a-0050568833c8:1-3, # 当前执行的事务
可以发现当前执行 8f9e146f-0a18-11e7-810a-0050568833c8:4 sql时候发生了主键重复,导致sql无法继续执行
现在需要跳过此事务,继续往后执行,跳过方法如下:

stop slave ; #首先停止gtid复制
SET @@SESSION.GTID_NEXT= '8f9e146f-0a18-11e7-810a-0050568833c8:4' ; 设置当前下一个执行的事务Id
BEGIN; COMMIT; # 设置空事务,直接提交
SET SESSION GTID_NEXT = AUTOMATIC; #恢复下一个事务号 
 START SLAVE; # 继续开启事务

4.2 解决方法二、重置master方法跳过错误

mysql>STOP SLAVE;
mysql> RESET MASTER;
mysql>SET @@GLOBAL.GTID_PURGED ='8f9e146f-0a18-11e7-810a-0050568833c8:1-4'
mysql>START SLAVE;

你可能感兴趣的:(mysql)