【工具】主从一致性修复 pt-table-sync

2020-12-29  本文已影响0人  醉红尘丶

pt-table-sync

官方文档:https://www.percona.com/doc/percona-toolkit/3.0/pt-table-sync.html

主要参数介绍

当前环境数据

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)
上一篇 下一篇

猜你喜欢

热点阅读