Help Center> Distributed Database Middleware> User Guide> Data Management> Data Migration> Scenario 1: Migrating Data from HUAWEI CLOUD RDS to DDM

Scenario 1: Migrating Data from HUAWEI CLOUD RDS to DDM

Scenarios

As an enterprise user, you have purchased and started using an RDS MySQL DB instance on HUAWEI CLOUD and now expect to store data in a distributed manner.

Services may be interrupted during migration. The duration of the interruption depends on the amount of data to be migrated and on network conditions.

Migration Process

Figure 1 Migrating data from an RDS MySQL DB instance to DDM

The source RDS MySQL DB instance, destination DDM instance, and target RDS MySQL DB instance must be in the same VPC and have the same security group rules.

The migration procedure is as follows:

  1. Prepare for the migration.
  2. Export data from the source RDS MySQL DB instance to an ECS.
  3. Import the data into the destination DDM instance.
  4. Verify the consistency of data before and after migration.
  5. Verify services.

Constraints

  • The source DB instance, destination DDM instance, and target DB instance, and the required ECS must be in the same VPC and security group.
  • Before data migration, you have to stop your workloads to ensure data integrity.
  • You have to export data from an existing DB instance and import the data into DDM. Associating DDM with the DB instance to store existing data in a distributed manner is not supported.
  • Before migrating data, you have to prepare schemas, database shards, table shards, and broadcast tables with the same names and structures as the source. Importing data into new databases, table shards, or broadcast tables during migration is not supported. Methods for creating various types of logical tables are described in Table 2.
  • The target DB instance must be of the same MySQL version as the source DB instance.

Prepare for the Migration

  • Prepare an ECS that can access the source DB instance.
    1. Ensure that source and target DB instances and destination DDM instance are in the same VPC.
    2. Configure the same security group for the source and target DB instances and the destination DDM instance. If they belong to different security groups, configure security group rules to allow them to access each other.
    3. Install an official MySQL (5.6 or 5.7) client on the ECS and the following OSs by running the required commands:
      • Red Hat Linux: yum install mysql mysql-devel
      • Debian Linux: apt install mysql-client-5.7 mysql-client-core-5.7
    1. Ensure that there is enough disk space and memory on the ECS to store and compare dump files.
  • Prepare a DDM instance and create the required DDM accounts, schemas, and logical tables.
    1. Apply for a DDM instance and create a DDM account and schema on the DDM console.

      Determine the number of DDM instances based on Migration Evaluation.

    2. Export table structures of the source DB instance to a SQL text file.
      mysqldump -h {DB_ADDRESS} -P {DB_PORT} -u {DB_USER} -p --skip-add-locks --add-locks=false --no-data {DB_NAME} {TABLE_NAME} > {mysql_table_schema.sql}

      Table 1 describes the parameters in the command.

      Table 1 Parameter description

      Parameter

      Description

      Remarks

      DB_ADDRESS

      Indicates the connection address of the target database.

      This parameter is mandatory.

      DB_PORT

      Indicates the listening port of the target database.

      This parameter is mandatory.

      DB_USER

      Indicates the database account.

      This parameter is mandatory.

      --skip-lock-tables

      Indicates that data is exported when tables are not locked.

      The declaration for table locking is enabled for some parameters by default. Therefore, you are advised to add this parameter to the end of a data export statement.

      --add-locks=false

      Indicates that no locks are added to tables.

      -

      --no-data

      Indicates that only database table structures are exported.

      This parameter is used to export table structures.

      DB_NAME

      Indicates the database name.

      This parameter is mandatory.

      TABLE_NAME

      Indicates the table name.

      Separate every two table names of the same type using a space.

      Exporting only service-related table structures is recommended.

      mysql_table_schema.sql

      Indicates the name of the generated table structure file.

      The file varies depending on the table whose structure is exported.

      You can name the file in the format of schema name_logical table name_schema to prevent data from being overwritten, for example, mysql_table_schema.sql.

      The parameters listed above are commonly used for data export. It is impossible to list all mysqldump parameters. To optimize certain parameters, visit the official MySQL website or contact DDM technical support.

    3. Create a logical table.
      Ensure that structures of the logical table are consistent with those exported in 2. Map source tables to the logical table of the destination DDM instance, and specify migration policies for different table structures and data.

      Before creating a logical table, execute SHOW CREATE TABLE TABLE_NAME to query structures of tables in the source DB instance.

      Table 2 Table migration policies

      Schema Type

      Logical Table Type

      Table Structure Migration Policy

      Table Data Migration Policy

      Sharded

      Sharded

      Create logical tables on the DDM console.

      Connect to DDM and import table data.

      Sharded

      Global

      Sharded

      Unsharded

      Connect to the target DB instance and import table creation statements.

      Connect to the target DB instance and import table data.

      Unsharded

      Common

    1. Delete test data in the destination DDM instance to prevent conflicts with the data to be migrated.
  • Prepare a new RDS MySQL DB instance.

