利用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问题原因?
868 1
|
3月前
|
人工智能 Rust 监控
这 3 个开源小工具,帮你让 Coding Agent 少吃点 Token
今天我们就来分享 3 个有用的开源项目,专门帮你的 Coding Agent 整理“上下文”:让它少翻无关代码,少吞冗长日志,把 token 留给更关键的信息。
536 0
这 3 个开源小工具,帮你让 Coding Agent 少吃点 Token
|
10月前
|
人工智能 JavaScript Devops
iFlow CLI x 云效 mcp-server:解锁云效用户的 AI 开发新姿势
iFlow CLI 是一款终端AI助手,可无缝集成云效mcp-server,支持需求管理、代码生成、合并请求及流水线自动化,助力开发者通过命令行高效完成DevOps全流程,显著提升研发效率。
3357 4
|
SQL 关系型数据库 MySQL
阿里面试:1000万级大表, 如何 加索引?
45岁老架构师尼恩在其读者交流群中分享了如何在生产环境中给大表加索引的方法。文章详细介绍了两种索引构建方式:在线模式(Online DDL)和离线模式(Offline DDL),并深入探讨了 MySQL 5.6.7 之前的“影子策略”和 pt-online-schema-change 方案,以及 MySQL 5.6.7 之后的内部 Online DDL 特性。通过这些方法,可以有效地减少 DDL 操作对业务的影响,确保数据的一致性和完整性。尼恩还提供了大量面试题和解决方案,帮助读者在面试中充分展示技术实力。
|
存储 Java 关系型数据库
[LDAP: error code 34 - invalid DN]
`亲测可用,之前搜索了很多博客,啥样的都有,就是不介绍报错以及配置用处,根本不懂照抄那些配置是干啥的,稀里糊涂的按照博客搭完也跑不起来,因此记录这个。` `项目背景`:公司项目当前采用http协议+shiro+mysql的登录认证方式,而现在想支持ldap协议认证登录然后能够访问自己公司的项目网站。 `举例说明`:假设我们公司有自己的门户网站,现在我们收购了一家公司,他们数据库采用ldap存储用户数据,那么为了他们账户能登陆我们公司项目所以需要集成,而不是再把他们的账户重新在mysql再创建一遍,万一人家有1W个账户呢,不累死了且也不现实啊。
366 13
[LDAP: error code 34 - invalid DN]
|
Windows
全网超详细的【Axure】Axure RP 9的下载、安装、中文字体、授权
全网超详细的【Axure】Axure RP 9的下载、安装、中文字体、授权
2717 0
全网超详细的【Axure】Axure RP 9的下载、安装、中文字体、授权
|
存储 NoSQL Redis
零基础手把手带你阅读Redis源代码系列-ZSet底层原理详解(跳表SkipList)
>之前就说了要来西索Redis,现在来辣! >本文的部分内容参考自《小林Coding》,部分地方根据源代码进行剖析。 >Redis源码地址:https://github.com/redis/redis.git
721 0
零基础手把手带你阅读Redis源代码系列-ZSet底层原理详解(跳表SkipList)
|
存储 数据采集 安全
影像信息系统(PACS)介绍
采用DICOM和非DICOM格式获取影像数据; 以通讯方式采集影像原始DICOM数据; 支持静态和动态影像数据采集; 支持透视采集和曝光采集等多种采集方式; 支持非DICOM影像设备的影像数据转化为DICOM标准数据; 支持三维影像处理功能;三维重建,最大/小密度投影、三维容积重建,三维表面重建,虚拟内窥镜、曲面重建,血管重建和分析; 支持胶片打印管理;可设置胶片打印排版,电子胶片管理。
985 0
|
Kubernetes 应用服务中间件 调度
kubernetes Ingress、Ingress controller
kubernetes Ingress、Ingress controller
|
存储 JavaScript 关系型数据库
利用钉钉通讯录同步构建本地LDAP服务
目前钉钉已经成为很多企业日常处理流程的必备工具,但是由于钉钉并没有开放鉴权接口,无法让钉钉作为本地系统的统一鉴权系统使用,每次有同事加入或者离开时,都需要人为的对本地系统进行维护,非常繁琐。那么有没有一种方法可以让钉钉作为本地的统一鉴权系统使用呢?
8464 1
利用钉钉通讯录同步构建本地LDAP服务