If you select a database as the object to be synchronized and the database contains a trigger that updates a table, data inconsistency may occur. This topic describes how to configure a data synchronization task for a source database that contains a trigger.
A database named triggertestdata contains the parent and child tables. The parent table contains a trigger. If a data record is inserted into the parent table, the trigger inserts the data record into the child table.
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.
In this example, the source database is a self-managed MySQL database and the destination database is an ApsaraDB RDS for MySQL instance. For more information about configuration examples, see Overview of data synchronization scenarios.
- Create a data migration task to migrate the schema of the source database to the destination database. For more information, see Migrate data from a self-managed MySQL database to an ApsaraDB RDS for MySQL instance. In this example, only Schema Migration is selected for the Migration Types parameter. Note In the Configure Migration Types and Objects step, select only Schema Migration for the Migration Types parameter, and then select databases and tables to be migrated.
- During schema migration, DTS migrates triggers to the destination database. After data migration is complete, you must log on to the destination database and execute the following statements to delete the migrated triggers.
drop trigger <Trigger name>;
drop trigger data_check;
- Create a data synchronization task to synchronize data from the source database to the destination database. For more information, see Synchronize data from a self-managed MySQL database hosted on ECS to an ApsaraDB RDS for MySQL instance. In this example, only Initial Full Data Synchronization is selected for the Initial Synchronization parameter. Note
- When you create the data synchronization task, you must select the same objects as those you selected in Step 1.
- Schema migration is complete. Therefore, you need to select only Initial Full Data Synchronization for the Initial Synchronization parameter.
Check data consistency
- Log on to the source database and insert a data record into the parent table.
insert into parent values(1,'testname');After a data record is inserted, the trigger inserts the data record 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.
The results show that the data in the source child table is consistent with the data in the destination child table.
- Query results of the source child table
- Query results of the destination child table