码迷,mamicode.com
首页 > 其他好文 > 详细

pt-table-sync修复主从库不一致数据

时间:2019-06-16 09:29:56      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:second   row   提示   column   权限   category   输出   delete   最简   

一、实例演示1

模拟主从库数据不一致环境:

192.168.0.39 master库:
mysql> select * from test01.frame01;;
+----+-----------+------------------------------------------+
| id | parent_id | dsn                                      |
+----+-----------+------------------------------------------+
|  1 |         1 | 192.168.0.22,u=pt22,p=ptchecksums,P=3307 |
|  2 |         2 | 192.168.0.33,u=pt33,p=ptchecksums,P=3308 |
+----+-----------+------------------------------------------+
2 rows in set (0.00 sec)

192.168.0.39 slave库:
mysql> update frame01 set dsn=‘192.168.0.55,u=umaaa,p=ptchsyeudew,P=3310‘ where id=1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> update frame01 set dsn=‘192.168.0.66,u=umbbb,p=ptchsyeudew,P=3311‘ where id=2;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> select * from frame01;
+----+-----------+-------------------------------------------+
| id | parent_id | dsn                                       |
+----+-----------+-------------------------------------------+
|  1 |         1 | 192.168.0.55,u=umaaa,p=ptchsyeudew,P=3310 |
|  2 |         2 | 192.168.0.66,u=umbbb,p=ptchsyeudew,P=3311 |
+----+-----------+-------------------------------------------+

pt-table-sync校验修复数据具体指令:

[root@kusou-es11 ~]#  /usr/local/percona-toolkit/bin/pt-table-sync h=192.168.0.39,u=ptsum,p=ptchecksums,P=3306 --databases=test01 --tables=frame01 --replicate=percona.checksums --print
REPLACE INTO `test01`.`frame01`(`id`, `parent_id`, `dsn`) VALUES (‘1‘, ‘1‘, ‘192.168.0.22,u=pt22,p=ptchecksums,P=3307‘) /*percona-toolkit src_db:test01 src_tbl:frame01 src_dsn:P=3306,h=192.168.0.39,p=...,u=ptsum dst_db:test01 dst_tbl:frame01 dst_dsn:P=3306,h=192.168.0.11,p=...,u=ptsum lock:1 transaction:1 changing_src:percona.checksums replicate:percona.checksums bidirectional:0 pid:176411 user:root host:kusou-es11*/;
REPLACE INTO `test01`.`frame01`(`id`, `parent_id`, `dsn`) VALUES (‘2‘, ‘2‘, ‘192.168.0.33,u=pt33,p=ptchecksums,P=3308‘) /*percona-toolkit src_db:test01 src_tbl:frame01 src_dsn:P=3306,h=192.168.0.39,p=...,u=ptsum dst_db:test01 dst_tbl:frame01 dst_dsn:P=3306,h=192.168.0.11,p=...,u=ptsum lock:1 transaction:1 changing_src:percona.checksums replicate:percona.checksums bidirectional:0 pid:176411 user:root host:kusou-es11*/;

提示: 命令末尾的--print的指令是打印出需要修复数据的命令,不执行命令

pt-table-sync 修复主从库的表frame01数据 ,使主从库表frame01数据一致:


[root@kusou-es11 ~]#  /usr/local/percona-toolkit/bin/pt-table-sync h=192.168.0.39,u=ptsum,p=ptchecksums,P=3306 --databases=test01 --tables=frame01 --replicate=percona.checksums --execute
[root@kusou-es11 ~]# 

提示: 命令末尾的 --execute 的指令是执行修复数据的指令,使master库mysql.user表和slave库的mysql.user表数据一致

pt-table-checksum 检测主从库的表frame01数据一致 (因为 DIFFS =0 )

[root@kusou-es11 ~]#  /usr/local/percona-toolkit/bin/pt-table-checksum  h=192.168.0.39,u=ptsum,p=‘ptchecksums‘,P=3306 --databases=test01 --tables=frame01 --replicate=percona.checksums  --no-check-binlog-format --nocheck-replication-filters  
Checking if all tables can be checksummed ...
Starting checksum ...
            TS ERRORS  DIFFS     ROWS  DIFF_ROWS  CHUNKS SKIPPED    TIME TABLE
06-15T13:41:47      0      0        2          0       1       0   0.316 test01.frame01

slave库查看,数据和主库一致

mysql> select * from test01.frame01;
+----+-----------+------------------------------------------+
| id | parent_id | dsn                                      |
+----+-----------+------------------------------------------+
|  1 |         1 | 192.168.0.22,u=pt22,p=ptchecksums,P=3307 |
|  2 |         2 | 192.168.0.33,u=pt33,p=ptchecksums,P=3308 |
+----+-----------+------------------------------------------+
2 rows in set (0.00 sec)

