利用pt-table-sync 解决主备数据不一致的问题

简介:

https://www.percona.com/doc/percona-toolkit/2.2/pt-table-sync.html

提醒

  • 在使用之前备份将要操作的数据表
  • 使用 --replicate or --sync-to-master方法,是在主库做修改,而不是直接修改备库

同步语法

-- Sync db.tbl on host1 to host2:
pt-table-sync --execute h=host1,D=db,t=tbl h=host2

-- Sync all tables on host1 to host2 and host3:
pt-table-sync --execute host1 host2 host3

-- Make slave1 have the same data as its replication master:
pt-table-sync --execute --sync-to-master slave1 

# Resolve differences that pt-table-checksum found on all slaves of master1:
pt-table-sync --execute --replicate test.checksum master1

# Same as above but only resolve differences on slave1:
pt-table-sync --execute --replicate test.checksum --sync-to-master slave1

# Sync master2 in a master-master replication configuration, where master2’s copy of db.tbl is known or suspected to be incorrect:
pt-table-sync --execute --sync-to-master h=master2,D=db,t=tbl

# Note that in the master-master configuration, the following will NOT do what you want, because it will make changes directly on master2, which will then flow through replication and change master1’s data:
#! Don't do this in a master-master setup!
pt-table-sync --execute h=master1,D=db,t=tbl master2

# 有主键或者唯一键,在主库进行 replace into 的操作
pt-table-sync --execute h=192.168.3.26,u=root,p=zhujie1986,D=working,t=department,P=3306 --sync-to-master --verbose --verbose --charset=utf8 --print

# 没主键或唯一键,直接在备库操作,要有超级用户权限
pt-table-sync --execute h=192.168.3.25,u=root,p=zhujie1986,D=working,t=department,P=3306 h=192.168.3.26 --no-check-slave --verbose --verbose --charset=utf8 --print

风险

FBI WARNING: pt-table-sync changes data! Before using this tool, please:

Read the tool’s documentation
Review the tool’s known “BUGS”
Test the tool on a non-production server
Backup your production server and verify the backups
pt-table-sync is mature, proven in the real world, and well tested, but if used improperly it can have adverse consequences. Always test syncing first with --dry-run and --print.

功能点

  • 使用单向和双向同步数据
  • 并不会同步表结构、索引或者其他对象
  • 针对单向数据同步

    • --replicate的目的
    • 找出不同
    • 匹配主库

      if DSN has a t part, sync only that table:
         if 1 DSN:
            if --sync-to-master:
               The DSN is a slave.  Connect to its master and sync.
         if more than 1 DSN:
            The first DSN is the source.  Sync each DSN in turn.
      else if --replicate:
         if --sync-to-master:
            The DSN is a slave.  Connect to its master, find records
            of differences, and fix.
         else:
            The DSN is the master.  Find slaves and connect to each,
            find records of differences, and fix.
      else:
         if only 1 DSN and --sync-to-master:
            The DSN is a slave.  Connect to its master, find tables and
            filter with --databases etc, and sync each table to the master.
         else:
            find tables, filtering with --databases etc, and sync each
            DSN to the first.
  1. pt-table-sync默认不使用 --replicate参数,程序内部找出表数据的差异并修复差异
  2. 如果启用,pt-table-sync会读取 pt-table-checksum已经验证出的差异信息
  3. 必须指定需要同步的数据库信息:

    • --sync-to-master,后面跟备库的信息;程序运行过程中自动发现并连接主库
    • 检测到差异,在主库上做修改;通过复制,同步到备库
    • 如果是一主多重的环境,那么所有备库都会同步更新
    • 如果不指定 --sync-to-master,那么必须指定至少两个 DSN配置,最前一个作为主库,后一个作为备库
    • 如果配置为主库的信息实际上是备库,那么进程将停止运行,因为备库不可写;
  4. 如果使用了 --replicate但是没有使用 --sync-to-master,那么只需要一个主库的DSN配置;程序会自动发现所有的备库,并且同时修复差异的数据表
  5. 以 DSN的形式配置的第一个数据库,其后的 DSN配置会使用第一个的参数资源,比如
pt-table-sync --execute h=host1,u=msandbox,p=msandbox h=host2

host2 将会使用 host1的 u,p参数连接数据库

限制

Replicas using row-based replication

pt-table-sync requires statement-based replication when used with the --sync-to-master or --replicate option. Therefore it will set binlog_format=STATEMENT on the master for its session if required. To do this user must have SUPER privilege.

输出 --verbose --print --charset=utf8

pt-table-checksum --nocheck-binlog-format --nocheck-replication-filters --replicate=percona.checksums --set-vars innodb_lock_wait_timeout=50 --host=192.168.3.25 --port=3306 --user=root --password=zhujie1986 --databases working --tables department --replicate-check
            TS ERRORS  DIFFS     ROWS  CHUNKS SKIPPED    TIME TABLE
01-18T14:58:11      0      1        7       1       0   0.009 working.department

