场景:

需要同步两个不同系统的user表的积分字段,但是系统逻辑又超级复杂,实在不能在系统程序上面统一用接口去同步了。

 

1.创建数据a和数据库b,

2.在两个库中分别插入下表

CREATE TABLE `users` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) CHARACTER SET utf8mb4 DEFAULT NULL,
  `integral` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

  

3.在数据库a中添加同步积分触发器

delimiter ||
DROP TRIGGER
IF EXISTS t_update_member||
CREATE TRIGGER t_update_member AFTER UPDATE ON a.users FOR EACH ROW
BEGIN
    IF @disable_trigger IS NULL THEN
        SET @disable_trigger = 1;
        UPDATE b.users SET
            `integral`=new.`integral`
        WHERE
            `id`= OLD.`id` ;
        SET @disable_trigger = NULL;
    END IF;
END||
delimiter ;

  

4.在数据库b中添加同步积分触发器

delimiter ||
DROP TRIGGER
IF EXISTS t_update_member||
CREATE TRIGGER t_update_member AFTER UPDATE ON b.users FOR EACH ROW
BEGIN
    IF @disable_trigger IS NULL THEN
        SET @disable_trigger = 1;
            UPDATE a.users SET
                `integral`=new.`integral`
            WHERE
                `id`= OLD.`id` ;
        SET @disable_trigger = NULL;
    END IF;
END||
delimiter ;

  

12-31 08:20