DM 分库分表合并场景

本文介绍如何在分库分表合并场景中使用 Data Migration (DM)。使用场景中,三个上游 MySQL 实例的分库和分表数据需要同步至下游 TiDB 集群。

上游实例

假设上游库结构如下:

  • 实例 1
Schema Tables
user information, log_north, log_bak
store_01 sale_01, sale_02
store_02 sale_01, sale_02
  • 实例 2
Schema Tables
user information, log_east, log_bak
store_01 sale_01, sale_02
store_02 sale_01, sale_02
  • 实例 3
Schema Tables
user information, log_south, log_bak
store_01 sale_01, sale_02
store_02 sale_01, sale_02

同步需求

  1. 合并三个实例中的 user.information 表至下游 TiDB 中的 user.information 表。
  2. 合并三个实例中的 user.log_{north|south|east} 表至下游TiDB中的 user.log_{north|south|east} 表。
  3. 合并三个实例中的 store_{01|02}.sale_{01|02} 表至下游TiDB中的 store.sale 表。
  4. 过滤掉三个实例的 user.log_{north|south|east} 表的所有删除操作。
  5. 过滤掉三个实例的 user.information 表的所有删除操作。
  6. 过滤掉三个实例的 store_{01|02}.sale_{01|02} 表的所有删除操作。
  7. 过滤掉三个实例的 user.log_bak 表。
  8. 因为 store_{01|02}.sale_{01|02} 表带有 bigint 型的自增主键,将其合并至 TiDB 时会引发冲突。你需要有相应的方案来避免冲突。

下游实例

假设同步后下游库结构如下:

Schema Tables
user information, log_north, log_east, log_south
store sale

同步方案

  • 要满足同步需求 #1 和 #2,配置 Table routing 规则 如下:

    1. routes:
    2. ...
    3. user-route-rule:
    4. schema-pattern: "user"
    5. target-schema: "user"
  • 要满足同步需求 #3,配置 table routing 规则 如下:

    1. routes:
    2. ...
    3. store-route-rule:
    4. schema-pattern: "store_*"
    5. target-schema: "store"
    6. sale-route-rule:
    7. schema-pattern: "store_*"
    8. table-pattern: "sale_*"
    9. target-schema: "store"
    10. target-table: "sale"
  • 要满足同步需求 #4 和 #5,配置 Binlog event filter 规则 如下:

    1. filters:
    2. ...
    3. user-filter-rule:
    4. schema-pattern: "user"
    5. events: ["truncate table", "drop table", "delete", "drop database"]
    6. action: Ignore

    注意:

    同步需求 #4、#5 和 #7 的操作意味着过滤掉所有对 user 库的删除操作,所以此处配置了库级别的过滤规则。但是 user 库以后加入表的删除操作也都会被过滤。

  • 要满足同步需求 #6,配置 Binlog event filter 规则 如下:

    1. filters:
    2. ...
    3. sale-filter-rule:
    4. schema-pattern: "store_*"
    5. table-pattern: "sale_*"
    6. events: ["truncate table", "drop table", "delete"]
    7. action: Ignore
    8. store-filter-rule:
    9. schema-pattern: "store_*"
    10. events: ["drop database"]
    11. action: Ignore
  • 要满足同步需求 #7,配置 Black & white table lists 如下:

    1. black-white-list:
    2. log-bak-ignored:
    3. ignore-tables:
    4. - db-name: "user"
    5. tbl-name: "log_bak"
  • 要满足同步需求 #8,首先参考自增主键冲突处理来解决冲突,保证在同步到下游时不会因为分表中有相同的主键值而使同步出现异常;然后需要配置 ignore-checking-items 来跳过自增主键冲突的检查:

    1. ignore-checking-items: ["auto_increment_ID"]

同步任务配置

同步任务的完整配置如下。详情请参阅 Data Migration 任务配置文件

  1. name: "shard_merge"
  2. task-mode: all
  3. meta-schema: "dm_meta"
  4. remove-meta: false
  5. ignore-checking-items: ["auto_increment_ID"]
  6. target-database:
  7. host: "192.168.0.1"
  8. port: 4000
  9. user: "root"
  10. password: ""
  11. mysql-instances:
  12. -
  13. source-id: "instance-1"
  14. route-rules: ["user-route-rule", "store-route-rule", "sale-route-rule"]
  15. filter-rules: ["user-filter-rule", "store-filter-rule", "sale-filter-rule"]
  16. black-white-list: "log-bak-ignored"
  17. mydumper-config-name: "global"
  18. loader-config-name: "global"
  19. syncer-config-name: "global"
  20. -
  21. source-id: "instance-2"
  22. route-rules: ["user-route-rule", "store-route-rule", "sale-route-rule"]
  23. filter-rules: ["user-filter-rule", "store-filter-rule", "sale-filter-rule"]
  24. black-white-list: "log-bak-ignored"
  25. mydumper-config-name: "global"
  26. loader-config-name: "global"
  27. syncer-config-name: "global"
  28. -
  29. source-id: "instance-3"
  30. route-rules: ["user-route-rule", "store-route-rule", "sale-route-rule"]
  31. filter-rules: ["user-filter-rule", "store-filter-rule", "sale-filter-rule"]
  32. black-white-list: "log-bak-ignored"
  33. mydumper-config-name: "global"
  34. loader-config-name: "global"
  35. syncer-config-name: "global"
  36. # 所有实例共享的其他通用配置
  37. routes:
  38. user-route-rule:
  39. schema-pattern: "user"
  40. target-schema: "user"
  41. store-route-rule:
  42. schema-pattern: "store_*"
  43. target-schema: "store"
  44. sale-route-rule:
  45. schema-pattern: "store_*"
  46. table-pattern: "sale_*"
  47. target-schema: "store"
  48. target-table: "sale"
  49. filters:
  50. user-filter-rule:
  51. schema-pattern: "user"
  52. events: ["truncate table", "drop table", "delete", "drop database"]
  53. action: Ignore
  54. sale-filter-rule:
  55. schema-pattern: "store_*"
  56. table-pattern: "sale_*"
  57. events: ["truncate table", "drop table", "delete"]
  58. action: Ignore
  59. store-filter-rule:
  60. schema-pattern: "store_*"
  61. events: ["drop database"]
  62. action: Ignore
  63. black-white-list:
  64. log-bak-ignored:
  65. ignore-tables:
  66. - db-name: "user"
  67. tbl-name: "log_bak"
  68. mydumpers:
  69. global:
  70. threads: 4
  71. chunk-filesize: 64
  72. skip-tz-utc: true
  73. loaders:
  74. global:
  75. pool-size: 16
  76. dir: "./dumped_data"
  77. syncers:
  78. global:
  79. worker-count: 16
  80. batch: 100
  81. max-retry: 100