Export Data

Connect to the source DB instance using an IP address and export data from the instance using mysqldump.

  • Prerequisites
    • The destination DDM instance is in the same subnet and VPC as the required ECS.
    • Security group rules allow DDM access.
  • Procedure
    Perform the following operations to export table data from the source DB instance:
    1. Stop the service system of the source DB instance to ensure that exported data is up to date.
    2. Open your MySQL client and run the following command to connect to the destination DDM instance and export data as a SQL text file:

      mysqldump -h {DB_ADDRESS} -P {DB_PORT} -u {DB_USER} -p --single-transaction --hex-blob --complete-insert --set-gtid-purged=OFF --quick --no-create-info --skip-comments --skip-add-locks --add-locks=false [--where=""] {DB_NAME}{TABLE_NAME} > {mysq_table_data.sql}

      Table 3 Parameter description

      Parameter

      Description

      Remarks

      DB_ADDRESS

      Indicates the connection address of the target database.

      This parameter is mandatory.

      DB_PORT

      Indicates the listening port of the target database.

      This parameter is mandatory.

      DB_USER

      Indicates the database account.

      This parameter is mandatory.

      --complete-insert

      Uses a complete INSERT statement (including column names).

      -

      --single-transaction

      After this parameter is configured, a BEGIN SQL statement is submitted before data is exported. The BEGIN SQL statement does not block any application and ensures data consistency when the data is exported. It applies only to the multi-version storage engine, InnoDB.

      -

      --quick

      Directly exports data to standard output files without buffering queries.

      This avoids sharply increasing memory usage if there is massive volumes of data.

      --hex-blob

      Exports binary string fields in hexadecimal format. This parameter is mandatory if there is binary data to be exported.

      -

      --no-create-info

      Exports data without adding any CREATE TABLE statements.

      This parameter is used for data export.

      --skip-comments

      Disables additional comments.

      -

      --skip-lock-tables

      Indicates that data is exported when tables are not locked.

      The declaration for table locking is enabled for some parameters by default. Add this parameter to the end of each data export statement.

      --add-locks=false

      Indicates that no locks are added to tables.

      -

      --set-gtid-purged=OFF

      If the MySQL version is 5.6 or 5.7, configure this parameter.

      If the MySQL version is earlier than 5.6, do not configure this parameter.

      --skip-add-locks

      Controls lock operations during data export to avoid performance issues.

      -

      --where

      Dumps only the records selected based on a specified WHERE condition.

      If the condition contains command comment symbols such as special spaces or characters, put the condition in quotes.

      DB_NAME

      Indicates the database name.

      This parameter is mandatory.

      TABLE_NAME

      Indicates the table name.

      Separate every two table names of the same type using a space.

      Exporting only service-related table structures is recommended.

      mysq_table_data.sql

      Indicates the name of the generated table data file.

      The file name varies depending on the table whose data is exported.

      You can name the file in the format of schema name_logical table name_data to prevent data from being overwritten, for example, mysq_table_data.sql.

      The parameters listed above are generally used for data export. Not all mysqldump parameters are described here. Log in to the official MySQL website or contact the DDM administrator to optimize certain parameters.

    3. Check the size of the SQL text file and check whether data is successfully exported.
      • If the file size is not 0 bytes, data export is successful.
      • If the file size is 0 bytes, data export failed. Contact DDM administrator.

