MySQL Last_SQL_Errno: 1062----經典錯誤,主鍵衝突

来源:https://www.cnblogs.com/small-wei/archive/2019/12/05/11990402.html
-Advertisement-
Play Games

一、基礎信息 1. Centos7.4 2.MySQL 5.7.21 3.基於gtid的複製 二、異常描述 誤把從節點當成主節點插入一條數據,同一條數據在主、從節點插入都進行了一次插入操作,導致主鍵衝突,slave下的SQL線程異常。 建議設置從庫為只讀: set global read_only= ...


一、基礎信息

1. Centos7.4

2.MySQL 5.7.21

3.基於gtid的複製

二、異常描述

誤把從節點當成主節點插入一條數據,同一條數據在主、從節點插入都進行了一次插入操作,導致主鍵衝突,slave下的SQL線程異常。

建議設置從庫為只讀:

set global read_only=1;--針對普通MySQL資料庫用戶設置為只讀
set global super_read_only=1;--針對super類MySQL資料庫用戶設置為只讀,比如root用戶

 

my.cnf 文件設置永久只讀

read_only=1
super_read_only=1;

 

三、處理過程  

1.查看報錯信息-- 報錯大致如下

mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.1
                  Master_User: repuser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_binlog.000023
          Read_Master_Log_Pos: 2266930
               Relay_Log_File: devmidsrv01kf-relay-bin.000003
                Relay_Log_Pos: 423
        Relay_Master_Log_File: mysql_binlog.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: 1062
                   Last_Error: Could not execute Write_rows event on table testdb1.students; Duplicate entry '8' for key 'PRIMARY', Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event's master log mysql_binlog.000016, end_log_pos 340380520
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 340380190
              Relay_Log_Space: 6541566399
              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 testdb1.students; Duplicate entry '8' for key 'PRIMARY', Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event's master log mysql_binlog.000016, end_log_pos 340380520
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 757
                  Master_UUID: 7bd36140-a92d-11e9-ba1f-005056a4099b
             Master_Info_File: /data/mysql/3306/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: 191205 16:19:02
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 7bd36140-a92d-11e9-ba1f-005056a4099b:835084-2324874
            Executed_Gtid_Set: 7bd36140-a92d-11e9-ba1f-005056a4099b:1-835083,
bcad977d-a934-11e9-a589-005056a483f4:1-29
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

 

2.確認需要跳過的gtid是關鍵

找到這個已經執行的gtid,加1

7bd36140-a92d-11e9-ba1f-005056a4099b:1-835083

 

3.跳過gtid

stop slave;

SET @@SESSION.GTID_NEXT= '7bd36140-a92d-11e9-ba1f-005056a4099b:1-835084';

BEGIN; COMMIT;

SET SESSION GTID_NEXT = AUTOMATIC;

START SLAVE;

 

4、檢查

mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.1
                  Master_User: repuser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_binlog.000023
          Read_Master_Log_Pos: 2266930
               Relay_Log_File: devmidsrv01kf-relay-bin.000003
                Relay_Log_Pos: 423
        Relay_Master_Log_File: mysql_binlog.000016
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

 

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
一周排行
    -Advertisement-
    Play Games
  • 1. 說明 /* Performs operations on System.String instances that contain file or directory path information. These operations are performed in a cross-pla ...
  • 視頻地址:【WebApi+Vue3從0到1搭建《許可權管理系統》系列視頻:搭建JWT系統鑒權-嗶哩嗶哩】 https://b23.tv/R6cOcDO qq群:801913255 一、在appsettings.json中設置鑒權屬性 /*jwt鑒權*/ "JwtSetting": { "Issuer" ...
  • 引言 集成測試可在包含應用支持基礎結構(如資料庫、文件系統和網路)的級別上確保應用組件功能正常。 ASP.NET Core 通過將單元測試框架與測試 Web 主機和記憶體中測試伺服器結合使用來支持集成測試。 簡介 集成測試與單元測試相比,能夠在更廣泛的級別上評估應用的組件,確認多個組件一起工作以生成預 ...
  • 在.NET Emit編程中,我們探討了運算操作指令的重要性和應用。這些指令包括各種數學運算、位操作和比較操作,能夠在動態生成的代碼中實現對數據的處理和操作。通過這些指令,開發人員可以靈活地進行算術運算、邏輯運算和比較操作,從而實現各種複雜的演算法和邏輯......本篇之後,將進入第七部分:實戰項目 ...
  • 前言 多表頭表格是一個常見的業務需求,然而WPF中卻沒有預設實現這個功能,得益於WPF強大的控制項模板設計,我們可以通過修改控制項模板的方式自己實現它。 一、需求分析 下圖為一個典型的統計表格,統計1-12月的數據。 此時我們有一個需求,需要將月份按季度劃分,以便能夠直觀地看到季度統計數據,以下為該需求 ...
  • 如何將 ASP.NET Core MVC 項目的視圖分離到另一個項目 在當下這個年代 SPA 已是主流,人們早已忘記了 MVC 以及 Razor 的故事。但是在某些場景下 SSR 還是有意想不到效果。比如某些靜態頁面,比如追求首屏載入速度的時候。最近在項目中回歸傳統效果還是不錯。 有的時候我們希望將 ...
  • System.AggregateException: 發生一個或多個錯誤。 > Microsoft.WebTools.Shared.Exceptions.WebToolsException: 生成失敗。檢查輸出視窗瞭解更多詳細信息。 內部異常堆棧跟蹤的結尾 > (內部異常 #0) Microsoft ...
  • 引言 在上一章節我們實戰了在Asp.Net Core中的項目實戰,這一章節講解一下如何測試Asp.Net Core的中間件。 TestServer 還記得我們在集成測試中提供的TestServer嗎? TestServer 是由 Microsoft.AspNetCore.TestHost 包提供的。 ...
  • 在發現結果為真的WHEN子句時,CASE表達式的真假值判斷會終止,剩餘的WHEN子句會被忽略: CASE WHEN col_1 IN ('a', 'b') THEN '第一' WHEN col_1 IN ('a') THEN '第二' ELSE '其他' END 註意: 統一各分支返回的數據類型. ...
  • 在C#編程世界中,語法的精妙之處往往體現在那些看似微小卻極具影響力的符號與結構之中。其中,“_ =” 這一組合突然出現還真不知道什麼意思。本文將深入剖析“_ =” 的含義、工作原理及其在實際編程中的廣泛應用,揭示其作為C#語法奇兵的重要角色。 一、下劃線 _:神秘的棄元符號 下劃線 _ 在C#中並非 ...