Migrate Data Using Data Migration

This guide shows how to migrate data using the Data Migration (DM) tool.

Step 1: Deploy the DM cluster

It is recommended to deploy the DM cluster using TiUP. You can also deploy the DM cluster using binary for trial or test.

Migrate Data Using DM - 图1

Note

  • For database passwords in all the DM configuration files, it is recommended to use the passwords encrypted by dmctl. If a database password is empty, it is unnecessary to encrypt it. See Encrypt the database password using dmctl.
  • The user of the upstream and downstream databases must have the corresponding read and write privileges.

Step 2: Check the cluster information

After the DM cluster is deployed using TiUP, the configuration information is like what is listed below.

  • The configuration information of related components in the DM cluster:

    ComponentHostPort
    dm_worker1172.16.10.728262
    dm_worker2172.16.10.738262
    dm_master172.16.10.718261
  • The information of upstream and downstream database instances:

    Database instanceHostPortUsernameEncrypted password
    Upstream MySQL-1172.16.10.813306rootVjX8cEeTX+qcvZ3bPaO4h0C80pe/1aU=
    Upstream MySQL-2172.16.10.823306rootVjX8cEeTX+qcvZ3bPaO4h0C80pe/1aU=
    Downstream TiDB172.16.10.834000root

The list of privileges needed on the MySQL host can be found in the precheck documentation.

Step 3: Create data source

  1. Write MySQL-1 related information to conf/source1.yaml:

    1. # MySQL1 Configuration.
    2. source-id: "mysql-replica-01"
    3. # This indicates that whether DM-worker uses Global Transaction Identifier (GTID) to pull binlog. Before you use this configuration item, make sure that the GTID mode is enabled in the upstream MySQL.
    4. enable-gtid: false
    5. from:
    6. host: "172.16.10.81"
    7. user: "root"
    8. password: "VjX8cEeTX+qcvZ3bPaO4h0C80pe/1aU="
    9. port: 3306
  2. Execute the following command in the terminal, and use tiup dmctl to load the MySQL-1 data source configuration to the DM cluster:

    1. tiup dmctl --master-addr 172.16.10.71:8261 operate-source create conf/source1.yaml
  3. For MySQL-2, modify the relevant information in the configuration file and execute the same dmctl command.

Step 4: Configure the data migration task

The following example assumes that you need to migrate all the test_table table data in the test_db database of both the upstream MySQL-1 and MySQL-2 instances, to the downstream test_table table in the test_db database of TiDB, in the full data plus incremental data mode.

Edit the task.yaml task configuration file as below:

  1. # The task name. You need to use a different name for each of the multiple tasks that
  2. # run simultaneously.
  3. name: "test"
  4. # The full data plus incremental data (all) migration mode.
  5. task-mode: "all"
  6. # The downstream TiDB configuration information.
  7. target-database:
  8. host: "172.16.10.83"
  9. port: 4000
  10. user: "root"
  11. password: ""
  12. # Configuration of all the upstream MySQL instances required by the current data migration task.
  13. mysql-instances:
  14. -
  15. # The ID of upstream instances or the migration group. You can refer to the configuration of `source_id` in the "inventory.ini" file or in the "dm-master.toml" file.
  16. source-id: "mysql-replica-01"
  17. # The configuration item name of the block and allow lists of the name of the
  18. # database/table to be migrated, used to quote the global block and allow
  19. # lists configuration that is set in the global block-allow-list below.
  20. block-allow-list: "global" # Use black-white-list if the DM version is earlier than or equal to v2.0.0-beta.2.
  21. # The configuration item name of the dump processing unit, used to quote the global configuration of the dump unit.
  22. mydumper-config-name: "global"
  23. -
  24. source-id: "mysql-replica-02"
  25. block-allow-list: "global" # Use black-white-list if the DM version is earlier than or equal to v2.0.0-beta.2.
  26. mydumper-config-name: "global"
  27. # The global configuration of block and allow lists. Each instance can quote it by the
  28. # configuration item name.
  29. block-allow-list: # Use black-white-list if the DM version is earlier than or equal to v2.0.0-beta.2.
  30. global:
  31. do-tables: # The allow list of upstream tables to be migrated.
  32. - db-name: "test_db" # The database name of the table to be migrated.
  33. tbl-name: "test_table" # The name of the table to be migrated.
  34. # The global configuration of the dump unit. Each instance can quote it by the configuration item name.
  35. mydumpers:
  36. global:
  37. extra-args: ""

Step 5: Start the data migration task

To detect possible errors of data migration configuration in advance, DM provides the precheck feature:

  • DM automatically checks the corresponding privileges and configuration while starting the data migration task.
  • You can also use the check-task command to manually precheck whether the upstream MySQL instance configuration satisfies the DM requirements.

For details about the precheck feature, see Precheck the upstream MySQL instance configuration.

Migrate Data Using DM - 图2

Note

Before starting the data migration task for the first time, you should have got the upstream configured. Otherwise, an error is reported while you start the task.

Run the tiup dmctl command to start the data migration tasks. task.yaml is the configuration file that is edited above.

  1. tiup dmctl --master-addr 172.16.10.71:8261 start-task ./task.yaml
  • If the above command returns the following result, it indicates the task is successfully started.

    1. {
    2. "result": true,
    3. "msg": "",
    4. "workers": [
    5. {
    6. "result": true,
    7. "worker": "172.16.10.72:8262",
    8. "msg": ""
    9. },
    10. {
    11. "result": true,
    12. "worker": "172.16.10.73:8262",
    13. "msg": ""
    14. }
    15. ]
    16. }
  • If you fail to start the data migration task, modify the configuration according to the returned prompt and then run the start-task task.yaml command to restart the task.

Step 6: Check the data migration task

If you need to check the task state or whether a certain data migration task is running in the DM cluster, run the following command in tiup dmctl:

  1. tiup dmctl --master-addr 172.16.10.71:8261 query-status

Step 7: Stop the data migration task

If you do not need to migrate data any more, run the following command in tiup dmctl to stop the task:

  1. tiup dmctl --master-addr 172.16.10.71:8261 stop-task test

test is the task name that you set in the name configuration item of the task.yaml configuration file.

Step 8: Monitor the task and check logs

Assuming that Prometheus, Alertmanager, and Grafana are successfully deployed along with the DM cluster deployment using TiUP, and the Grafana address is 172.16.10.71. To view the alert information related to DM, you can open http://172.16.10.71:9093 in a browser and enter into Alertmanager; to check monitoring metrics, go to http://172.16.10.71:3000, and choose the DM dashboard.

While the DM cluster is running, DM-master, DM-worker, and dmctl output the monitoring metrics information through logs. The log directory of each component is as follows:

  • DM-master log directory: It is specified by the --log-file DM-master process parameter. If DM is deployed using TiUP, the log directory is {log_dir} in the DM-master node.
  • DM-worker log directory: It is specified by the --log-file DM-worker process parameter. If DM is deployed using TiUP, the log directory is {log_dir} in the DM-worker node.