您的位置:首页 > 其它

percona-toolkit 之 【pt-table-checksum】、【pt-table-sync】说明

2013-05-09 11:58 363 查看
摘要:

工作上需要把一个从库提升为主库,但对从库和主库的数据一致性不能保证一样,所以就利用 pt-table-checksum 工作来检查主从的一致性(之前写过用1.0.1的版本可以进行操作的文章,但是在新版本操作就不行了,只能重新来过)以及通过 pt-table-sync 如何修复这些不一致的数据。

前提:

下载地址:wget www.percona.com/downloads/percona-toolkit/2.2.2/percona-toolkit-2.2.2.tar.gz

安装方法:perl Makefile.PL;make;make install

使用方法:

pt-table-checksum [OPTIONS] [DSN]


pt-table-checksum在主<M>上通过执行校验的查询对复制的一致性进行检查,对比主从的校验值,从而产生结果。DSN指向的是主的地址,该工具的退出状态不为零,如果发现有任何差别,或者如果出现任何警告或错误,更多信息请见官网。
不制定任何参数,会直接对本地的所有数据库的表进行检查。

pt-table-checksum -S /var/run/mysqld/mysqld.sock


现在开始使用它,检查主从状态:
表信息:

View Code

主:
root@localhost : rep_test 08:58:41>select * from test1;
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  2 | b    |
|  3 | c    |
|  4 | d    |
+----+------+
4 rows in set (0.00 sec)

从:
root@192.168.200.201 : rep_test 08:58:56>select * from test1;
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  2 | b    |
|  3 | c    |
|  4 | d    |
+----+------+
4 rows in set (0.00 sec)


OK,数据已经保持一致了。不过建议还是用--print 打印出来的好,这样就可以知道那些数据有问题,可以人为的干预下。不然直接执行了,出现问题之后更不好处理。总之还是在处理之前做好数据的备份工作。

注意:要是表中没有唯一索引或则主键则会报错:

Can't make changes on the master because no unique index exists at /usr/local/bin/pt-table-sync line 10591.


另外补充:
要是从库有的数据,而主库没有,那这个数据怎么处理?在原先的基础上,在从上添加一条5记录。

root@zhoujy:~# pt-table-sync --replicate=rep_test.checksums --databases=rep_test,test --tables=test2,test1 h=127.0.0.1,u=root,p=123456 h=192.168.200.201,u=root,p=123456 --print
DELETE FROM `rep_test`.`test1` WHERE `id`='5' LIMIT 1

/*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:12343 user:root host:zhoujy*/;

REPLACE INTO `rep_test`.`test1`(`id`, `name`) VALUES ('3', 'c')

/*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:12343 user:root host:zhoujy*/;


从上面可以看到,直接把从库的有的数据给删除了,没有的数据进行插入。这样2边又保持了 一致性。

要是在shell窗口不想显示的输入密码则可以添加:--ask-pass 参数,如:

zhoujy@zhoujy:~$ pt-table-checksum  --nocheck-replication-filters --no-check-binlog-format  --replicate=rep_test.checksums --ask-pass --databases=rep_test --tables=test1,test2  h=127.0.0.1,u=root,P=3306
Enter MySQL password:
TS ERRORS  DIFFS     ROWS  CHUNKS SKIPPED    TIME TABLE
05-09T13:57:04      0      1        4       1       0   0.015 rep_test.test1
05-09T13:57:04      0      1        1       1       0   0.010 rep_test.test2

zhoujy@zhoujy:~$ pt-table-sync --replicate=rep_test.checksums  --ask-pass h=127.0.0.1,u=root h=192.168.200.201,u=root --print
Enter password for 127.0.0.1:
Enter password for 192.168.200.201:
DELETE FROM `rep_test`.`test1` WHERE `id`='5' LIMIT 1;
REPLACE INTO `rep_test`.`test1`(`id`, `name`) VALUES ('4', 'd') ;


总结:
该工具检查的表,需要检查连接的帐号需要有很高的权限,在一般权限行需要加SELECT, PROCESS, SUPER, REPLICATION SLAVE等权限,测试方便我直接给了ALL的权限,pt-table-checksum 和 pt-table-sync 一起互补使用,检查一定是在同步操作之前,更多的信息请见 这里 这里这里
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: