sync-diff-inspector 使用实践 记录数据同步校验过程

网友投稿 315 2024-03-16



简介

在数据同步的场景下,上下游数据的一致性校验是非常重要的一个环节,缺少数据校验,在某种程度上甚至可以说名整个数据同步是无效的。sync-diff-inspector 是一个用于校验 MySQL/TiDB 中两份数据是否一致的工具。该工具提供了修复数据的功能(适用于修复少量不一致的数据)。首先我们看下 Sync-diff-inspector 的架构图,了解一下 Sync-diff-inspector 的作用和实现原。

sync-diff-inspector 使用实践 记录数据同步校验过程

主要功能

对比表结构和数据

如果数据不一致,则生成用于修复数据的 SQL 语句

支持不同库名或表名的数据校验

支持分库分表场景下的数据校验

支持 TiDB 主从集群的数据校验

支持从 TiDB DM 拉取配置的数据校验

工具下载

https://download.pingcap.org/tidb-community-toolkit-v6.1.0-linux-amd64.tar.gz

[tidb@jian tidb-community-toolkit-v6.1.0-linux-amd64]$ pwd

/home/tidb/tidb-community-toolkit-v6.1.0-linux-amd64

[tidb@jian tidb-community-toolkit-v6.1.0-linux-amd64]$ ls sync_diff_inspector

sync_diff_inspector

数据库用户创建

sync-diff-inspector 需要获取表结构信息、查询数据,需要的数据库权限如下:

源端的目标端的权限需求是一样的

(root@127.0.01) [jian] 16:44:25> create user data_check@% identified by 123456; Query OK, 0 rows affected (0.04 sec) (root@127.0.01) [jian] 16:44:41>  grant show databases,reload,select on *.* to data_check@%; Query OK, 0 rows affected (0.40 sec)

实践

1 对比源端和目标端的同一张表

数据状态

源端:                                                                                                          目标端:

(root@localhost) [jian] 16:55:48> select * from jian.jiantb;                   (root@127.0.01) [jian] 16:53:32> select * from jian.jiantb;

+------+------+                                                                                                    +------+------+

| id   | name |                                                                                                          | id   | name |

+------+------+                                                                                                    +------+------+

|    1 | a    |                                                                                                               |    1 | a    |

|    2 | a    |                                                                                                               |    2 | a    |

|    3 | a    |                                                                                                             +------+------+

+------+------+

对于以上的数据情况我们期望看到的结果是检查数据一致性失败,并且提供sql可以插入目标端不存在的数据(3,‘a’)

配置文件

export-fix-sql = true check-struct-only = false [data-sources] [data-sources.mysql1]     host = "192.168.135.149"     port = 3306     user = "root"     password = "123456" [data-sources.tidb0]     host = "127.0.0.1"     port = 4000     user = "root"     password = "123456" [task]     output-dir = "./output"     source-instances = ["mysql1"]     target-instance = "tidb0"     target-check-tables = ["jian.jiantb"]

执行校验

[tidb@jian ~]$ sync_diff_inspector --config=sync_check

A total of 1 tables need to be compared

Comparing the table structure of ``jian`.`jiantb`` ... equivalent

Comparing the table data of ``jian`.`jiantb`` ... failure

_____________________________________________________________________________

Progress [============================================================>] 100% 0/0

The data of `jian`.`jiantb` is not equal

The rest of tables are all equal.

The patch file has been generated in

        output/fix-on-tidb0/

You can view the comparision details through ./output/sync_diff.log

修复sql查看

[tidb@jian ~]$ cat output/fix-on-tidb0/jian\:jiantb\:0\:0-0\:0.sql

-- table: jian.jiantb

-- range in sequence: Full

REPLACE INTO `jian`.`jiantb`(`id`,`name`) VALUES (3,a);

2 源端和目标端的表名不同

数据状态

源端:                                                                                                          目标端:

(root@localhost) [jian] 16:55:48> select * from jian.yao;                   (root@127.0.01) [jian] 16:53:32> select * from jian.jiantb;

+------+------+                                                                                                    +------+------+

| id   | name |                                                                                                          | id   | name |

+------+------+                                                                                                    +------+------+

|    1 | a    |                                                                                                               |    1 | a    |

+------+------+                                                                                                       |    2 | a    |

                                                                                                                              +------+------+

对于以上的数据情况我们期望看到的结果是,检查出jian.yao和jian.jiantb两张不同表名的表检查数据一致性失败,并且提供sql可以删除源端不存在的数据(2,‘a’)

配置文件

export-fix-sql = true check-struct-only = false [data-sources] [data-sources.mysql1]     host = "192.168.135.149"     port = 3306     user = "root"     password = "123456"     route-rules = ["rule1"] [data-sources.tidb0]     host = "127.0.0.1"     port = 4000     user = "root"     password = "123456" [routes] [routes.rule1] schema-pattern = "jian" table-pattern = "yao" target-schema = "jian" target-table = "jiantb" [task]     output-dir = "./output"     source-instances = ["mysql1"]     target-instance = "tidb0"     target-check-tables = ["jian.jiantb"]

执行校验

[tidb@jian ~]$ sync_diff_inspector --config=sync_check2