提示:需要注意的是,需要同步的表上必须要有主键或者唯一索引,否则会出错。
同时,pt-table-sync 修复数据时,会造成锁表,要在业务低峰期来修复主库的数据

对找到的主从不一致的行,采用replace into语句,在主库执行一遍以生成该行全量的binlog,并同步到从库,这会以主库数据为基准来修复从库;
对于主库有的行而从库没有的行,采用replace在主库上插入(必须不能是×××ert);
对于从库有而主库没有的行,通过在主库执行delete来删除(pt-table-sync强烈建议所有的数据修复都只在主库进行,而不建议直接修改从库数据;但是也有特例,以后面会讲到)。

二、实例演示2

在slave库上的mysql user表上,新建一个用户 qdtets@‘192.168.0.%‘ 模拟master和slave库数据不一致

grant all on *.* to qdtets@‘192.168.0.%‘ identified by ‘DHWUOEdwerer‘;flush privileges;
[root@kusou-es11 ~]#  /usr/local/percona-toolkit/bin/pt-table-checksum  h=192.168.0.39,u=ptsum,p=‘ptchecksums‘,P=3306 --databases=mysql --replicate=percona.checksums  --no-check-binlog-format --nocheck-replication-filters  
Checking if all tables can be checksummed ...
Starting checksum ...
            TS ERRORS  DIFFS     ROWS  DIFF_ROWS  CHUNKS SKIPPED    TIME TABLE
06-15T13:32:27      0      0        0          0       1       0   0.317 mysql.columns_priv
06-15T13:32:28      0      0        2          0       1       0   0.318 mysql.db
06-15T13:32:28      0      0        2          0       1       0   0.316 mysql.engine_cost
06-15T13:32:28      0      0        0          0       1       0   0.317 mysql.event
06-15T13:32:29      0      0        0          0       1       0   0.316 mysql.func
06-15T13:32:29      0      0       41          0       1       0   0.317 mysql.help_category
06-15T13:32:29      0      0      699          0       1       0   0.318 mysql.help_keyword
06-15T13:32:30      0      0     1413          0       1       0   0.319 mysql.help_relation
06-15T13:32:30      0      0      643          0       1       0   0.325 mysql.help_topic
06-15T13:32:30      0      0        0          0       1       0   0.316 mysql.ndb_binlog_index
06-15T13:32:31      0      0        0          0       1       0   0.316 mysql.plugin
06-15T13:32:31      0      1       48          0       1       0   0.317 mysql.proc
06-15T13:32:31      0      0        0          0       1       0   0.317 mysql.procs_priv
06-15T13:32:32      0      0        1          0       1       0   0.317 mysql.proxies_priv
06-15T13:32:32      0      0        6          0       1       0   0.317 mysql.server_cost
06-15T13:32:32      0      0        0          0       1       0   0.316 mysql.servers
06-15T13:32:33      0      0        2          0       1       0   0.317 mysql.tables_priv
06-15T13:32:33      0      0        0          0       1       0   0.316 mysql.time_zone
06-15T13:32:33      0      0        0          0       1       0   0.316 mysql.time_zone_leap_second
06-15T13:32:33      0      0        0          0       1       0   0.316 mysql.time_zone_name
06-15T13:32:34      0      0        0          0       1       0   0.318 mysql.time_zone_transition
06-15T13:32:34      0      0        0          0       1       0   0.317 mysql.time_zone_transition_type
**06-15T13:32:34      0      1        6          1       1       0   0.319 mysql.user**

在slave库上pt-table-sync 修复数据:

slave库上操作:

[root@kusou-es11 ~]# /usr/local/percona-toolkit/bin/pt-table-sync h=192.168.0.39,u=ptsum,p=ptchecksums,P=3306 --databases=mysql --tables=user --replicate=percona.checksums --print
Access denied for user ‘ptsum‘@‘192.168.0.%‘ to database ‘mysql‘ [for Statement "LOCK TABLES `mysql`.`user` WRITE"] at line 6172 while doing mysql.user on 192.168.0.11

报错,提示没lock tables 权限

解决办法:登录master库重新授权,添加lock tables权限

grant  update,×××ert,select,create,drop,delete,index,execute,lock tables,super,process,replication slave on *.* to ptsum@‘192.168.0.%‘ identified by ‘ptchecksums‘; flush privileges;

再次操作不在报错br/>**输出提示master库的mysql.user表和slave库的mysql.user表的数据不一致。slave库的mysql.user表多了一个用户qdtets@‘192.168.0.%‘。需要删除这个用户,才能保证主库和从库数据的一致
换句话说:
由于从库只是比主库多了一条数据,pt-table-sync将以主库以准,在主库执行一个删除操作的事件,然后slave应用此事件完成同步**

