MySQL数据库主从数据对比及修复

点击上方蓝字关注我

MySQL主从数据不一致是比较常见的情况,如何对比,如何修复是DBA必知必会的一项熟练的技能。

1、工具安装

使用percona-Toolkit进行数据对比,因此先进行此工具的安装

官方地址:

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

安装依赖包

yum install -y perl-DBI  perl-DBD-MySQL   perl-Time-HiRes  perl-IO-Socket-SSL perl-TermReadKey

安装工具

rpm  -ivh  percona-toolkit-3.0.4-1.el6.x86_64.rpm

2. 数据对比

数据对比工具使用pt-table-checksum 进行主从数据对比,可以参考如下命令

pt-table-checksum  --host=127.0.0.1--port 3306 --databases=test1 -uroot -ppass   --no-check-binlog-format

对比过程如下:

[root@mysql1 ~]# pt-table-checksum  --host=127.0.0.1 --port 3346 --databases=test1 -uroot -ppass   --no-check-binlog-format
Diffs cannot be detected because no slaves were found.  Please read the --recursion-method documentation for information.
# A software update is available:
            TS ERRORS  DIFFS     ROWS  CHUNKS SKIPPED    TIME TABLE
08-19T00:05:20      0      0     9759       4       0   0.742 test1.COST_RISKFREE
08-19T00:05:20      0      0    17862       1       0   0.349 test1.FFUT_QUOTATION
08-19T00:05:27      0      0   461012      12       0   6.714 test1.STK_MKT_QUOTATION
08-19T00:05:27      0      0       20       1       0   0.177 test1.e
08-19T00:05:27      0      0        1       1       0   0.171 test1.heartbeat
08-19T00:05:28      0      0        1       1       0   0.175 test1.test_int
You have new mail in /var/spool/mail/root

字段说明:

TS            :完成检查的时间。
ERRORS        :检查时候发生错误和警告的数量。
DIFFS         :0表示一致,1表示不一致。当指定--no-replicate-check时,会一直为0,当指定--replicate-check-only会显示不同的信息。
ROWS          :表的行数。
CHUNKS        :被划分到表中的块的数目。
SKIPPED       :由于错误或警告或过大,则跳过块的数目。
TIME          :执行的时间。
TABLE         :被检查的表名

如出现DIFFS不为0 的情况,即对应表主从数据不一致

3. 数据修复

如果出现主从数据不一致的情况,则需要进行数据修复,修复的方式通常是通过主库的数据修复从库。修复时使用pt-table-sync工具进行处理,案例如下:

pt-table-sync --execute  --databases=test1  --tables=COST_RISKFREE   --replicate percona.checksums --sync-to-master  h=106.14.184.46,u=root,p=123456
# TableChecksum:4998 5214 SELECT db, tbl, CONCAT(db, '.', tbl) AS `table`, chunk, chunk_index, lower_boundary, upper_boundary, COALESCE(this_cnt-master_cnt, 0) AS cnt_diff, COALESCE(this_crc <> master_crc OR ISNULL(master_crc) <> ISNULL(this_crc), 0) AS crc_diff, this_cnt, master_cnt, this_crc, master_crc FROM percona.checksums WHERE master_cnt <> this_cnt OR master_crc <> this_crc OR ISNULL(master_crc) <> ISNULL(this_crc)
# TableChecksum:4998 5214 SELECT db, tbl, CONCAT(db, '.', tbl) AS `table`, chunk, chunk_index, lower_boundary, upper_boundary, COALESCE(this_cnt-master_cnt, 0) AS cnt_diff, COALESCE(this_crc <> master_crc OR ISNULL(master_crc) <> ISNULL(this_crc), 0) AS crc_diff, this_cnt, master_cnt, this_crc, master_crc FROM percona.checksums WHERE master_cnt <> this_cnt OR master_crc <> this_crc OR ISNULL(master_crc) <> ISNULL(this_crc)
# SchemaIterator:7923 5214 Table heartbeat is not in --tables list, ignoring
# pt_table_sync:10262 5214 No checksum differences
# pt_table_sync:11128 5214 Disconnected dbh DBI::db=HASH(0x2c229e0)
# pt_table_sync:11128 5214 Disconnected dbh DBI::db=HASH(0x25c18e0)
# pt_table_sync:11128 5214 Disconnected dbh DBI::db=HASH(0x25c1e80)
# pt_table_sync:11128 5214 Disconnected dbh DBI::db=HASH(0x25c1610)
完成后,可以再次进行检测是否修复完毕。

往期精彩回顾

1.  MySQL高可用之MHA集群部署

2.  mysql8.0新增用户及加密规则修改的那些事

3.  比hive快10倍的大数据查询利器-- presto

4.  监控利器出鞘:Prometheus+Grafana监控MySQL、Redis数据库

5.  PostgreSQL主从复制--物理复制

6.  MySQL传统点位复制在线转为GTID模式复制

7.  MySQL敏感数据加密及解密

8.  MySQL数据备份及还原(一)

9.  MySQL数据备份及还原(二)

扫码关注     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值