黄东旭解析 TiDB 的核心优势
766
2024-03-16
在数据同步的场景下,上下游数据的一致性校验是非常重要的一个环节,缺少数据校验,在某种程度上甚至可以说名整个数据同步是无效的。sync-diff-inspector 是一个用于校验 MySQL/TiDB 中两份数据是否一致的工具。该工具提供了修复数据的功能(适用于修复少量不一致的数据)。首先我们看下 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)源端: 目标端:
(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’)
[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
[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);
源端: 目标端:
(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’)
[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
[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;
源端: 目标端:
(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的部分数据。
[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
[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);
源端: 目标端:
(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)
[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
[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小时内删除侵权内容。