1. 程式人生 > 資料庫 >mysql 主從複製如何跳過報錯

mysql 主從複製如何跳過報錯

一、傳統binlog主從複製,跳過報錯方法

mysql> stop slave;
mysql> set global sql_slave_skip_counter = 1;
mysql> start slave;
mysql> show slave status \G

二、GTID主從複製,跳過報錯方法

mysql> stop slave; #先關閉slave複製;
mysql> change master to ...省略... #配置主從複製;
mysql> show slave status\G #檢視主從狀態;

發現報錯:

mysql> show slave status\G
*************************** 1. row ***************************
        Slave_IO_State: Waiting for master to send event
         Master_Host: 172.19.195.212
         Master_User: master-slave
         Master_Port: 3306
        Connect_Retry: 60
       Master_Log_File: mysql-bin.000021
     Read_Master_Log_Pos: 194
        Relay_Log_File: nginx-003-relay-bin.000048
        Relay_Log_Pos: 454
    Relay_Master_Log_File: mysql-bin.000016
       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: 1007
          Last_Error: Error 'Can't create database 'code'; database exists' on query. Default database: 'code'. Query: 'create database code'
         Skip_Counter: 0
     Exec_Master_Log_Pos: 8769118
       Relay_Log_Space: 3500
       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: 1007
        Last_SQL_Error: Error 'Can't create database 'code'; database exists' on query. Default database: 'code'. Query: 'create database code'
 Replicate_Ignore_Server_Ids: 
       Master_Server_Id: 100
         Master_UUID: fea89052-11ef-11eb-b241-00163e00a190
       Master_Info_File: /usr/local/mysql/data/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: 201022 09:31:29
        Master_SSL_Crl: 
      Master_SSL_Crlpath: 
      Retrieved_Gtid_Set: fea89052-11ef-11eb-b241-00163e00a190:8-5617
      Executed_Gtid_Set: a56c9b04-11f1-11eb-a855-00163e128853:1-11224,fea89052-11ef-11eb-b241-00163e00a190:1-5614
        Auto_Position: 1
     Replicate_Rewrite_DB: 
         Channel_Name: 
      Master_TLS_Version: 
1 row in set (0.01 sec)

可以看到 Slave_SQL_Running 為 NO,表示執行取回的二進位制日誌出了問題;
在 Last_Error 中也可以看到大概的報錯;(因為我之前的操作,大概可以判斷出 是因為主庫的二進位制日誌中有建立code庫的sql,而從庫上我已經建立了這個庫,應該是產生了衝突;)

解決方法:

1、如果清楚自己之前的操作,可以將從庫中產生衝突的庫刪除;
2、或者通過跳過GTID報錯的事務的方法

--- 通過 Last_SQL_Errno 報錯編號查詢具體的報錯事務
mysql> select * from performance_schema.replication_applier_status_by_worker where LAST_ERROR_NUMBER=1007\G
*************************** 1. row ***************************
     CHANNEL_NAME: 
      WORKER_ID: 0
      THREAD_ID: NULL
    SERVICE_STATE: OFF
LAST_SEEN_TRANSACTION: fea89052-11ef-11eb-b241-00163e00a190:5615
  LAST_ERROR_NUMBER: 1007
  LAST_ERROR_MESSAGE: Error 'Can't create database 'code'; database exists' on query. Default database: 'code'. Query: 'create database code'
 LAST_ERROR_TIMESTAMP: 2020-10-22 09:31:29
1 row in set (0.00 sec)

mysql> stop slave;
Query OK,0 rows affected (0.00 sec)

--- 跳過查詢到報錯的事務(LAST_SEEN_TRANSACTION 的值)
mysql> set @@session.gtid_next='fea89052-11ef-11eb-b241-00163e00a190:5615';
Query OK,0 rows affected (0.00 sec)

mysql> begin;
Query OK,0 rows affected (0.00 sec)

--- 提交一個空的事務,因為設定gtid_next後,gtid的生命週期開始了,必須通過顯性的提交一個事務來結束;
mysql> commit;
Query OK,0 rows affected (0.00 sec)

--- 設定回自動模式;
mysql> set @@session.gtid_next=automatic;
Query OK,0 rows affected (0.00 sec)

mysql> start slave;
Query OK,0 rows affected (0.00 sec)

通過以上步驟,就跳過了這次的GTID報錯的事務,如果 start slave 之後還是有報錯,那麼就按照此步驟繼續跳過;

經驗豐富的話,基本不用查詢事務,通過 Executed_Gtid_Set 就可以判斷出報錯的事務是 fea89052-11ef-11eb-b241-00163e00a190:5615 了;因為執行事務,到 fea89052-11ef-11eb-b241-00163e00a190:1-5614 的時候報錯了,應該可以判斷是 5615事務出現的錯誤;

以上就是mysql 主從複製如何跳過報錯的詳細內容,更多關於MySQL 跳過報錯的資料請關注我們其它相關文章!