创建数据迁移任务

    在本地部署两个开启 binlog 的 MySQL 实例和一个 TiDB 实例;使用 DM 集群的一个 DM-master 来管理集群和数据迁移任务。各个节点的信息如下:

    下面以此为例,说明如何创建数据迁移任务。

    准备 2 个可运行的 MySQL 实例,也可以使用 Docker 快速启动 MySQL,示例命令如下:

    准备数据

    • 向 mysql-3306 写入示例数据。

      1. create database `sharding1`;
      2. use `sharding1`;
      3. create table t1 (id bigint, uid int, name varchar(80), info varchar(100), primary key (`id`), unique key(`uid`)) DEFAULT CHARSET=utf8mb4;
      4. create table t2 (id bigint, uid int, name varchar(80), info varchar(100), primary key (`id`), unique key(`uid`)) DEFAULT CHARSET=utf8mb4;
      5. insert into t1 (id, uid, name) values (1, 10001, 'Gabriel García Márquez'), (2 ,10002, 'Cien años de soledad');
      6. insert into t2 (id, uid, name) values (3,20001, 'José Arcadio Buendía'), (4,20002, 'Úrsula Iguarán'), (5,20003, 'José Arcadio');
    • 向 mysql-3307 写入示例数据。

      1. drop database if exists `sharding2`;
      2. create database `sharding2`;
      3. use `sharding2`;
      4. create table t2 (id bigint, uid int, name varchar(80), info varchar(100), primary key (`id`), unique key(`uid`)) DEFAULT CHARSET=utf8mb4;
      5. create table t3 (id bigint, uid int, name varchar(80), info varchar(100), primary key (`id`), unique key(`uid`)) DEFAULT CHARSET=utf8mb4;
      6. insert into t2 (id, uid, name, info) values (6, 40000, 'Remedios Moscote', '{}');
      7. insert into t3 (id, uid, name, info) values (7, 30001, 'Aureliano José', '{}'), (8, 30002, 'Santa Sofía de la Piedad', '{}'), (9, 30003, '17 Aurelianos', NULL);

    使用以下命令运行一个 TiDB server:

    运行数据迁移任务前,需要对 source 进行配置,也就是 MySQL 的相关设置。

    对密码进行加密

    注意:

    • 如果数据库没有设置密码,则可以跳过该步骤。
    • DM v1.0.6 及其以后版本可以使用明文密码配置 source 信息。

    为了安全,可配置及使用加密后的密码。使用 dmctl 对 MySQL/TiDB 的密码进行加密,以密码为 “123456” 为例:

    1. ./bin/dmctl encrypt "123456"
    1. fCxfQ9XKCezSzuCD0Wf5dUD+LsKegSg=

    记录该加密后的密码,用于下面新建 MySQL 数据源。

    把以下配置文件内容写入到 conf/source1.yaml 中。

    MySQL1 的配置文件:

    对于 MySQL2 数据源,将以上内容复制到文件 conf/source2.yaml 中,将 conf/source2.yaml 配置文件中的 name 修改为 mysql-replica-02,并将 passwordport 改为相应的值。

    创建 source

      对于 MySQL2,将上面命令中的配置文件替换成 MySQL2 对应的配置文件。

      在导入后,MySQL1 和 MySQL2 实例中有若干个分表,这些分表的结构相同,所在库的名称都以 “sharding” 开头,表名称都以 “t” 开头,并且主键或唯一键不存在冲突(即每张分表的主键或唯一键各不相同)。现在需要把这些分表迁移到 TiDB 中的 db_target.t_target 表中。

      首先创建任务的配置文件:

      1. ---
      2. name: test
      3. task-mode: all
      4. shard-mode: "pessimistic"
      5. target-database:
      6. host: "127.0.0.1"
      7. port: 4000
      8. user: "root"
      9. password: "" # 如果密码不为空,则推荐使用经过 dmctl 加密的密文
      10. mysql-instances:
      11. - source-id: "mysql-replica-01"
      12. block-allow-list: "instance" # 如果 DM 版本早于 v2.0.0-beta.2 则使用 black-white-list
      13. route-rules: ["sharding-route-rules-table", "sharding-route-rules-schema"]
      14. mydumper-thread: 4
      15. loader-thread: 16
      16. syncer-thread: 16
      17. - source-id: "mysql-replica-02"
      18. block-allow-list: "instance" # 如果 DM 版本早于 v2.0.0-beta.2 则使用 black-white-list
      19. route-rules: ["sharding-route-rules-table", "sharding-route-rules-schema"]
      20. mydumper-thread: 4
      21. loader-thread: 16
      22. syncer-thread: 16
      23. block-allow-list: # 如果 DM 版本早于 v2.0.0-beta.2 则使用 black-white-list
      24. instance:
      25. do-dbs: ["~^sharding[\\d]+"]
      26. do-tables:
      27. tbl-name: "~^t[\\d]+"
      28. sharding-route-rules-table:
      29. schema-pattern: sharding*
      30. table-pattern: t*
      31. target-schema: db_target
      32. target-table: t_target
      33. sharding-route-rules-schema:
      34. schema-pattern: sharding*
      35. target-schema: db_target

      将以上配置内容写入到 conf/task.yaml 文件中,使用 dmctl 创建任务:

      结果如下:

      1. {
      2. "result": true,
      3. "msg": "",
      4. "sources": [
      5. {
      6. "result": true,
      7. "msg": "",
      8. "source": "mysql-replica-01",
      9. "worker": "worker1"
      10. },
      11. {
      12. "result": true,
      13. "msg": "",
      14. "source": "mysql-replica-02",
      15. "worker": "worker2"
      16. }
      17. ]
      18. }

      这样就成功创建了一个将 MySQL1 和 MySQL2 实例中的分表数据迁移到 TiDB 的任务。

      修改上游 MySQL 分表中的数据,然后使用 校验上下游数据是否一致,如果一致则说明迁移任务运行正常。