Mysql 主从复制失败,主键冲突解决

By | 2025-12-18
ShellScript
mysql> SHOW REPLICA STATUS\G

...
Last_SQL_Errno: 1062
Last_SQL_Error: Coordinator stopped because there were error(s) in the worker(s). The most recent failure being: Worker 1 failed executing transaction 'ANONYMOUS' at source log binlog.000721, end_log_pos 581125367. See error log and/or performance_schema.replication_applier_status_by_worker table for more details about this failure or others, if any.
...

ShellScript
mysql> SELECT
  WORKER_ID,
  LAST_ERROR_NUMBER,
  LAST_ERROR_MESSAGE,
  LAST_ERROR_TIMESTAMP,
  LAST_APPLIED_TRANSACTION,
  LAST_APPLIED_TRANSACTION_ORIGINAL_COMMIT_TIMESTAMP
FROM performance_schema.replication_applier_status_by_worker
WHERE LAST_ERROR_NUMBER <> 0\G

*************************** 1. row ***************************
                                         WORKER_ID: 1
                                 LAST_ERROR_NUMBER: 1062
                                LAST_ERROR_MESSAGE: Worker 1 failed executing transaction 'ANONYMOUS' at source log binlog.000721, end_log_pos 581125367; Could not execute Write_rows event on table cloak.clicks_cache; Duplicate entry '27372618' for key 'clicks_cache.PRIMARY', Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event's source log binlog.000721, end_log_pos 581125367
                              LAST_ERROR_TIMESTAMP: 2025-12-18 10:51:53.823222
                          LAST_APPLIED_TRANSACTION: 
LAST_APPLIED_TRANSACTION_ORIGINAL_COMMIT_TIMESTAMP: 0000-00-00 00:00:00.000000
1 row in set (0.01 sec)

注意上面的 on table cloak.clicks_cache; Duplicate entry ‘27372618‘ for key ‘clicks_cache.PRIMARY’,

ShellScript
mysql> STOP REPLICA SQL_THREAD;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> DELETE FROM cloak.clicks_cache WHERE ccid >= 27372618;
Query OK, 1 row affected (0.01 sec)

mysql> START REPLICA SQL_THREAD;
Query OK, 0 rows affected (0.01 sec)

mysql> SHOW REPLICA STATUS\G
...
...

如果还有错误,重复上面步骤

Leave a Reply

Your email address will not be published. Required fields are marked *