一、 问题描述:
查看从库状态时发现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 |
+-----+---------+---------------------+