您的位置:首页 > 其它

pt工具:pt-table-checksum与pt-table-sync修复主从不一致

2016-07-05 15:43 549 查看
pt-table-checksum原理见下文 http://www.tuicool.com/articles/ZNn6JbV
默认端口,使用processlist方式做一致性检查

主上赋权,权限同步到slaves上,确保此用户可以在master/slave 上的权限
GRANT SELECT, INSERT, UPDATE, DELETE, PROCESS, SUPER, REPLICATION SLAVE ON *.* TO 'check'@'192.168.%' IDENTIFIED BY 'xxxxx';

主上执行:
pt-table-checksum --nocheck-replication-filters --no-check-binlog-format --replicate=rep_test.checksums --recursion-method=processlist --databases=rep_test --tables=test1 h=127.0.0.1,u=root,p=123456,P=3306
TS ERRORS DIFFS ROWS CHUNKS SKIPPED TIME TABLE
05-08T16:21:06 0 1 4 1 0 0.012 rep_test.test1
TS :完成检查的时间。
ERRORS :检查时候发生错误和警告的数量。
DIFFS :0表示一致,1表示不一致。当指定--no-replicate-check时,会一直为0,当指定--replicate-check-only会显示不同的信息。
ROWS :表的行数。
CHUNKS :被划分到表中的块的数目。
SKIPPED :由于错误或警告或过大,则跳过块的数目。
TIME :执行的时间。
TABLE :被检查的表名。
参数的意义:
--nocheck-replication-filters :不检查复制过滤器,建议启用。后面可以用--databases来指定需要检查的数据库。
--no-check-binlog-format : 不检查复制的binlog模式,要是binlog模式是ROW,则会报错。
--replicate-check-only :只显示不同步的信息。
--replicate= :把checksum的信息写入到指定表中,建议直接写到被检查的数据库当中。
--databases= :指定需要被检查的数据库,多个则用逗号隔开。
--tables= :指定需要被检查的表,多个用逗号隔开

从库上可以执行
select * from checksums where master_cnt <> this_cnt OR master_crc <> this_crc OR ISNULL(master_crc) <> ISNULL(this_crc) \G
确认那些表不一致
pt-table-sync [OPTIONS] DSN [DSN]
pt-table-sync: 高效的同步MySQL表之间的数据,他可以做单向和双向同步的表数据。他可以同步单个表,也可以同步整个库。它不同步表结构、索引、或任何其他模式对象。所以在修复一致性之前需要保证他们表存在。
接着上面的复制情况,主和从的test1数据不一致,需要修复,
从库上执行:
pt-table-sync --sync-to-master --replicate=rep_test.checksums h=127.0.0.1,u=root,p=123456,P=3307 --databases=rep_test --tables=test1 --print
REPLACE INTO `rep_test`.`test1`(`id`, `name`) VALUES ('3', 'c')
#用一个IP(SLAVE)就可以了。
/*percona-toolkit src_db:rep_test src_tbl:test1 src_dsn:P=3306,h=192.168.200.25,p=...,u=root dst_db:rep_test dst_tbl:test1 dst_dsn:h=192.168.200.201,p=...,u=root lock:1 transaction:1 changing_src:1 replicate:0 bidirectional:0 pid:20122 user:zhoujy host:zhoujy*/;
参数的意义:
--replicate= :指定通过pt-table-checksum得到的表,这2个工具差不多都会一直用。
--databases= : 指定执行同步的数据库,多个用逗号隔开。
--tables= :指定执行同步的表,多个用逗号隔开。
--sync-to-master :指定一个DSN,即从的IP,他会通过show processlist或show slave status 去自动的找主。
--print :打印,但不执行命令。
--execute :执行命令。
更多的参数请见官网,上面指出来的是常用的,对该场景够用的参数。
上面的效果和这个一样:
pt-table-sync --replicate=rep_test.checksums h=127.0.0.1,u=root,p=123456 h=192.168.200.201,u=root,p=123456 --print
REPLACE INTO `rep_test`.`test1`(`id`, `name`) VALUES ('3', 'c')
#先M的IP,再S的IP
/*percona-toolkit src_db:rep_test src_tbl:test1 src_dsn:h=127.0.0.1,p=...,u=root dst_db:rep_test dst_tbl:test1 dst_dsn:h=192.168.200.201,p=...,u=root lock:1 transaction:0 changing_src:rep_test.checksums replicate:rep_test.checksums bidirectional:0 pid:12285 user:root host:zhoujy*/;

