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) |
|
|
|
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. |
|
|
SQL Server |
SQL Server |
Workflow
-
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.
-
-
Log on to the destination database and delete the triggers that were migrated from the source database.
-
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.
-
-
(Optional) After the data transfer is complete, terminate or release the instance.
For more information, see Terminate a DTS instance and Release DTS instances.
NoteAfter the instance is terminated or released, manually add the triggers to the destination database based on your business requirements.
Example
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.
The following table shows the table schemas and trigger definition.
|
Object type |
Name |
Statement |
|
Table |
parent |
|
|
Table |
child |
|
|
trigger |
data_check |
|
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
-
Create a data migration task to migrate the schema from the source database to the destination database.
-
Navigate to the migration task list page for the destination region using one of the following methods.
From the DTS console
-
Log on to the Data Transmission Service (DTS) console.
-
In the navigation pane on the left, click Data Migration.
-
In the upper-left corner of the page, select the region where the migration instance is located.
From the DMS console
NoteThe 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.
-
Log on to the Data Management (DMS) console.
-
In the top menu bar, choose .
-
To the right of Data Migration Tasks, select the region where the migration instance is located.
-
-
Click Create Task to navigate to the task configuration page.
-
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.
-
For Migration Types, select Schema Migration, and then select the database or schema to migrate.
-
Complete the remaining configuration steps based on your business requirements.
-
-
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; -
Create a data synchronization task to synchronize data from the source database to the destination database.
-
In the left-side navigation pane, click Data Synchronization.
-
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.
NoteUse the same source and destination databases that you configured in Step 1.
-
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.
-
-
Complete the remaining configuration steps based on your business requirements.
-
Test data consistency
-
Log on to the source database and insert a row into the
parenttable.insert into parent values(1,'testname');The trigger in the source database then inserts the same row into the source child table.
-
Log on to the source and destination databases. Query the
childtable 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
-
After data synchronization is complete, terminate the synchronization task that you created in Step 3.
For more information, see Terminate a DTS instance.
-
(Optional) View the statement for creating the trigger.
-
Find the migration task that you created in Step 1.
-
Click the ID of the task.
-
On the Task Management page, click Schema Migration2.
-
On the Task Details tab, click View Statements.
-
-
Log on to the destination database and manually add the trigger.