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

sync-diff-inspector 支持对分库分表场景进行数据校验。例如有多个 MySQL 实例,当你使用同步工具 TiDB DM 同步到一个 TiDB 时,可以使用 sync-diff-inspector 对上下游数据进行校验。

使用 datasource config 进行配置

使用 Datasource config 对 table-0 进行特殊配置,设置对应 rules,配置上游表与下游表的映射关系。这种配置方式需要对所有分表进行设置,适合上游分表数量较少,且分表的命名规则没有规律的场景。场景如图所示:

shard-table-sync-1

sync-diff-inspector 完整的示例配置如下:

1. # Diff Configuration.

3. ######################### Global config #########################

5. # 检查数据的线程数量,上下游数据库的连接数会略大于该值
6. check-thread-count = 4

8. # 如果开启,若表存在不一致,则输出用于修复的 SQL 语句
9. export-fix-sql = true

11. # 只对比表结构而不对比数据
12. check-struct-only = false

15. ######################### Datasource config #########################
16. [data-sources.mysql1]
17. host = "127.0.0.1"
18. port = 3306
19. user = "root"
20. password = ""

22. route-rules = ["rule1"]

24. [data-sources.mysql2]
25. host = "127.0.0.1"
26. port = 3306
27. user = "root"
28. password = ""

30. route-rules = ["rule2"]

32. [data-sources.tidb0]
33. host = "127.0.0.1"
34. port = 4000
35. user = "root"
36. password = ""

38. ########################### Routes ###########################
39. [routes.rule1]
40. schema-pattern = "test"        # 匹配数据源的库名,支持通配符 "*" 和 "?"
41. table-pattern = "table-[1-2]"  # 匹配数据源的表名,支持通配符 "*" 和 "?"
42. target-schema = "test"         # 目标库名
43. target-table = "table-0"       # 目标表名

45. [routes.rule2]
46. schema-pattern = "test"      # 匹配数据源的库名,支持通配符 "*" 和 "?"
47. table-pattern = "table-3"    # 匹配数据源的表名,支持通配符 "*" 和 "?"
48. target-schema = "test"       # 目标库名
49. target-table = "table-0"     # 目标表名

51. ######################### Task config #########################
52. [task]
53. output-dir = "./output"

55. source-instances = ["mysql1", "mysql2"]

57. target-instance = "tidb0"

59. # 需要比对的下游数据库的表,每个表需要包含数据库名和表名,两者由 `.` 隔开
60. target-check-tables = ["test.table-0"]

当上游分表较多,且所有分表的命名都符合一定的规则时,则可以使用 table-rules 进行配置。场景如图所示:

shard-table-sync-2

sync-diff-inspector 完整的示例配置如下:

1. # Diff Configuration.

3. ######################### Global config #########################

5. # 检查数据的线程数量,上下游数据库的连接数会略大于该值
6. check-thread-count = 4

8. # 如果开启,若表存在不一致,则输出用于修复的 SQL 语句
9. export-fix-sql = true

11. # 只对比表结构而不对比数据
12. check-struct-only = false

15. ######################### Datasource config #########################
16. [data-sources.mysql1]
17. host = "127.0.0.1"
18. port = 3306
19. user = "root"
20. password = ""

22. route-rules = ["rule1"]

24. [data-sources.mysql2]
25. host = "127.0.0.1"
26. port = 3306
27. user = "root"
28. password = ""

30. route-rules = ["rule1"]

32. [data-sources.tidb0]
33. host = "127.0.0.1"
34. port = 4000
35. user = "root"
36. password = ""

38. ########################### Routes ###########################
39. [routes.rule1]
40. schema-pattern = "test"      # 匹配数据源的库名,支持通配符 "*" 和 "?"
41. table-pattern = "table-*"    # 匹配数据源的表名,支持通配符 "*" 和 "?"
42. target-schema = "test"       # 目标库名
43. target-table = "table-0"     # 目标表名

45. ######################### Task config #########################
46. [task]
47. output-dir = "./output"

49. source-instances = ["mysql1", "mysql2"]

51. target-instance = "tidb0"

53. # 需要比对的下游数据库的表,每个表需要包含数据库名和表名,两者由 `.` 隔开
54. target-check-tables = ["test.table-0"]

注意事项

如果上游数据库有 test.table-0 也会被下游数据库匹配到。