主从复制环境中,可能有种种原因导致主、从库数据不一致的情况,主从一致性也一直是DBA需要关注的问题,校验MySQL的主从一致性一般有多种工具,诸如MySQL自带的checksum、mysqldiff、pt-table-checksum等,每种工具各有优缺,本文就pt-table-checksum工具进行介绍和记录实验环节。
下载二进制版本的percona-toolkit工具箱
https://www.percona.com/downloads/percona-toolkit
本文用的是2.2.18版本
[[email protected]
~]# tar xvf percona-toolkit-2.2.18.tar.gz
[[email protected]
~]# cd percona-toolkit-2.2.18
[[email protected]
percona-toolkit-2.2.18]# perl Makefile.PL
[[email protected] percona-toolkit-2.2.18]# make && make install
主库创建checksums用户用于校验主从是否一致
mysql>GRANT SELECT, PROCESS, SUPER, REPLICATION SLAVE ON *.* TO ‘checksums‘@‘%‘ IDENTIFIED BY ‘MANAGER‘;
开始执行校验,在校验结束后,会在test库下创建表名为checksums的表用于存储主从一致性信息
[[email protected] ~]#/usr/local/bin/pt-table-checksum h=‘192.168.1.250‘,u=‘checksums‘,p=‘MANAGER‘,P=3306 -d www --nocheck-replication-filters --replicate=test.checksums --no-check-binlog-format
从库可以执行本条SQL来查看哪些表有主从不一致的情况,此时主从是一致的所以没有结果
mysql> SELECT db,tbl, SUM(this_cnt) AS total_rows, COUNT(*) AS chunks FROM test.checksums WHERE ( master_cnt <> this_cnt OR master_crc <> this_crc OR ISNULL(master_crc) <> ISNULL(this_crc)) GROUP BY db, tbl; Empty set (0.03 sec)
我们先删除www库下的某张表的某一行
[SQL]delete from decorate_order where id=10; 受影响的行: 1 时间: 0.003s
再次运行同步校验
[[email protected] ~]#/usr/local/bin/pt-table-checksum h=‘192.168.1.250‘,u=‘checksums‘,p=‘MANAGER‘,P=3306 -d www --nocheck-replication-filters --replicate=test.checksums --no-check-binlog-format
可以发现在从库上执行本条SQL可以看到主从不一致的表名
mysql> SELECT db,tbl, SUM(this_cnt) AS total_rows, COUNT(*) AS chunks FROM test.checksums WHERE( master_cnt <> this_cnt OR master_crc <> this_crc OR ISNULL(master_crc) <> ISNULL(this_crc)) GROUP BY db, tbl; +-----+----------------+------------+--------+ | db | tbl | total_rows | chunks | +-----+----------------+------------+--------+ | www | decorate_order | 25356 | 1 | +-----+----------------+------------+--------+ 1 row in set (0.00sec)
同理我们删掉erp库下的某张表的某一行
[SQL]delete from erp_mard id=104; 受影响的行: 1 时间: 0.002s
再次运行同步校验
[[email protected] ~]#/usr/local/bin/pt-table-checksum h=‘192.168.1.250‘,u=‘checksums‘,p=‘MANAGER‘,P=3306 -d www --nocheck-replication-filters --replicate=test.checksums --no-check-binlog-format
可以发现刚刚删除的erp_mard表已经进入到了我们的主从不一致记录表里
mysql> SELECT db,tbl, SUM(this_cnt) AS total_rows, COUNT(*) AS chunks FROM test.checksums WHERE ( master_cnt <> this_cnt OR master_crc <> this_crc OR ISNULL(master_crc) <> ISNULL(this_crc)) GROUP BY db, tbl; +-----+----------------+------------+--------+ | db | tbl | total_rows | chunks | +-----+----------------+------------+--------+ | erp | erp_mard | 11183 | 1 | | www | decorate_order | 25356 | 1 | +-----+----------------+------------+--------+ 2 rows in set (0.02sec)
我们在主库执行pt-table-sync命令来进行重新同步
[[email protected] ~]#/usr/local/bin/pt-table-sync --execute --replicate test.checksums h=‘192.168.1.250‘,u=‘sys_admin‘,p=‘MANAGER‘
同步完后重新验证
[[email protected] ~]#/usr/local/bin/pt-table-checksum h=‘192.168.1.250‘,u=‘checksums‘,p=‘MANAGER‘,P=3306 -d www --nocheck-replication-filters --replicate=test.checksums --no-check-binlog-format [[email protected] ~]# /usr/local/bin/pt-table-checksum h=‘192.168.1.250‘,u=‘checksums‘,p=‘MANAGER‘,P=3306 -d erp --nocheck-replication-filters --replicate=test.checksums --no-check-binlog-format
可以发现checksums表已经没有不同步的表了
mysql> SELECT db,tbl, SUM(this_cnt) AS total_rows, COUNT(*) AS chunks FROM test.checksums WHERE ( master_cnt <> this_cnt OR master_crc <> this_crc OR ISNULL(master_crc) <> ISNULL(this_crc)) GROUP BY db, tbl; Empty set (0.03 sec)
至此主从校验、同步完成