不是默认端口,使用dsn方式做一致性检查:
主库上创建保存从库dsn信息的数据表
(root:CS:3307) [shop_zp]> create database pt;
Query OK, 1 row affected (0.01 sec)
(root:CS:3307) [shop_zp]> use pt
Database changed
(root:CS:3307) [pt]> CREATE TABLE `dsns` (
-> `id` int(11) NOT NULL AUTO_INCREMENT,
-> `parent_id` int(11) DEFAULT NULL,
-> `dsn` varchar(255) NOT NULL,
-> PRIMARY KEY (`id`)
-> );
Query OK, 0 rows affected (0.01 sec)
插入从库的dsn信息:从库dsn格式为: ‘h=replica_host,u=repl_user,p=repl_pass’
(root:CS:3307) [pt]> insert into dsns (parent_id,dsn) values(1, 'h=192.168.1.200,u=check,p=check,P=3308');
pt-table-checksums使用dsn的方式为:
--recursion-method dsn=h=host,D=percona,t=dsns
主上执行命令:
pt-table-checksum h=192.168.1.200,u=check,p=check,P=3307 -d shop_zp -t gt_product_spec --nocheck-replication-filters --replicate=pt.checksums --no-check-binlog-format --recursion-method dsn=h=192.168.1.200,D=pt,t=dsns --chunk-time=2
TS ERRORS DIFFS ROWS CHUNKS SKIPPED TIME TABLE
07-05T10:20:40 0 1 182524 7 0 2.407 shop_zp.gt_product_spec
从上检查:
(root:CS:3308) [pt]> select * from checksums where master_cnt <> this_cnt OR master_crc <> this_crc OR ISNULL(master_crc) <> ISNULL(this_crc) ;
+---------+-----------------+-------+------------+-------------+----------------+----------------+----------+----------+------------+------------+---------------------+
| db | tbl | chunk | chunk_time | chunk_index | lower_boundary | upper_boundary | this_crc | this_cnt | master_crc | master_cnt | ts |
+---------+-----------------+-------+------------+-------------+----------------+----------------+----------+----------+------------+------------+---------------------+
| shop_zp | gt_product_spec | 3 | 0.277126 | PRIMARY | 82905 | 123203 | c32a7771 | 40000 | 4e3dbe23 | 40000 | 2016-07-05 10:20:38 |
+---------+-----------------+-------+------------+-------------+----------------+----------------+----------+----------+------------+------------+---------------------+
1 row in set (0.01 sec)
从上执行命令,同步:
pt-table-sync --print --sync-to-master h='192.168.1.200',u='check',p='check',P=3308 -d shop_zp -t gt_product_spec

pt-table-sync的语法逻辑:
if DSN has a t part, sync only that table:
if 1 DSN:
if --sync-to-master:
The DSN is a slave. Connect to its master and sync.
if more than 1 DSN:
The first DSN is the source. Sync each DSN in turn.
else if --replicate:
if --sync-to-master:
The DSN is a slave. Connect to its master, find records of differences, and fix.
else:
The DSN is the master. Find slaves and connect to each,find records of differences, and fix.
else:
if only 1 DSN and --sync-to-master:
The DSN is a slave. Connect to its master, find tables and filter with --databases etc, and sync each table to the master.
else:
find tables, filtering with --databases etc, and sync each DSN to the first.

详解:
Sync db.tbl on host1 to host2:
pt-table-sync --execute h=host1,D=db,t=tbl h=host2
Sync all tables on host1 to host2 and host3:
pt-table-sync --execute host1 host2 host3
Make slave1 have the same data as its replication master:
pt-table-sync --execute --sync-to-master slave1
Resolve differences that pt-table-checksum found on all slaves of master1:
pt-table-sync --execute --replicate test.checksum master1
Same as above but only resolve differences on slave1:
pt-table-sync --execute --replicate test.checksum --sync-to-master slave1
Sync master2 in a master-master replication configuration, where master2’s copy of db.tbl is known or suspected to be
incorrect:
pt-table-sync --execute --sync-to-master h=master2,D=db,t=tbl
Note that in the master-master configuration, the following will NOT do what you want, because it will make changes
directly on master2, which will then flow through replication and change master1’s data:
# Don't do this in a master-master setup!
pt-table-sync --execute h=master1,D=db,t=tbl master2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: