mysql 主从服务- 数据不一致使用pt-table-sync工具恢复数据

51 篇文章 0 订阅
20 篇文章 0 订阅

我们可以通过使用一个工具 pt-table-sync 进行数据的同步。

工具的安装,请看这篇文章:https://blog.csdn.net/qq_39408664/article/details/119422712

手册地址:https://www.percona.com/doc/percona-toolkit/LATEST/pt-table-sync.html


首先,我们查询一下数据的对比

在这里插入图片描述

在主库中执行:

在这里插入图片描述
如上图,执行效验后,需要进行多余的数据删除。

执行打印命令:


# 打印信息,但不执行命令
[root@localhost ~]# pt-table-sync --sync-to-master h=192.168.183.181,u=mytest_slave,p=root,P=3306 --databases=mytest --print

命令操作指令解释:


pt-table-sync [options] dsn [dsn]

--replicate= :指定通过pt-table-checksum得到的表,这2个工具差不多都会一直用。

--databases= : 指定执行同步的数据库,多个用逗号隔开。

--tables= :指定执行同步的表,多个用逗号隔开。

--sync-to-master :指定一个DSN,即从的IP,他会通过show processlist或show slave status去自动的找主。

h=127.0.0.1 :服务器地址,命令里有2个ip,第一次出现的是Master的地址,第2次是Slave的地址。

u=root :帐号。

p=123456 :密码。

--print :打印,但不执行命令。

--execute :执行命令。

建议:

  1. 修复数据的时候,用 --print 打印出来,这样就可以知道哪些数据有问题。
  2. 修复数据之前一定要备份数据库;然后再手动执行或者添加 --execute

执行修复命令:


# 执行修复命令
[root@localhost ~]# pt-table-sync --sync-to-master h=192.168.183.181,u=mytest_slave,p=root,P=3306 --databases=mytest --execute

数据一致查看:
在这里插入图片描述



设置定时任务

线上项目不太建议做定时任务执行,从上面知道,恢复数据是把多余的数据删除的。

我们可以把这个编辑成脚本,定期通过 centos 定时器定期检查,对于我们来说我们执行在意的是通过 pt-table-checksum 显示信息中的 DIFFS 信息。


[root@localhost ~]# pt-table-checksum --nocheck-replication-filters --no-check-binlog-format --replicate=check_data.checksums --databases=mytest --tables=user --user=mytest_slave --password=root
Checking if all tables can be checksummed ...
Starting checksum ...
            TS ERRORS  DIFFS     ROWS  DIFF_ROWS  CHUNKS SKIPPED    TIME TABLE
08-06T15:16:11      0      0        7          0       1       0   0.018 mytest.user

对于 cemtos 来说,我们可以通过 awk 命令获取到 DIFFS 中的值,然后判断这个值是否不等于 0;则可以判断是否一致。


[root@localhost ~]# pt-table-checksum --nocheck-replication-filters --no-check-binlog-format --replicate=check_data.checksums --databases=mytest --tables=user --user=mytest_slave --password=root | awk 'NR>1{sum+=$3}END{print sum}'
0
# 数据一致则输出0,否则输出相差的数

下一步要做的就是编辑 shell 脚本 - 额外解释,所谓的 sh 脚本就是可以直接模拟 centos 执行我们在命令台执行的命令,然后根据返回的结果进行相应的逻辑处理,我们可以创建一个 pt-table-checksums(名字是自取的)文件;注意!!!windows 下编辑的 sh 脚本在 linux 中执行可能会存在一定的问题,推荐可以直接在 linux 中编辑 sh 脚本,这样问题会少很多。


# 我们放在 home 目录下,所以先进入 home 目录
[root@localhost ~]# cd /home/

# 然后,直接使用 vi / vim 编辑文件即可创建
[root@localhost home]# vi pt-table-checksum-timer.sh

# 输入脚本内容--Start

#!/usr/bin/env bash
NUM=`pt-table-checksum --nocheck-replication-filters --no-check-binlog-format --replicate=check_data.checksums --databases=mytest --tables=user --user=mytest_slave --password=root | awk 'NR>1{sum+=$3}END{print sum}'`

if [ $NUM -eq 0 ];then
echo "Data is Ok!"
else
echo "Data is Error!"
pt-table-sync --sync-to-master h=192.168.183.181,u=mytest_save,p=root,P=3306 --databases=mytest --print
pt-table-sync --sync-to-master h=192.168.183.181,u=mytest_save,p=root,P=3306 --databases=mytest --execute
fi

# 输入脚本内容--End

# 脚本内容解释
# #!/usr/bin/env bash   代表 shell 脚本的符号,和 <?php 相同
# NUM=.......   定义需要执行的命令
# if [ $NUM -eq 0 ];then  如果执行命令的结果等于0,进入判断里面
# else  否则进入下面

# 第一条命令是查看信息
#pt-table-sync --sync-to-master h=192.168.183.181,u=mytest_save,p=root,P=3306 --databases=mytest --print
# 第二条命令是执行修复
#pt-table-sync --sync-to-master h=192.168.183.181,u=mytest_save,p=root,P=3306 --databases=mytest --execute

# 如果有多个从服务,需要多加几个检测点,例如:
#pt-table-sync --sync-to-master h=192.168.183.181,u=mytest_save,p=root,P=3306 --databases=mytest --print
#pt-table-sync --sync-to-master h=192.168.183.181,u=mytest_save,p=root,P=3306 --databases=mytest --execute
#pt-table-sync --sync-to-master h=192.168.183.182,u=mytest_save,p=root,P=3306 --databases=mytest --print
#pt-table-sync --sync-to-master h=192.168.183.182,u=mytest_save,p=root,P=3306 --databases=mytest --execute
#pt-table-sync --sync-to-master h=192.168.183.183,u=mytest_save,p=root,P=3306 --databases=mytest --print
#pt-table-sync --sync-to-master h=192.168.183.183,u=mytest_save,p=root,P=3306 --databases=mytest --execute
# 每个IP地址对应每个从服务的IP地址

# fi 判断结束


然后通过编辑 crontab -e 定时执行这个脚本就好。


[root@localhost home]# crontab -e
* * * * * /home/pt-table-checksum-timer.sh
# 保存退出即可

# 解释
* * * * * [file]  代表每分钟执行路径下的文件
20 23 * * * [file]  代表每天晚上 23:20 执行路径下文件

# 其他设置时间网上搜索一下。


# 给权限,不然报错权限不够
[root@localhost home]# chmod 777 pt-table-checksum-timer.s





  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值