Import Data

  1. Enable read-only access to DDM databases on your application.
  2. Delete test data in the destination DDM instance to prevent conflicts with the data to be migrated.
  3. Proceed with subsequent operations depending on the types of tables:

    • For unsharded or common tables, use a MySQL client to connect to the target DB instance and run the following commands to import table structure and data files:
      mysql -f -h {RDS_ADDRESS} -P {RDS_PORT} -u {RDS_USER} -p {DB_NAME} < {mysql_table_schema.sql}
      Enter password: **********
      mysql -f -h {RDS_ADDRESS} -P {RDS_PORT} -u {RDS_USER} -p {DB_NAME} < {mysq_table_data.sql}
      Enter password: **********
      • RDS_ADDRESS indicates the address of the target DB instance.
      • RDS_PORT indicates the port number of the target DB instance.
      • RDS_USER indicates the username of the target DB instance.
      • DB_NAME indicates the name of the target database. If data of unsharded tables is to be imported, DB_NAME indicates the name of the first shard in the DB instance.
      • mysql_table_schema.sql indicates the name of the table structure file to be imported.
      • mysq_table_data.sql indicates the name of the table data file to be imported.

        Before importing data of unsharded tables or common tables, delete the last line (for example, Dump completed on 2018-06-28 19:53:03) in the table structure file. Otherwise, data import may fail.

    • For sharded tables or broadcast tables, use a MySQL client to connect to DDM and import data files.
      mysql -h {DDM_ADDRESS} -P {DDM_PORT} -u {DDM_USER} -p {DB_NAME} < {mysq_table_data.sql}
      Enter password: ********** 
      • DDM_ADDRESS indicates the address of the DDM instance into which data is imported.
      • DDM_PORT indicates the listening port.
      • DDM_USER indicates the DDM account.
      • DB_NAME indicates the name of the target schema.
      • mysq_table_data.sql indicates the name of the table data file to be imported.
        • Import data during off-peak hours. Performance of the destination DDM instance and target DB instance may be affected during data import.
        • If an interruption or exception occurs during data import, execute TRUNCATE TABLE {TABLE_NAME} to clear and import data again. Executing this statement will clear all table data. Exercise caution when executing it.
        • Ensure that the number of data records to be imported into a broadcast table is less than 5 million.

Verify Data

  1. Back up logical information of the destination DDM instance on the ECS.

    • Export table structures:
      mysqldump -h {DDM_ADDRESS} -P {DDM_PORT} -u {DDM_USER} -p --single-transaction --set-gtid-purged=OFF --no-data{DB_NAME} {TABLE_NAME} > {mysql_table_schema_new.sql}
    • Export table data:
      mysqldump -h {DDM_ADDRESS} -P {DDM_PORT} -u {DDM_USER} -p --single-transaction --hex-blob --complete-insert --set-gtid-purged=OFF --quick --no-create-info --skip-comments [--where=""] {DB_NAME}{TABLE_NAME} > {mysq_table_data_new.sql}

  2. Check data consistency.

    1. Check whether the number of data records in each table is the same on the source DB instance and destination DDM instance: {TABLE_NAME} indicates the table name.
      select count(*) from {TABLE_NAME};
    2. On the ECS, check whether table structures and data records are consistent before and after the export:
      diff -B -w -q -i {mysql_table_schema.sql} {mysql_table_schema_new.sql};echo $?
      diff -B -w -q -i {mysq_table_data.sql} {mysq_table_data_new.sql};echo $?
      • If yes, the data is successfully migrated.
      • If no, contact DDM technical support.

  3. Verify in an E2E manner whether your application's read-only access is normal.
  4. Disable read-only access.

Verify Services

  1. Switch the service data source to DDM.
  2. Verify whether you can read and write data from and to DDM normally.
    • If yes, data migration is completed.
    • If no, switch services to the source RDS MySQL DB instance and contact DDM technical support.