[root@kusou-es11 ~]# /usr/local/percona-toolkit/bin/pt-table-sync h=192.168.0.39,u=ptsum,p=ptchecksums,P=3306 --databases=mysql --tables=user --replicate=percona.checksums --print
DELETE FROM `mysql`.`user` WHERE `host`=‘192.168.0.%‘ AND `user`=‘qdtets‘ LIMIT 1 /*percona-toolkit src_db:mysql src_tbl:user src_dsn:P=3306,h=192.168.0.39,p=...,u=ptsum dst_db:mysql dst_tbl:user dst_dsn:P=3306,h=192.168.0.11,p=...,u=ptsum lock:1 transaction:0 changing_src:percona.checksums replicate:percona.checksums bidirectional:0 pid:137319 user:root host:kusou-es11*/;

提示: 命令末尾的--print的指令是打印出需要修复数据的命令,不执行命令。

使用pt-table-checksum检测mysql的user表,DIFFS =1 事实证明master和slave的mysql user表数据确实不一致:

[root@kusou-es11 ~]# /usr/local/percona-toolkit/bin/pt-table-checksum  h=192.168.0.39,u=ptsum,p=‘ptchecksums‘,P=3306 --databases=mysql --tables=user  --no-check-binlog-format --nocheck-replication-filters
Checking if all tables can be checksummed ...
Starting checksum ...
            TS ERRORS  DIFFS     ROWS  DIFF_ROWS  CHUNKS SKIPPED    TIME TABLE
06-15T09:58:37      0      1        6          1       1       0   0.318 mysql.user

执行修复命令pt-table-sync 修复master和slave的mysql user表数据:

[root@kusou-es11 ~]#  /usr/local/percona-toolkit/bin/pt-table-sync h=192.168.0.39,u=ptsum,p=ptchecksums,P=3306 --databases=mysql --tables=user --replicate=percona.checksums --execute
[root@kusou-es11 ~]# 

提示: 命令末尾的 --execute 的指令是执行修复数据的指令,使master库mysql.user表和slave库的mysql.user表数据一致

slave库上检查master库的mysql.user表 和slave库的mysql.user 表数据是否一致:

[root@kusou-es11 ~]# /usr/local/percona-toolkit/bin/pt-table-checksum  h=192.168.0.39,u=ptsum,p=‘ptchecksums‘,P=3306 --databases=mysql --tables=user  --no-check-binlog-format --nocheck-replication-filters
Checking if all tables can be checksummed ...
Starting checksum ...
            TS ERRORS  DIFFS     ROWS  DIFF_ROWS  CHUNKS SKIPPED    TIME TABLE
06-15T09:46:52      0      0        6          0       1       0   0.319 mysql.user

可以看到DIFFS =0 ,说明数据已经修复完成

三、实例演示3

将master 上的所有数据同步到slave:

[root@kusou-es11 ~]# /usr/local/percona-toolkit/bin/pt-table-sync --execute  h=192.168.0.39,u=ptsum,p=ptchecksums,P=3306 --databases=mysql --tables=user h=192.168.0.11,u=ptsum,p=‘ptchecksums‘,P=3306 --no-check-slave  --print
DELETE FROM `mysql`.`user` WHERE `host`=‘10.0.0.1‘ AND `user`=‘dhrue‘ LIMIT 1 /*percona-toolkit src_db:mysql src_tbl:user src_dsn:P=3306,h=192.168.0.39,p=...,u=ptsum dst_db:mysql dst_tbl:user dst_dsn:P=3306,h=192.168.0.11,p=...,u=ptsum lock:0 transaction:0 changing_src:0 replicate:0 bidirectional:0 pid:12565 user:root host:kusou-es11*/;

将master 上的所有数据同步到slave1和slave2:


[root@kusou-es11 ~]# /usr/local/percona-toolkit/bin/pt-table-sync --execute  h=192.168.0.39,u=ptsum,p=ptchecksums,P=3306 --databases=mysql --tables=user h=192.168.0.11,u=ptsum,p=‘ptchecksums‘,P=3307    h=192.168.0.22,u=ptsum,p=‘ptchecksums‘,P=3308   --no-check-slave  --print

对pt-table-checksum和pt-table-sync这一组工具进行了最简单的测试,其实运行这一组命令不一定需要在主从结构的主库上进行,网段内的任何服务器都可以运行,前提就是安装好这套工具就好。

四、原理介绍

pt-table-checksum/pt-table-sync原理介绍可以参考如下博文:
http://blog.sina.com.cn/s/blog_a1e9c7910102vnsd.html

pt-table-sync修复主从库不一致数据

标签:second   row   提示   column   权限   category   输出   delete   最简   

原文地址:https://blog.51cto.com/wujianwei/2409529

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!