B服务器1.2
需求:要将A服务器中a库的a1,b1,c1表同步到B服务器中b库里
A服务器:
1.首先将A服务器中a-c表导出,可以通过mysqldump命令导出
B服务器:
首先,建立要用于测试的 数据库 与 表。create database a
create database b
use a
create table table1(id int, val int)
use b
create table table2(id int, val int)
然后建触发器
use a
DELIMITER //
CREATE TRIGGER tr_Insert_t1
AFTER INSERT ON table1
FOR EACH ROW
BEGIN
-- 检查当前 环境,避免递归.
IF @disable_trigger IS NULL THEN
-- 设置禁用触发器标志.
SET @disable_trigger = 1
-- 插入目标表
INSERT INTO
b.table2
VALUES
(new.id, new.val)
-- 恢复禁用触发器标志.
SET @disable_trigger = NULL
END IF
END
//
DELIMITER
use b
DELIMITER //
CREATE TRIGGER tr_Insert_t2
AFTER INSERT ON table2
FOR EACH ROW
BEGIN
-- 检查当前 环境,避免递归.
IF @disable_trigger IS NULL THEN
-- 设置禁用触发器标志.
SET @disable_trigger = 1
-- 插入目标表
INSERT INTO
a.table1
VALUES
(new.id, new.val)
-- 恢复禁用触发器标志.
SET @disable_trigger = NULL
END IF
END
//
DELIMITER
测试执行效果:
mysql>use b
Database changed
mysql>insert into table2 values(2,2)
Query OK, 1 row affected (0.01 sec)
mysql>use a
Database changed
mysql>insert into table1 values (1, 1)
Query OK, 1 row affected (0.00 sec)
mysql>select * from table1
+------+------+
| id | val |
+------+------+
|2 |2 |
|1 |1 |
+------+------+
2 rows in set (0.00 sec)
mysql>select * from b.table2
+------+------+
| id | val |
+------+------+
|2 |2 |
|1 |1 |
+------+------+
2 rows in set (0.00 sec)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)