安装pt工具

yum -y install percona-toolkit


配置主从复制

主库:192.168.0.24

从库:192.168.0.25

创建账号

在主库上创建账号并授权,该账号需要主库和从库都能使用

create user 'test_check'@'%' identified by 'W123b456%';
grant SELECT,PROCESS, SUPER, REPLICATION SLAVE,Replication client ON *.* TO 'test_check'@'%';
grant all on test.* to 'test_check'@'%';


数据校验

使用pt-table-checksum工具对主从数据进行校验

pt-table-checksum --nocheck-binlog-format --nocheck-replication-filters  --replicate=test.checksums --databases=test --host=192.168.0.24 --port=3306 --user=test_check --password=W123b456%

TS ERRORS DIFFS ROWS DIFF_ROWS CHUNKS SKIPPED TIME TABLE
09-01T10:03:24 0 1 251 0 1 0 0.030 test.a
09-01T10:03:24 0 0 1053 0 1 0 0.032 test.b
09-01T10:03:24 0 1 262 6 1 0 0.030 test.c


加上选项--replicate-check-only只查看不一致的

pt-table-checksum --replicate-check-only --nocheck-binlog-format --nocheck-replication-filters  --replicate=test.checksums --databases=test --host=192.168.0.24 --port=3306 --user=test_check --password=W123b456%

Checking if all tables can be checksummed ...
Starting checksum ...
Differences on test-db-0-24
TABLE CHUNK CNT_DIFF CRC_DIFF CHUNK_INDEX LOWER_BOUNDARY UPPER_BOUNDARY
test.a 1 0 1
test.c 1 6 1


修复数据

利用pt-table-sync修复主从不一致,先利用--print查看修复生成的SQL语句。

pt-table-sync --replicate=test.checksums h=192.168.0.24,u=test_check,p=W123b456% h=192.168.0.25,u=test_check,p=W123b456% --print


确认无误后,加上--execute修复主从同步

修复方向master向从库同步,由于是一主一从所以这个方向没问题,以master的数据为准:

pt-table-sync --replicate=supply_chain_fengtan.checksums h=192.168.0.24,u=test_check,p=W123b456% h=192.168.0.25,u=test_check,p=W123b456% --print --execute --charset=utf8mb4


主向从同步需要–no-check-slave这个选项,否则只能从向主同步,因为如果有多个从库可能会导致新的不一致

pt-table-sync --print --execute h=master,D=database_name,u=root,p=password h=web3 --no-check-slave --lock=1 --database=database_name --charset=utf8mb4


以上操作均需要在主库上操作