記一次生產環境主從庫數據不同步問題的處理

一. 進入mysql 從庫所在機器,通過mysql客戶端進入mysql命令提示符狀態下:

mysql -u root -p
密碼
輸入命令:show slave status\G;
發現如下錯誤:

Last_SQL_Errno: 1062
               Last_SQL_Error: Error 'Duplicate entry 'aac886b000d34149b86e5eac76fb2f6a' for key 'PRIMARY'' on query. Default database: 'xxxwork'. Query: 'insert into process_log (GY_ID, BROWSER, BUSINESS_ID, CONTENT, COST_TIME, CREATE_BY, CREATE_NAME, CREATE_TIME, INFO_GL, OPER_IP, OPER_TYPE, TO_USER, TO_USER_ID, TYAJBS, ID) values (null, 'rv:11.0', '88512ffd8cd48dd948df4e81b26ce068', '(2019), 0, '0e6916a96e749ec7016e74b67b145cef', '?..?., '2019-12-30 18:39:26.497', 'InfoEntity', '...126', 0, null, null, null, 'aac886b000d34149b86e5eac76fb2f6a')'

錯誤原因分析:Duplicate entry 重複條目 主鍵衝突

處理方式:

停掉主庫,或避免主庫寫入新數據

執行如下指令:

stop slave; 
set global sql_slave_skip_counter=1;  #跳過該錯誤的指令的執行
start slave; 
然後執行指令:show slave status\G ;
發現已經ok了

mysql> show slave status\G 
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: xxxxxxxxxxx
                  Master_User: slave1
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000216
          Read_Master_Log_Pos: 333121554
               Relay_Log_File: dzsd-database-server-relay-bin.000573
                Relay_Log_Pos: 6669552
        Relay_Master_Log_File: mysql-bin.000215
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 190352148
              Relay_Log_Space: 1223182362
              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: 9519
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: 86918329-d52d-11e9-bcc3-fa163eb434eb
             Master_Info_File: /usr/local/mysql/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Reading event from the relay log
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

二、同步過程中又發現如下錯誤

Replicate_Wild_Ignore_Table: 
                   Last_Errno: 1060
                   Last_Error: Error 'Duplicate column name 'xxx_code'' on query. Default database: 'xxxwork'. Query: 'ALTER TABLE `process_log` ADD COLUMN `xxx_code`  varchar(50) NULL COMMENT '娉..浠g.' AFTER `gy_id`'
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 191853675
              Relay_Log_Space: 1275845919
              Until_Condition: None

處理方式同上;

三、此時查看主從庫狀態

主庫Position和從庫Read_Master_Log_Pos值不一致 ;

分析後發現是因爲上面的錯誤導致大量同步日誌未被執行引起的,
從庫同步機制需要一定時間趕上主庫記錄。

經過一個晚上的同步,第二天發現主從庫pos完全一致了。


四、錯誤原因分析

這個問題應該是項目沒有停,往這個表裏加字段,同時又有數據往這個表裏插入,導致的

下次更新過程應該是,1.停止服務,2.修改數據庫,3.更新代碼,4.啓動服務

五、總結

Mysql 主從同步機制比較“脆弱”,實際應用過程中一定要注意避免對讀庫的操作,
一旦出現數據衝突等問題,都會影響主從同步機制。建議對讀庫設置不同的密碼,以防止
人爲的造成同步異常。

另外,需要建立應對異常處理的機制,一旦主從同步出現異常,可以把應用程序的連接迅速切換至
主庫。因此,從庫僅僅起到讀取數據的作用,避免任何相關的外部依賴,例如觸發器等。


 

發佈了6 篇原創文章 · 獲贊 39 · 訪問量 72萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章