A total of 1 tables need to be compared

Comparing the table structure of ``jian`.`jiantb`` ... equivalent

Comparing the table data of ``jian`.`jiantb`` ... failure

_____________________________________________________________________________

Progress [============================================================>] 100% 0/0

The data of `jian`.`jiantb` is not equal

The rest of tables are all equal.

The patch file has been generated in

output/fix-on-tidb0/

You can view the comparision details through ./output/sync_diff.log

修复sql查看

[tidb@jian ~]$ cat output/fix-on-tidb0/jian\:jiantb\:0\:0-0\:0.sql

-- table: jian.jiantb

-- range in sequence: Full

DELETE FROM `jian`.`jiantb` WHERE `id` = 2 AND `name` = a LIMIT 1;

3 对比时指定条件范围

数据状态

源端:                                                                                                          目标端:

(root@localhost) [jian] 18:10:51> select * from jiantb;                            (root@localhost) [jian] 18:10:51> select * from jiantb;

+------+                                                                                                                        +------+

| id   |                                                                                                                                | id   |

+------+                                                                                                                         +------+

|   10 |                                                                                                                               |   10 |

|   20 |                                                                                                                               |   11 |

|   30 |                                                                                                                               |   20 |

|   26 |                                                                                                                               |   30 |

对于以上的数据情况我们期望看到的结果是,检查数据一致性失败,并且提供sql可以插入目标端不存在的数据(26),但是对于目标端比源端多出的11不希望生成删除的sql,因为我们在配置文件中指定了只检查id>20的部分数据。

配置文件

export-fix-sql = true check-struct-only = false [data-sources] [data-sources.mysql1]     host = "192.168.135.149"     port = 3306     user = "root"     password = "123456" [data-sources.tidb0]     host = "127.0.0.1"     port = 4000     user = "root"     password = "123456" [task]     output-dir = "./output"     source-instances = ["mysql1"]     target-instance = "tidb0"     target-check-tables = ["jian.\*"]     target-configs = ["config1"] [table-configs.config1] target-tables=["jian.jiantb"] range = "id > 20"

执行校验

[tidb@jian ~]$ ./tidb-community-toolkit-v6.1.0-linux-amd64/sync_diff_inspector --config=sync_check3

A total of 1 tables need to be compared

Comparing the table structure of ``jian`.`jiantb`` ... equivalent

Comparing the table data of ``jian`.`jiantb`` ... failure

_____________________________________________________________________________

Progress [============================================================>] 100%

The data of `jian`.`jiantb` is not equal

The rest of tables are all equal.

The patch file has been generated in

        output/fix-on-tidb0/

You can view the comparision details through ./output/sync_diff.log

修复sql查看

[tidb@jian ~]$ cat output/fix-on-tidb0/

jian:jiantb:0:0-0:0.sql           .trash-2022-07-28T18:11:39+08:00/

[tidb@jian ~]$ cat output/fix-on-tidb0/jian\:jiantb\:0\:0-0\:0.sql

-- table: jian.jiantb

-- range in sequence: Full

REPLACE INTO `jian`.`jiantb`(`id`) VALUES (26);

4 对比时数据存在部分不一致

数据状态

    源端:                                                                                                          目标端:

(root@localhost) [jian] 16:55:48> select * from jian.yao;                    (root@127.0.01) [jian] 16:53:32> select * from jian.jiantb;

+------+------+                                                                                                    +------+------+

| id   | name |                                                                                                          | id   | name |

+------+------+                                                                                                    +------+------+

|    1 | a    |                                                                                                               |    1 | a    |

|    2 | a    |                                                                                                               |    2 | b   |

+------+------+                                                                                                      +------+------+

对于以上的数据情况我们期望看到的结果是,检查数据一致性失败,并且提供sql可以将目标端的错误数据(2,b)修正为(2,a)

配置文件

export-fix-sql = true check-struct-only = false [data-sources] [data-sources.mysql1]     host = "192.168.135.149"     port = 3306     user = "root"     password = "123456" [data-sources.tidb0]     host = "127.0.0.1"     port = 4000     user = "root"     password = "123456" [task]     output-dir = "./output"     source-instances = ["mysql1"]     target-instance = "tidb0"     target-check-tables = ["jian.jiantb"]

执行校验

[tidb@jian ~]$ ./tidb-community-toolkit-v6.1.0-linux-amd64/sync_diff_inspector --config=sync_check_1

A total of 1 tables need to be compared

Comparing the table structure of ``jian`.`jiantb`` ... equivalent

Comparing the table data of ``jian`.`jiantb`` ... failure

_____________________________________________________________________________

Progress [============================================================>] 100

The data of `jian`.`jiantb` is not equal

The rest of tables are all equal.

The patch file has been generated in

        output/fix-on-tidb0/

You can view the comparision details through ./output/sync_diff.log

修复sql查看

[tidb@jian ~]$ cat output/fix-on-tidb0/jian\:jiantb\:0\:0-0\:0.sql

-- table: jian.jiantb

-- range in sequence: Full

/*

  source data &n

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:sst文件损坏修复实战记录
下一篇:TiCDC 6.0 版本 Sorter 模块演进解析
相关文章