If you specify an entire database as the object to be synchronized and the database contains a trigger that updates a table, data inconsistency may occur between the source and destination databases. This topic describes how to configure a data synchronization task for a source database that contains a trigger. This method maintains data consistency and integrity between the source and destination databases.
Background information
If the source database of the synchronization or migration task contains a trigger and meets the following conditions, synchronize or migrate the trigger based on the references to ensure data consistency between the source and destination databases. This topic describes how to configure a data synchronization or migration task for other databases that contain triggers.
Source database | Destination database | Description | References |
Unlimited | PostgreSQL, PolarDB for PostgreSQL or PolarDB for PostgreSQL (Compatible with Oracle) |
| |
MySQL, PolarDB for MySQL, or ApsaraDB RDS for MariaDB | MySQL, PolarDB for MySQL, or ApsaraDB RDS for MariaDB | You can manually configure a trigger synchronization or migration task. | |
SQL Server | SQL Server |
Synchronization or migration process
Create a migration task to migrate data from the source database to the destination database.
For more information, see Overview of data migration scenarios.
ImportantSelect only Schema Migration for the Migration Types parameter.
Set the Source Objects parameter to a database or a schema.
Log on to the destination database and delete the trigger that is migrated from the source database.
Creates a synchronization or migration task to synchronize or migrate data from the source database to the destination database.
For more information, see Overview of data synchronization scenarios and Overview of data migration scenarios.
ImportantData synchronization: By default, Incremental Data Synchronization is selected for the Synchronization Types parameter. You also need to select Full Data Synchronization. Do not select Schema Synchronization.
Data migration: Select Full Data Migration and Incremental Data Migration for the Migration Types parameter. Do not select Schema Migration.
Optional. Terminate or release the task after data synchronization or migration is complete.
For more information, see Terminate a DTS instance and Release DTS instances.
NoteAfter the task is terminated or released, you can manually add triggers to the destination database based on your business requirements.
Example
The following example shows how to migrate data from a self-managed MySQL database to an ApsaraDB RDS for MySQL instance.
Data preparation
Two tables (parent and child) are created in the MySQL database named triggertestdata. The parent table contains a trigger. If a data entry is inserted into the parent table, the trigger inserts the data entry into the child table.
The following table describes the statements of the tables and trigger.
Object type | Name | Statement |
Table | parent |
|
Table | child |
|
Trigger | data_check |
|
In this case, if an INSERT operation is performed on the parent table during data synchronization, the data in the source child table is inconsistent with the data in the destination child table. To solve this issue, you must delete the trigger in the destination database.
Procedure
Create a data migration task to migrate the schema of the source database to the destination database.
Go to the Data Migration page.
Log on to the Data Management (DMS) console.
In the top navigation bar, move the pointer over DTS.
Choose .
NoteThe actual operations may vary based on the mode and layout of the DMS console. For more information, see Simple mode and Customize the layout and style of the DMS console.
You can also go to the Data Migration page of the new DTS console.
From the drop-down list on the right side of Data Migration Tasks, select the region in which your data migration instance resides.
NoteIf you use the new DTS console, you must select the region in which the data migration instance resides in the upper-left corner.
Click Create Task to go to the task configuration page.
Configure the parameters in the Source Database and Destination Database sections, and click Test Connectivity and Proceed.
For more information, see Migrate data from a self-managed MySQL database to an ApsaraDB RDS for MySQL instance.
Set the Migration Types parameter to Schema Migration, and select a database or a schema.
Configure the other parameters based on your business requirements.
During schema migration, DTS also migrates triggers to the destination database. After the migration task completes, you need to log on to the destination database and run the following command to delete the migrated triggers:
drop trigger <Trigger name>;
Sample command:
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 parameters in the Source Database and Destination Database sections, and click Test Connectivity and Proceed.
For more information, see Synchronize data from a self-managed MySQL database to an ApsaraDB RDS for MySQL instance.
NoteThe configurations of the source and destination databases must be the same as those in Step 1.
Select Full Data Synchronization for the Synchronization Types parameter.
NoteThe object to be synchronized must be the same as the object migrated in Step 1.
By default, Incremental Data Synchronization is selected for the Synchronization Types parameter. Do not select Schema Synchronization.
Configure the other parameters based on your business requirements.
Test data consistency
Log on to the source database and insert a data entry into the parent table.
insert into parent values(1,'testname');
After a data entry is inserted, the trigger inserts the data entry into the source child table.
Log on to the source and destination databases. Query the data of the source child table and the destination child table. Check whether the data is consistent between the two tables.
Query results of the source child table
+--------------+------------+----------+ | sys_child_id | user_vs_id | name | +--------------+------------+----------+ | 2001 | 1 | testname | +--------------+------------+----------+
Query results of the destination child table
+--------------+------------+----------+ | sys_child_id | user_vs_id | name | +--------------+------------+----------+ | 2001 | 1 | testname | +--------------+------------+----------+
The results show that the data in the source child table is consistent with the data in the destination child table.
What to do next
After data synchronization is complete, terminate the data synchronization task created in Step 3.
For more information, see Terminate a DTS instance.
Optional. View the statements for creating the trigger.
Find the data migration task created in Step 1.
Click the ID of the task that you want to manage.
On the Task Management page, click Schema Migration2.
On the Task Details tab, click View Statements.
Log on to the destination database and add a trigger.