1. 程式人生 > >Mysql-GTID複製跳過錯誤的方法

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;