使用percona-toolkit同步mysql表数据

背景

做了主备mysql的配置以后,可能因为切换过程造成不一致的情况,这个时候可以处理的方式是全量导入再导出,这个有个问题就是操作的数据太多了
我们只需要数据补全同步即可

mysql的同步是基于binlog的,如果没有记录的部分的数据,这个是无法同步的,就需要手动处理

或者就是两个数据库,定期手动同步到另外一个数据库,这个都可以使用这个工具

工具下载

下载网址

https://www.percona.com/percona-toolkit

我的环境是centos7的,那么使用的就是3.6.0版本

1
wget https://downloads.percona.com/downloads/percona-toolkit/3.6.0/binary/redhat/7/x86_64/percona-toolkit-3.6.0-1.el7.x86_64.rpm

下载完成后安装即可

同步数据

同步这个需要数据库和表都存在,这个如果缺少的,需要处理下

1
[root@lab102 ~]# pt-table-sync --print  --ignore-databases=mysql   h=192.168.0.101,u=root,p='password'   h=192.168.0.103,u=root,p='password'

这个是打印准备处理的sql语句,用于检查,避免操作错误
上面的前面的是主,让后面的跟前面的同步,也就是让192.168.0.103的数据跟192.168.0.101的数据一致
确认没有问题后再执行

1
[root@lab102 ~]# pt-table-sync --execute  --ignore-databases=mysql   h=192.168.0.101,u=root,p='password'   h=192.168.0.103,u=root,p='password'

执行完就会开始执行同步的操作

pt-slave-find查找副本的结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@lab102 percona]# pt-slave-find h=192.168.0.101,u=root,p='password'
192.168.0.101
Version 8.0.30
Server ID 1
Uptime 01:56:19 (started 2025-04-15T15:11:29)
Replication Is not a slave, has 1 slaves connected, is not read_only
Filters binlog_ignore_db=mysql,sys,information_schema,performance_schema
Binary logging MIXED
Slave status
Slave mode STRICT
Auto-increment increment 1, offset 1
InnoDB version 8.0.30
+- 192.168.0.102
Version 8.0.30
Server ID 2
Uptime 06:20 (started 2025-04-15T17:01:28)
Replication Is a slave, has 0 slaves connected, is read_only
Filters binlog_ignore_db=mysql,sys,information_schema,performance_schema
Binary logging MIXED
Slave status 0 seconds behind, running, no errors
Slave mode STRICT
Auto-increment increment 1, offset 1
InnoDB version 8.0.30

可以拿到一些信息,主本的信息,副本的节点的信息,这个用来做监控不错

总结

这个工具应该还有其它功能,目前只用到这一个,后续再继续看看


使用percona-toolkit同步mysql表数据
https://zphj1987.com/2025/04/15/使用percona-toolkit同步mysql表数据/
作者
zphj1987
发布于
2025年4月15日
许可协议