DBAtlas · 数据库运维图谱

修复出错的副本:1062 / 1032 行冲突

MySQL复制

命令

-- 1) Failing worker, error and GTID
SELECT WORKER_ID, LAST_ERROR_NUMBER, LEFT(LAST_ERROR_MESSAGE, 250) AS last_error, APPLYING_TRANSACTION
FROM performance_schema.replication_applier_status_by_worker
WHERE LAST_ERROR_NUMBER <> 0;

-- 2) Shell: decode the row images of that transaction from the relay log
mysqlbinlog --base64-output=DECODE-ROWS -vv --include-gtids='af6bc361-c353-11f1-8f12-f69e63ad6c0e:11' /var/lib/mysql/db2-relay-bin.0* | grep '^###'

-- 3a) 1062 Duplicate entry: delete the extra row on the replica without binlogging
SET SESSION sql_log_bin = 0;
DELETE FROM shop.orders WHERE id = 20001;
-- 3b) 1032 Can't find record: insert the missing row from the WHERE (before image) values
INSERT INTO shop.orders (id, cust, amount, note, created) VALUES (6, 6, 9.00, 'n6', '2026-10-08 20:09:44');
SET SESSION sql_log_bin = 1;

-- 4) Start the applier and verify
START REPLICA SQL_THREAD;
SELECT SERVICE_STATE, LAST_ERROR_NUMBER FROM performance_schema.replication_applier_status_by_coordinator;

作用

不跳过事务,而是将副本上的行恢复到源所期望的状态,以修复两种最常见的基于行的复制中断(1062 duplicate key、1032 row not found);数据一致性得以保持。

适用场景

当 Replica_SQL_Running = No 且错误为 1062 / 1032 时;作为正确的修复方式,代替用空事务跳过 GTID。

注意

在副本上的修复必须在 sql_log_bin=0 下运行;否则会产生 errant GTID,在故障转移时造成问题。如果 super_read_only=ON,请临时禁用并在之后重新启用。before-image 的值假定 binlog_row_image=FULL;使用 MINIMAL 时只记录 PK,行的其余部分必须从源读取。一张表中出现一次冲突通常意味着还有更多;修复后请用 pt-table-checksum 将该表与源进行比对。relay log 路径:SELECT @@relay_log_basename;

在 DBAtlas 中打开 →

相关内容