pt-table-checksum和pt-table-sync修复主从不一致的数据

环境:
主:192.168.1.134 端口:13306
从:192.168.1.135 端口:13306

从2:192.168.1.136 端口:13306

 

说明:
工具字典库:db_percona
数据业务库:db_hxl

 

 

创建数据库,该数据库用来保存percona工具使用中用到的表
create database db_percona;

 

事先创建好账号(该账号需要在主从上都存在,如主从都是正常的话,主库创建了,从库会自动同步的)

grant select, process, super, replication slave on *.* to admin@'%' identified by 'mysql';
grant all on db_percona.* to admin@'%';

同时需要将业务库的dml权限授予该账号(用于修复不一致数据)

 grant insert,update,delete,select ON `db_hxl`.* TO 'admin'@'%';


1.检查表是否有差异tb_test
主从在不同机器同一端口下,在主库上执行(系统会自动创建表db_hxl.checksums)
/opt/percona-toolkit-3.1.0/bin/pt-table-checksum --recursion-method="processlist" --nocheck-binlog-format --nocheck-replication-filters --replicate=db_percona.checksums --databases=db_hxl --tables=tb_test h=192.168.1.134,u=admin,p=mysql,P=13306

 

Checking if all tables can be checksummed ...
Starting checksum ...
TS ERRORS DIFFS ROWS DIFF_ROWS CHUNKS SKIPPED TIME TABLE
12-31T17:26:24 0 1 8 1 1 0 0.823 db_hxl.tb_test

这种方法好像不能识别是那个从库出现了数据差异

 

注意这里前提条件是所有的从库的io_thread和sql_thread必须是正常运行的.

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

 

否则会一直在等待

Replica localhost.localdomain is stopped. Waiting.
Replica localhost.localdomain is stopped. Waiting.
Replica localhost.localdomain is stopped. Waiting.
Replica localhost.localdomain is stopped. Waiting.

 

 

 

2.判断是那个从库上有差异

在每个从库下执行(主库上也可以执行),有差异的会返回记录

 

 

select * 
from db_percona.checksums 
where master_cnt <> this_cnt 
OR master_crc <> this_crc 
OR ISNULL(master_crc) <> ISNULL(this_crc)

 

 

 

 


3.修复差异的数据(从库上执行)
h=192.168.1.135和h=192.168.1.136 是指从库的地址
打印出来
/opt/percona-toolkit-3.1.0/bin/pt-table-sync --print --sync-to-master h=192.168.1.135,u=admin,p=mysql,P=13306,D=db_hxl,t=tb_test --charset=utf8

/opt/percona-toolkit-3.1.0/bin/pt-table-sync --print --sync-to-master h=192.168.1.136,u=admin,p=mysql,P=13306,D=db_hxl,t=tb_test --charset=utf8

 

 

真正执行修复不同的数据(在从库192.168.1.135上执行)
/opt/percona-toolkit-3.1.0/bin/pt-table-sync --execute --sync-to-master h=192.168.1.135,u=admin,p=mysql,P=13306,D=db_hxl,t=tb_test --charset=utf8

 

 

说明:

 

--------------------使用dns-------------------------------------------

##创建表
create table db_percona.dsns (
id int(11) not null auto_increment,
parent_id int(11) default null,
dsn varchar(255) not null,
primary key (id)
);


##把所有的从库写入到配置表
insert into db_percona.dsns(dsn) values('h=192.168.1.135,u=admin,p=mysql,P=13306');
insert into db_percona.dsns(dsn) values('h=192.168.1.136,u=admin,p=mysql,P=13306');

 

主库上检查
[root@localhost tmp]# /opt/percona-toolkit-3.1.0/bin/pt-table-checksum --socket=/opt/mysql5727/mysql.sock --no-check-binlog-format --nocheck-replication-filters --replicate=db_percona.checksums --tables=db_hxl.tb_test h=192.168.1.134,u=admin,p=mysql,P=13306 --recursion-method=dsn=h=192.168.1.134,D=db_hxl,t=dsns
Checking if all tables can be checksummed ...
Starting checksum ...
TS ERRORS DIFFS ROWS DIFF_ROWS CHUNKS SKIPPED TIME TABLE
12-31T17:31:01 0 1 8 1 1 0 5.801 db_hxl.tb_test

 

好像这种方式也识别不出来那个从库出现的差异

 

posted @ 2021-12-31 16:35  slnngk  阅读(182)  评论(0编辑  收藏  举报