Synchronizing an entire database that contains a trigger may cause data inconsistency between the source and destination databases if the trigger updates a table. This topic uses an example to describe how to configure a data synchronization task in this scenario.

Background information

A database named triggertestdata contains a trigger and the following two tables: parent and child. If a data record is inserted into the parent table, the trigger inserts the data record into the child table.

Note The following table describes the definitions of the tables and trigger.
Object type Name Definition
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 `triggertestdata`.`data_check`
AFTER INSERT  ON triggertestdata.parent
 FOR EACH ROW
begin

insert into child(user_vs_id, name) values(new.user_vs_id, new.name) ;

end;

In this example, if an INSERT operation is performed on the parent table during data synchronization, the data in the source child table will be inconsistent with the data in the destination child table. To resolve this issue, you must delete the trigger in the destination database.

Procedure

This section takes data synchronization from a user-created MySQL database to ApsaraDB RDS for MySQL as an example. For more examples, see Overview of data synchronization scenarios.

  1. 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 instance. In this example, select only Schema Migration 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.
    Select only Schema Migration for the Migration Types parameter
  2. During schema migration, DTS migrates triggers to the destination database. After the data migration task is completed, log on to the destination database and run the following command to delete the migrated triggers.
    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. 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, select only Initial Full Data Synchronization 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.
    • The schema migration has been completed. Therefore, you only need to select Initial Full Data Synchronization for the Initial Synchronization parameter.
    Select only Initial Full Data Synchronization for the Initial Synchronization parameter

Check data consistency

  1. 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.
  2. 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 result of the source child tableQuery result of the source child table
    • Query result of the destination child tableQuery result of the destination child table
    The results show that the data in the source child table is consistent with the data in the destination child table.