pt-table-sync
- 5.7.27
- 主从已搭建完成,101主,102从
官方文档:https://www.percona.com/doc/percona-toolkit/3.0/pt-table-sync.html
主要参数介绍
- --replicate :指定通过pt-table-checksum得到的表,修复差异数据.
- --databases : 指定执行同步的数据库。
- --tables :指定执行同步的表,多个用逗号隔开。
- --sync-to-master :指定从的IP,使指定从和主做同步。
- h= :服务器地址,命令里有2个ip,第一次出现的是Master的地址,第2次是Slave的地址。
- u= :帐号。
- p= :密码。
- --print :打印,但不执行命令。
- --execute :执行命令。
当前环境数据
mysql> show create table abc.tb1\G
*************************** 1. row ***************************
Table: tb1
Create Table: CREATE TABLE `tb1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`a` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)
# 101 主
mysql> select * from abc.tb1;
+----+--------------+
| id | a |
+----+--------------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 隔壁老王 |
+----+--------------+
5 rows in set (0.00 sec)
# 102从
mysql> select * from abc.tb1;
+----+------+
| id | a |
+----+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
+----+------+
4 rows in set (0.00 sec)
首先需要 pt-table-checksum 一下
pt-table-checksum具体请看另一篇文章,检测完成后发现有数据不一致
pt-table-checksum --nocheck-binlog-format --nocheck-replication-filters --replicate=check_test.checksums --databases=abc --host=192.168.66.101 --port=3306 --user=check_user --password=123456
Checking if all tables can be checksummed ...
Starting checksum ...
TS ERRORS DIFFS ROWS DIFF_ROWS CHUNKS SKIPPED TIME TABLE
12-30T17:39:24 0 1 5 1 1 0 0.038 abc.tb1
12-30T17:39:24 0 0 3 0 1 0 0.019 abc.tb2
数据不一致修复
# 可以先打印看一下
pt-table-sync --replicate=check_test.checksums h=192.168.66.101,u=check_user,p=123456 h=192.168.66.102,u=check_user,p=123456 --print
# 执行同步并打印,这边的地址是master的用户名密码
pt-table-sync --execute --replicate check_test.checksums h=192.168.66.101,u=check_user,p=123456 --print
REPLACE INTO `abc`.`tb1`(`id`, `a`) VALUES ('5', '隔壁老王') /*percona-toolkit src_db:abc src_tbl:tb1 src_dsn:h=192.168.66.101,p=...,u=check_user dst_db:abc dst_tbl:tb1 dst_dsn:h=192.168.66.102,p=...,u=check_user lock:1 transaction:1 changing_src:check_test.checksums replicate:check_test.checksums bidirectional:0 pid:7980 user:root host:initnode101*/;
查看结果
数据同步一致了
# 101 主
mysql> select * from abc.tb1;
+----+--------------+
| id | a |
+----+--------------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 隔壁老王 |
+----+--------------+
5 rows in set (0.00 sec)
mysql> show master status;
+------------+----------+--------------+------------------+--------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------+----------+--------------+------------------+--------------------------------------------+
| bin.000005 | 12379 | | | a7776f71-c8be-11e9-838f-0050563bb195:1-110 |
+------------+----------+--------------+------------------+--------------------------------------------+
1 row in set (0.00 sec)
# 102 从
mysql> select * from abc.tb1;
+----+--------------+
| id | a |
+----+--------------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 隔壁老王 |
+----+--------------+
5 rows in set (0.00 sec)
mysql> show master status;
+------------+----------+--------------+------------------+------------------------------------------------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------+----------+--------------+------------------+------------------------------------------------------------------------------------+
| bin.000001 | 3435 | | | 22302bb1-c8bb-11e9-8669-00505620a3cf:1,
a7776f71-c8be-11e9-838f-0050563bb195:1-110 |
+------------+----------+--------------+------------------+------------------------------------------------------------------------------------+
1 row in set (0.00 sec)











网友评论