Accessing Distributed Data with the Federated Storage Engine
http://dev.mysql.com/tech-resources/articles/mysql-federated-storage.html
Federated存储引擎可以使几台数据库逻辑上组成一个数据库,其作用相当于Oracle的数据库链接,通俗地说,即在本地建立远程的数据库表的引用。
Mysql需要5.0以上
(1)查看是否安装了federated引擎
show engines;
Engine Support Comment Transactions XA Savepoints
MEMORY YES Hash based, stored in memory, useful for temporary tables NO NO NO
FEDERATED NO Federated MySQL storage engine
MyISAM YES Default engine as of MySQL 3.23 with great performance NO NO NO
BLACKHOLE YES /dev/null storage engine (anything you write to it disappears) NO NO NO
MRG_MYISAM YES Collection of identical MyISAM tables NO NO NO
CSV YES CSV storage engine NO NO NO
ARCHIVE YES Archive storage engine NO NO NO
InnoDB DEFAULT Supports transactions, row-level locking, and foreign keys YES YES YES
从中可以看出federated引擎没有开启
windows下在my.ini中加入federated,即可开启;而在linux中,需要编译时加入选项,再在my.ini中加入federated,即可开启。
(2)建立远程数据库表链接
比如:在server1中有一个数据库db1,在server2中有db1,其中server1.db1为写,server.db1为备份;假设在两个服务器db1中分别有一个表a,在server1.db1中建立server2.db1.a的链接
create table remote_a ...... engine=federated connection = 'mysql://root:123456@server2:3306/db1/a';
(3)创建触发器
delimiter ;;
create trigger copy_a_insert after insert on a for each row begin 向remote_a插入数据 end;;
create trigger copy_a_update after update on a for each row begin 向remote_a更新数据 end;;
create trigger copy_a_delete after delete on a for each row begin 向remote_a删除数据 end;;
delimiter ;
这样在向server1.db1.a中插入数据时,调用触发器copy_a_insert,向remote_a插入数据,而remote_a是对server2.db1.a的链接,从而达到复制的目的。