主从数据校验
维度:
1个月对现网数据做一次数据校验
如果发生主从切换,当天要做一次数据校验
校验工具:
pt-table-checksum
修复工具:
pt-table-sync
pt-table-checksum
原理
在数据库创建表: checksums
每次取count(*) where id > 0 and id <= 1000 数据(this_cnt),做hash_code运算(this_crc)
主库:select count(*) from tb where id > 0 and id <= 1000;
主从分别执行函数,生成各自的cnt,crc的值
set binlog_format = 'statement'
replace into percona.checksum( db, tb1, chunk, chunk_index, lower_boundary, upper_boundary, this_cnt, this_crc) select xxxxxx
将主库的this_cnt, this_crc值更新到 master_cnt, master_crc 里。
select this_cnt, this_crc from checksum where db = 'xx' and tb = 'xx' and chunk = xx;
update checksum set master_cnt = this_cnt.value, master_crc = this_crc.value where db = 'xx' and tb = 'xx' and chunk = xx;
对比从库的this_cnt, this_crc值 和 master_cnt, master_crc
select db,tb1,sum(this_cnt) as total_rows, count(*) as chunks from checksums where (master_cnt <>this_cnt or master_crc <> this_crc or isnull(master_crc) <> isnull(this_crc)) group by db,tb1;
使用
pt-table-checksum --host=172.18.10.197 --port=3306 --databases=xxxx -uxxxx -p --nocheck-replication-filters --replicate=percona.checksums --nocheck-binlog-format --nocheck-plan --recursion-method=hosts
pt-table-sync
原理
通过pt-table_checksum查到出错的chunk,进行折半查找,找到出错记录。
主库设置
binlog_format='statement'
主库存在,从库不存在。主库执行delete from tb where id = xxx;
主库存在,从库存在,主库replace into ,将记录重新插入一次。从库会将正确数据插入进去。
使用