Mysql Federated Server 示例
Federated存储引擎访问在远程数据库的表中的数据,而不是本地的表。创建一个Federated表的时候,服务器在数据库目录创建一个表定义文件。无其它表被创建,因为实际的数据在一个远程数据库上。这不同于为本地表工作的存储引擎的方式。
1. 检查当前服务器配置是否支持Federated存储引擎:
Show Engines;
查询结果,如果Federated行对应的Support列值为YES,说明当前服务器支持Federated存储引擎。
如果Federated行对应的Support列值为NO,需要我们进行配置。找到服务器安装目录下my.ini文件;记事本打开文件添加Federated;保存;重启MySQL服务器。
2. 在远程服务器上,创建本地表;并导入数据。
CREATE TABLE IF NOT EXISTS `departmentweekdata` ( `department` varchar(255) NOT NULL, `week` varchar(10) NOT NULL, `interval` tinyint(4) DEFAULT NULL, `number` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; -- -- 插入表数据 -- INSERT INTO `departmentweekdata` (`department`, `week`, `interval`, `number`) VALUES ('CAO', '2014-12', 1, 1), ('GBG1', '2014-12', 1, 0), ('CAO', '2014-12', 2, 0), ('GBG1', '2014-12', 2, 0), ('CAO', '2014-12', 3, 1), ('GBG1', '2014-12', 3, 0), ('CAO', '2014-11', 1, 2), ('GBG1', '2014-11', 1, 0), ('CAO', '2014-11', 2, 0), ('GBG1', '2014-11', 2, 0), ('CAO', '2014-11', 3, 1), ('GBG1', '2014-11', 3, 0), ('CAO', '2014-10', 1, 1), ('GBG1', '2014-10', 1, 0), ('CAO', '2014-10', 2, 2), ('GBG1', '2014-10', 2, 0), ('CAO', '2014-10', 3, 0), ('GBG1', '2014-10', 3, 0), ('CAO', '2014-09', 1, 1), ('GBG1', '2014-09', 1, 0), ('CAO', '2014-09', 2, 0), ('GBG1', '2014-09', 2, 0), ('CAO', '2014-09', 3, 0), ('GBG1', '2014-09', 3, 0), ('CAO', '2014-08', 1, 2), ('GBG1', '2014-08', 1, 0), ('CAO', '2014-08', 2, 0), ('GBG1', '2014-08', 2, 0), ('CAO', '2014-08', 3, 1), ('GBG1', '2014-08', 3, 0), ('CAO', '2014-07', 1, 1), ('GBG1', '2014-07', 1, 0), ('CAO', '2014-07', 2, 2), ('GBG1', '2014-07', 2, 0), ('CAO', '2014-07', 3, 0), ('GBG1', '2014-07', 3, 0), ('CAO', '2014-06', 1, 0), ('GBG1', '2014-06', 1, 0), ('CAO', '2014-06', 2, 1), ('GBG1', '2014-06', 2, 0), ('CAO', '2014-06', 3, 0), ('GBG1', '2014-06', 3, 0), ('CAO', '2014-05', 1, 0), ('GBG1', '2014-05', 1, 0), ('CAO', '2014-05', 2, 1), ('GBG1', '2014-05', 2, 0), ('CAO', '2014-05', 3, 0), ('GBG1', '2014-05', 3, 0), ('CAO', '2014-04', 1, 1), ('GBG1', '2014-04', 1, 0), ('CAO', '2014-04', 2, 0), ('GBG1', '2014-04', 2, 0), ('CAO', '2014-04', 3, 0), ('GBG1', '2014-04', 3, 0), ('CAO', '2014-03', 1, 0), ('GBG1', '2014-03', 1, 0), ('CAO', '2014-03', 2, 0), ('GBG1', '2014-03', 2, 0), ('CAO', '2014-03', 3, 1), ('GBG1', '2014-03', 3, 0);
3. 在本地服务器上创建Federated表。
CREATE TABLE `DepartmentWeekData` ( `department` VARCHAR(255) NOT NULL, `week` VARCHAR(10) NOT NULL, `interval` TINYINT(4) NULL, `number` INT NULL ) ENGINE=FEDERATED CONNECTION='mysql://USER:PASSWORD@HOST:PORT/DATABASE/TABLE';
其中连接字符串示例:CONNECTION='mysql://root:123@127.0.0.1:3306/dbTest/TestTable';
4. 如果使用MySQL客户端工具,可以创建Federated Server:
CREATE SERVER `FServer` FOREIGN DATA WRAPPER mysql OPTIONS ( HOST '127.0.0.1', PORT 3306, USER 'root', PASSWORD '123', DATABASE 'DBTest' );
5. 如果创建了Federated Server,创建Federated表可以用下面的方式:
CREATE TABLE `DepartmentWeekData` ( `department` VARCHAR(255) NOT NULL, `week` VARCHAR(10) NOT NULL, `interval` TINYINT(4) NULL, `number` INT NULL ) ENGINE=FEDERATED CONNECTION='FServer/TABLE';
6.创建好Federated表以后,就可以访问远程数据库表中的数据了。