查看从库状态时发现Last_SQL_Errno: 1062(主键值重复,入库失败)

一、 问题描述:
查看从库状态时发现Last_SQL_Errno: 1062(主键值重复,入库失败)

从库报错如下:

复制代码
mysql> show slave status\G
Connection id:    23172
Current database: mysql

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.18.10.121
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000031
          Read_Master_Log_Pos: 958135718
               Relay_Log_File: mysql-relay-bin.000072
                Relay_Log_Pos: 41698212
        Relay_Master_Log_File: mysql-bin.000031
             Slave_IO_Running: Yes
            Slave_SQL_Running: No
              Replicate_Do_DB: 
          Replicate_Ignore_DB: information_schema,performance_schema,undolog,for_nagios,undolog,information_schema,performance_schema,undolog,for_nagios,undolog
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: information_schema.%,performance_schema.%,undolog.%,for_nagios.%,undolog.%,information_schema.%,performance_schema.%,undolog.%,for_nagios.%,undolog.%
                   Last_Errno: 1062
                   Last_Error: Error 'Duplicate entry '569' for key 'PRIMARY'' on query. Default database: 'info'. Query: 'INSERT INTO `user` (`user_id`, `exp`) VALUES ('21817642', '2021-02-15 11:29:08')'
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 957119483
              Relay_Log_Space: 42714820
              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: Error 'Duplicate entry '569' for key 'PRIMARY'' on query. Default database: 'info'. Query: 'INSERT INTO `user` (`user_id`, `exp`) VALUES ('21817642', '2021-02-15 11:29:08')'
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: 03941c59-423a-11e9-ba0e-00163e0ad22c
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 957119483
              Relay_Log_Space: 42714820
              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: Error 'Duplicate entry '569' for key 'PRIMARY'' on query. Default database: 'info'. Query: 'INSERT INTO `user` (`user_id`, `exp`) VALUES ('21817642', '2021-02-15 11:29:08')'
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
                  Master_UUID: 03941c59-423a-11e9-ba0e-00163e0ad22c
             Master_Info_File: mysql.slave_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: 210115 11:29:08
               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)
复制代码

主库查询:

mysql> select * from info.user where id = 569;
+-----+----------+---------------------+
| id  | user_id  | exp                 |
+-----+----------+---------------------+
| 569 | 21817642 | 2021-02-15 11:29:08 |
+-----+----------+---------------------+

# 从库查询

复制代码
mysql> select * from info.user where id = 569;
Connection id:    23181
Current database: mysql

+-----+---------+---------------------+
| id  | user_id | exp                 |
+-----+---------+---------------------+
| 569 |  515165 | 2025-12-30 00:00:00 |
+-----+---------+---------------------+
复制代码

二、从库与主库中的数据不一致,手工将从库数据更新

复制代码
mysql> desc info.user;
Connection id:    23183
Current database: mysql
+---------+----------+------+-----+-------------------+----------------+
| Field   | Type     | Null | Key | Default           | Extra          |
+---------+----------+------+-----+-------------------+----------------+
| id      | int(10)  | NO   | PRI | NULL              | auto_increment |
| user_id | int(10)  | NO   | UNI | NULL              |                |
| exp     | datetime | NO   |     | CURRENT_TIMESTAMP |                |
+---------+----------+------+-----+-------------------+----------------+

# 将从库数据替换为和主库保持一致
mysql> replace into info.user(id,user_id,exp) values(569,21817642,'2021-02-15 11:29:08');
Query OK, 2 rows affected (0.00 sec)

mysql> select * from info.user where id = 569;
+-----+----------+---------------------+
| id  | user_id  | exp                 |
+-----+----------+---------------------+
| 569 | 21817642 | 2021-02-15 11:29:08 |
+-----+----------+---------------------+
1 row in set (0.00 sec)


三、跳过本次sql同步,再次启动主从
stop slave;
set global sql_slave_skip_counter=1;
start slave;
show slave status\G
复制代码

 

主库查询:
mysql> select * from info.user where id = 569;
+-----+----------+---------------------+
| id  | user_id  | exp                 |
+-----+----------+---------------------+
| 569 | 21817642 | 2021-02-15 11:29:08 |
+-----+----------+---------------------+


# 从库查询
mysql> select * from info.user where id = 569;
Connection id:    23181
Current database: mysql

+-----+---------+---------------------+
| id  | user_id | exp                 |
+-----+---------+---------------------+
| 569 |  515165 | 2025-12-30 00:00:00 |
+-----+---------+---------------------+

posted @   reblue520  阅读(366)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2018-01-15 kafka manager安装配置和使用
2018-01-15 nginx中Geoip_module模块的使用
点击右上角即可分享
微信分享提示