Configure tasks for source databases with triggers

更新时间:
复制 MD 格式

Synchronizing or migrating a database can cause data inconsistencies between the source and destination if the source database contains a trigger that updates tables within it. This topic describes how to configure the task in this scenario to ensure data consistency.

Background

If your source database contains triggers and your scenario meets a condition in the table below, refer to the linked documentation. Otherwise, follow the procedure in this topic.

Source database

Destination database

Description

References

All supported types

PostgreSQL, PolarDB for PostgreSQL, PolarDB for PostgreSQL (Compatible with Oracle)

  • If the destination database account has sufficient permissions (such as a privileged or superuser account), Data Transmission Service (DTS) automatically disables the trigger at the session level after migration or synchronization.

  • If the destination database account has insufficient permissions, set the session_replication_role parameter to replica in the destination database while the task is running.

MySQL, PolarDB for MySQL, ApsaraDB RDS for MariaDB

MySQL, PolarDB for MySQL, ApsaraDB RDS for MariaDB

Triggers must be manually configured for synchronization or migration.

Synchronize or migrate triggers from the source database

SQL Server

SQL Server

Workflow

  1. Create a data migration task to migrate the schema from the source database to the destination database.

    For more information, see Overview of data migration scenarios.

    Important
    • For Migration Types, select only Schema Migration.

    • For Source Objects, select the entire database or schema.

  2. Log on to the destination database and delete the triggers that were migrated from the source database.

  3. Create a data synchronization or data migration task from the source database to the destination database.

    For more information, see Overview of data synchronization scenarios and Overview of data migration scenarios.

    Important
    • For a data synchronization task, Synchronization Types is selected by default under Incremental Data Synchronization. You must also select Full Data Synchronization. Do not select Schema Synchronization.

    • For a data migration task, select Migration Types and Full Data Migration for Incremental Data Migration. Do not select Schema Migration.

  4. (Optional) After the data transfer is complete, terminate or release the instance.

    For more information, see Terminate a DTS instance and Release DTS instances.

    Note

    After the instance is terminated or released, manually add the triggers to the destination database based on your business requirements.

Example

Note

This example demonstrates how to configure a task to synchronize data from a self-managed MySQL database to an ApsaraDB RDS for MySQL instance.

Data preparation

This example uses a MySQL database named triggertestdata that contains two tables: parent and child. A trigger on the parent table inserts a copy of each new row into the child table.

Note

The following table shows the table schemas and trigger definition.

Object type

Name

Statement

Table

parent

CREATE TABLE `parent` (
  `user_vs_id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(30) DEFAULT NULL,
  PRIMARY KEY (`user_vs_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2001 DEFAULT CHARSET=utf8

Table

child

CREATE TABLE `child` (
  `sys_child_id` int(11) NOT NULL AUTO_INCREMENT,
  `user_vs_id` int(11) DEFAULT NULL,
  `name` varchar(30) DEFAULT NULL,
  PRIMARY KEY (`sys_child_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2001 DEFAULT CHARSET=utf8

trigger

data_check

CREATE TRIGGER data_check
AFTER INSERT ON parent
FOR EACH ROW
insert into child(user_vs_id, name) values(new.user_vs_id, new.name) ;

In this scenario, an INSERT operation on the parent table during synchronization would cause the child table data to become inconsistent between the source and destination. To prevent this, you must delete the trigger from the destination database after the initial schema migration.

Procedure

  1. Create a data migration task to migrate the schema from the source database to the destination database.

    1. Navigate to the migration task list page for the destination region using one of the following methods.

      From the DTS console

      1. Log on to the Data Transmission Service (DTS) console.

      2. In the navigation pane on the left, click Data Migration.

      3. In the upper-left corner of the page, select the region where the migration instance is located.

      From the DMS console

      Note

      The actual operations may vary based on the mode and layout of the DMS console. For more information, see Simple mode console and Customize the layout and style of the DMS console.

      1. Log on to the Data Management (DMS) console.

      2. In the top menu bar, choose Data + AI > Data Transmission (DTS) > Data Migration.

      3. To the right of Data Migration Tasks, select the region where the migration instance is located.

    2. Click Create Task to navigate to the task configuration page.

    3. Configure the Source Database and Destination Database settings, and then click Test Connectivity and Proceed at the bottom of the page.

      For parameter descriptions and more information, see Migrate data from a self-managed MySQL database to an ApsaraDB RDS for MySQL instance.

    4. For Migration Types, select Schema Migration, and then select the database or schema to migrate.

    5. Complete the remaining configuration steps based on your business requirements.

  2. Schema migration also migrates the triggers. After the task completes, log on to the destination database and delete these triggers by running the following command:

    drop trigger <trigger_name>;

    Example:

    drop trigger data_check;
  3. Create a data synchronization task to synchronize data from the source database to the destination database.

    1. In the left-side navigation pane, click Data Synchronization.

    2. Configure the Source Database and Destination Database settings, and then click Test Connectivity and Proceed at the bottom of the page.

      For parameter descriptions and more information, see Synchronize data from a self-managed MySQL database to an ApsaraDB RDS for MySQL instance.

      Note

      Use the same source and destination databases that you configured in Step 1.

    3. For Synchronization Types, manually select Full Data Synchronization.

      Note
      • Ensure the synchronization objects are the same as the migration objects you configured in Step 1.

      • Synchronization Types is selected by default under Incremental Data Synchronization. Do not select Schema Synchronization.

    4. Complete the remaining configuration steps based on your business requirements.

Test data consistency

  1. Log on to the source database and insert a row into the parent table.

    insert into parent values(1,'testname');

    The trigger in the source database then inserts the same row into the source child table.

  2. Log on to the source and destination databases. Query the child table in both databases to verify data consistency.

    • Query results in the source database

      +--------------+------------+----------+
      | sys_child_id | user_vs_id | name     |
      +--------------+------------+----------+
      |         2001 |          1 | testname |
      +--------------+------------+----------+
    • Query results in the destination database

      +--------------+------------+----------+
      | sys_child_id | user_vs_id | name     |
      +--------------+------------+----------+
      |         2001 |          1 | testname |
      +--------------+------------+----------+

    The results show that the data is consistent.

Next steps

  1. After data synchronization is complete, terminate the synchronization task that you created in Step 3.

    For more information, see Terminate a DTS instance.

  2. (Optional) View the statement for creating the trigger.

    1. Find the migration task that you created in Step 1.

    2. Click the ID of the task.

    3. On the Task Management page, click Schema Migration2.

    4. On the Task Details tab, click View Statements.

  3. Log on to the destination database and manually add the trigger.