利用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大小和个数
    • 每次验证一个块,整个块作为一个整体算出一个值
    • 如果取得的块值不相同,那么单独验证这个块的数据
    • 每个块相对来说都是很小的,小号的系统资源、带宽等可以忽略不计
    • 验证块数据的时候,只有主键和算法值会通过网络传输,一边验证
    • 验证结果有差异,才会传输整个块的行记录
目录
相关文章
|
安全 数据安全/隐私保护 Ruby
5分钟带你重置Gitlab管理员账户密码
5分钟带你重置Gitlab管理员账户密码
4122 1
|
存储 关系型数据库 MySQL
|
SQL
记一次不常见到主从延迟问题
Slave_SQL_Running_State: Waiting for dependent transaction to commit 导致的主从延迟
8215 1
|
SQL 关系型数据库 MySQL
pt-tools系列:pt-online-schema-change 最佳实践
pt的详细步骤 Step 1: Create the new table. Step 2: Alter the new, empty table. This should be very quick, or die if the user specified a bad alter statement.
5578 0
|
6月前
|
SQL 关系型数据库 MySQL
Flink CDC 3.4 发布, 优化高频 DDL 处理,支持 Batch 模式,新增 Iceberg 支持
Apache Flink CDC 3.4.0 版本正式发布!经过4个月的开发,此版本强化了对高频表结构变更的支持,新增 batch 执行模式和 Apache Iceberg Sink 连接器,可将数据库数据全增量实时写入 Iceberg 数据湖。51位贡献者完成了259次代码提交,优化了 MySQL、MongoDB 等连接器,并修复多个缺陷。未来 3.5 版本将聚焦脏数据处理、数据限流等能力及 AI 生态对接。欢迎下载体验并提出反馈!
1160 1
Flink CDC 3.4 发布, 优化高频 DDL 处理,支持 Batch 模式,新增 Iceberg 支持
|
5月前
|
存储 SQL 关系型数据库
【赵渝强老师】使用mydumper备份MySQL
本文介绍了使用mydumper工具进行MySQL数据库备份与恢复的操作方法。相比单线程工作的mysqldump,mydumper支持多线程,速度提升可达10倍。其功能包括事务性表快照、快速压缩、导出binlog等,并提供详细的参数说明和操作步骤。文章通过实例演示了安装mydumper、创建存储目录、全库备份、指定数据库及表备份、删除数据库以及使用myloader恢复数据的完整流程,并附带视频讲解,帮助用户更好地理解和应用该工具。
234 0
|
SQL 监控 关系型数据库
使用 pt-query-digest 工具分析 MySQL 慢日志
【8月更文挑战第5天】使用 pt-query-digest 工具分析 MySQL 慢日志
734 3
使用 pt-query-digest 工具分析 MySQL 慢日志
|
缓存 NoSQL Java
SpringBoot的三种缓存技术(Spring Cache、Layering Cache 框架、Alibaba JetCache 框架)
Spring Cache 是 Spring 提供的简易缓存方案,支持本地与 Redis 缓存。通过添加 `spring-boot-starter-data-redis` 和 `spring-boot-starter-cache` 依赖,并使用 `@EnableCaching` 开启缓存功能。JetCache 由阿里开源,功能更丰富,支持多级缓存和异步 API,通过引入 `jetcache-starter-redis` 依赖并配置 YAML 文件启用。Layering Cache 则提供分层缓存机制,需引入 `layering-cache-starter` 依赖并使用特定注解实现缓存逻辑。
2862 1
SpringBoot的三种缓存技术(Spring Cache、Layering Cache 框架、Alibaba JetCache 框架)
|
关系型数据库 OLAP 分布式数据库
揭秘Polardb与OceanBase:从OLTP到OLAP,你的业务选对数据库了吗?热点技术对比,激发你的选择好奇心!
【8月更文挑战第22天】在数据库领域,阿里巴巴的Polardb与OceanBase各具特色。Polardb采用共享存储架构,分离计算与存储,适配高并发OLTP场景,如电商交易;OceanBase利用灵活的分布式架构,优化数据分布与处理,擅长OLAP分析及大规模数据管理。选择时需考量业务特性——Polardb适合事务密集型应用,而OceanBase则为数据分析提供强大支持。
4256 2
|
Kubernetes Cloud Native Java
灰度发布、蓝绿部署、金丝雀都是啥?
在滚动部署中,应用的新版本逐步替换旧版本。实际的部署发生在一段时间内。在此期间,新旧版本会共存,而不会影响功能和用户体验。这个过程可以更轻易的回滚和旧组件不兼容的任何新组件。
灰度发布、蓝绿部署、金丝雀都是啥?