pt-table-sync --execute h=192.168.3.25,u=root,p=zhujie1986,D=working,t=department,P=3306 h=192.168.3.26 --no-check-slave --verbose --charset=utf8
# Syncing A=utf8,D=working,P=3306,h=192.168.3.26,p=...,t=department,u=root
# DELETE REPLACE INSERT UPDATE ALGORITHM START    END      EXIT DATABASE.TABLE
#      0       0      7      0 GroupBy   14:59:28 14:59:28 2    working.department

同步处理流程

  • 在主备表结构相同,且存在唯一索引或主键的情况下,优先使用 INSERT UPDATE DELETE 操作数据,解决数据差异问题
  • 在主备表结构不同,但是主库表存在主键,备库表存在唯一索引的情况下,将会使用 DELETE REPLACE 修复数据

可选参数

- --verbose:输出差异数据处理信息,--verbose --verbose 输出块信息
- --print:输出处理 SQL语句
- --charset=utf8:设置编码,主要针对插入
- --no-check-slave:直接在备库插入,需要超级用户权限

算法

  • 使用不同的算法来验证数据差异
  • 根据索引、字段类型以及 --algorithms参数指定的值来选择最优的算法
  • Chunk

    • 第一个字段是数字类型(date/time)的索引,并根据 --chunk-size的值设置 chunk大小和个数
    • 每次验证一个块,整个块作为一个整体算出一个值
    • 如果取得的块值不相同,那么单独验证这个块的数据
    • 每个块相对来说都是很小的,小号的系统资源、带宽等可以忽略不计
    • 验证块数据的时候,只有主键和算法值会通过网络传输,一边验证
    • 验证结果有差异,才会传输整个块的行记录
目录
相关文章
|
测试技术 网络架构
【技术干货连载 一】业务经过WAF HTTP 400问题排查
教你如何排查和解决业务经过WAF 七层代理HTTP 400问题原因?
891 1
|
3月前
|
人工智能 Rust 监控
这 3 个开源小工具,帮你让 Coding Agent 少吃点 Token
今天我们就来分享 3 个有用的开源项目,专门帮你的 Coding Agent 整理“上下文”:让它少翻无关代码,少吞冗长日志,把 token 留给更关键的信息。
571 0
这 3 个开源小工具,帮你让 Coding Agent 少吃点 Token
|
6月前
|
网络协议 数据可视化 应用服务中间件
如何申请免费ssl证书以及自动续期
本文详解SSL证书申请与自动续期方案:推荐可视化工具CertGo(桌面端)和轻量脚本acme.sh。涵盖阿里云DNS配置、FreeSSL账号绑定(EAB)、多域名/泛域名申请、Nginx部署及自动续签,零依赖、易上手,助你轻松实现HTTPS全站加密。(239字)
2124 3
|
10月前
|
人工智能 JavaScript Devops
iFlow CLI x 云效 mcp-server:解锁云效用户的 AI 开发新姿势
iFlow CLI 是一款终端AI助手,可无缝集成云效mcp-server,支持需求管理、代码生成、合并请求及流水线自动化,助力开发者通过命令行高效完成DevOps全流程,显著提升研发效率。
3382 4
|
SQL 关系型数据库 MySQL
阿里面试:1000万级大表, 如何 加索引?
45岁老架构师尼恩在其读者交流群中分享了如何在生产环境中给大表加索引的方法。文章详细介绍了两种索引构建方式:在线模式(Online DDL)和离线模式(Offline DDL),并深入探讨了 MySQL 5.6.7 之前的“影子策略”和 pt-online-schema-change 方案,以及 MySQL 5.6.7 之后的内部 Online DDL 特性。通过这些方法,可以有效地减少 DDL 操作对业务的影响,确保数据的一致性和完整性。尼恩还提供了大量面试题和解决方案,帮助读者在面试中充分展示技术实力。
|
编译器 C++
fmt文本格式库的源码下载编译(Win10+VS2022)
fmt文本格式库的源码下载编译(Win10+VS2022)
1214 0
|
算法
记一次requests.get()返回数据乱码问题
【10月更文挑战第21天】使用`requests.get()`请求网页时遇到乱码问题,尝试通过设置`encoding`和使用`apparent_encoding`自动判断编码均无效。最终发现问题是由于请求头中的`Accept-Encoding`包含了`br`(Brotli压缩格式),导致响应内容未被正确解压。移除`Accept-Encoding`中的`br`后,问题得到解决。
1021 7
|
Windows
全网超详细的【Axure】Axure RP 9的下载、安装、中文字体、授权
全网超详细的【Axure】Axure RP 9的下载、安装、中文字体、授权
2794 0
全网超详细的【Axure】Axure RP 9的下载、安装、中文字体、授权
|
存储 NoSQL Redis
零基础手把手带你阅读Redis源代码系列-ZSet底层原理详解(跳表SkipList)
>之前就说了要来西索Redis,现在来辣! >本文的部分内容参考自《小林Coding》,部分地方根据源代码进行剖析。 >Redis源码地址:https://github.com/redis/redis.git
732 0
零基础手把手带你阅读Redis源代码系列-ZSet底层原理详解(跳表SkipList)
|
Kubernetes 应用服务中间件 调度
kubernetes Ingress、Ingress controller
kubernetes Ingress、Ingress controller