在實際工做中,咱們可能會遇到須要操做其餘數據庫實例的部分表,但又不想系統鏈接多庫。此時咱們就須要用到數據表映射。如同Oracle中的DBlink通常,使用過Oracle DBlink數據庫連接的人都知道能夠跨實例來進行數據查詢,一樣的,Mysql自帶的FEDERATED引擎完美的幫咱們解決了該問題。本篇文章介紹FEDERATED引擎的開啓和使用。mysql
若須要建立FEDERATED引擎表,則目標端實例要開啓FEDERATED引擎。從MySQL5.5開始FEDERATED引擎默認安裝 只是沒有啓用,進入命令行輸入show engines;
FEDERATED行狀態爲NO。sql
mysql> show engines; +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ | Engine | Support | Comment | Transactions | XA | Savepoints | +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ | PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO | | MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO | | CSV | YES | CSV storage engine | NO | NO | NO | | BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO | | MyISAM | YES | MyISAM storage engine | NO | NO | NO | | InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES | | ARCHIVE | YES | Archive storage engine | NO | NO | NO | | MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO | | FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL | +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ 9 rows in set (0.00 sec)
在配置文件[mysqld]中加入一行:federated,而後重啓數據庫,FEDERATED引擎就開啓了。數據庫
mysql> show engines; +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ | Engine | Support | Comment | Transactions | XA | Savepoints | +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ | CSV | YES | CSV storage engine | NO | NO | NO | | MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO | | MyISAM | YES | MyISAM storage engine | NO | NO | NO | | BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO | | PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO | | MEMORY | YES | Hash based, stored in memory, useful for temporary tables | 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 | YES | Federated MySQL storage engine | NO | NO | NO | +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ 9 rows in set (0.00 sec)
使用CONNECTION建立FEDERATED引擎表通用模型:服務器
CREATE TABLE (......) ENGINE =FEDERATED CONNECTION='mysql://username:password@hostname:port/database/tablename'
簡單建立測試:app
# 源端表結構及數據 mysql> show create table test_table\G *************************** 1. row *************************** Table: test_table Create Table: CREATE TABLE `test_table` ( `increment_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增主鍵', `stu_id` int(11) NOT NULL COMMENT '學號', `stu_name` varchar(20) DEFAULT NULL COMMENT '學生姓名', `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '建立時間', `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改時間', PRIMARY KEY (`increment_id`), UNIQUE KEY `uk_stu_id` (`stu_id`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COMMENT='學生表' 1 row in set (0.00 sec) mysql> select * from test_table; +--------------+--------+----------+---------------------+---------------------+ | increment_id | stu_id | stu_name | create_time | update_time | +--------------+--------+----------+---------------------+---------------------+ | 1 | 1001 | wang | 2019-06-21 10:52:03 | 2019-06-21 10:52:03 | | 2 | 1002 | dfsfd | 2019-06-21 10:52:03 | 2019-06-21 10:52:03 | | 3 | 1003 | fdgfg | 2019-06-21 10:52:03 | 2019-06-21 10:52:03 | | 4 | 1004 | sdfsdf | 2019-06-21 10:52:03 | 2019-06-21 10:52:03 | | 5 | 1005 | dsfsdg | 2019-06-21 10:52:03 | 2019-06-21 10:52:03 | | 6 | 1006 | fgd | 2019-06-21 10:52:03 | 2019-06-21 10:52:03 | +--------------+--------+----------+---------------------+---------------------+ 6 rows in set (0.00 sec) # 目標端建表及查詢 # 注意ENGINE=FEDERATED CONNECTION後爲源端地址 避免使用帶@的密碼 mysql> CREATE TABLE `test_table` ( -> `increment_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增主鍵', -> `stu_id` int(11) NOT NULL COMMENT '學號', -> `stu_name` varchar(20) DEFAULT NULL COMMENT '學生姓名', -> `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '建立時間', -> `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改時間', -> PRIMARY KEY (`increment_id`), -> UNIQUE KEY `uk_stu_id` (`stu_id`) -> ) ENGINE=FEDERATED DEFAULT CHARSET=utf8 COMMENT='學生表' CONNECTION='mysql://root:root@10.50.60.212:3306/source/test_table'; Query OK, 0 rows affected (0.01 sec) mysql> select * from test_table; +--------------+--------+----------+---------------------+---------------------+ | increment_id | stu_id | stu_name | create_time | update_time | +--------------+--------+----------+---------------------+---------------------+ | 1 | 1001 | wang | 2019-06-21 10:52:03 | 2019-06-21 10:52:03 | | 2 | 1002 | dfsfd | 2019-06-21 10:52:03 | 2019-06-21 10:52:03 | | 3 | 1003 | fdgfg | 2019-06-21 10:52:03 | 2019-06-21 10:52:03 | | 4 | 1004 | sdfsdf | 2019-06-21 10:52:03 | 2019-06-21 10:52:03 | | 5 | 1005 | dsfsdg | 2019-06-21 10:52:03 | 2019-06-21 10:52:03 | | 6 | 1006 | fgd | 2019-06-21 10:52:03 | 2019-06-21 10:52:03 | +--------------+--------+----------+---------------------+---------------------+ 6 rows in set (0.00 sec)
若是要在同一服務器上建立多個FEDERATED表,或者想簡化建立FEDERATED表的過程,則可使用該CREATE SERVER語句定義服務器鏈接參數,這樣多個表可使用同一個server。ide
CREATE SERVER建立的格式是:測試
CREATE SERVER fedlink FOREIGN DATA WRAPPER mysql OPTIONS (USER 'fed_user', PASSWORD '123456', HOST 'remote_host', PORT 3306, DATABASE 'federated');
以後建立FEDERATED表可採用以下格式:spa
CREATE TABLE (......) ENGINE =FEDERATED CONNECTION='test_link/tablename'
示例演示:命令行
# 目標端建立指向源端的server mysql> CREATE SERVER test_link -> FOREIGN DATA WRAPPER mysql -> OPTIONS (USER 'root', PASSWORD 'root',HOST '10.50.60.212',PORT 3306,DATABASE 'source'); Query OK, 1 row affected (0.00 sec) mysql> select * from mysql.servers\G *************************** 1. row *************************** Server_name: test_link Host: 10.50.60.212 Db: source Username: root Password: root Port: 3306 Socket: Wrapper: mysql Owner: 1 row in set (0.00 sec) # 目標端建立FEDERATED表 mysql> CREATE TABLE `s1` ( -> `increment_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增主鍵', -> `stu_id` int(11) NOT NULL COMMENT '學號', -> `stu_name` varchar(20) DEFAULT NULL COMMENT '學生姓名', -> `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '建立時間', -> `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改時間', -> PRIMARY KEY (`increment_id`), -> UNIQUE KEY `uk_stu_id` (`stu_id`) -> ) ENGINE=FEDERATED DEFAULT CHARSET=utf8 COMMENT='學生表' CONNECTION='test_link/s1'; Query OK, 0 rows affected (0.01 sec) mysql> CREATE TABLE `s2` ( -> `increment_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增主鍵', -> `stu_id` int(11) NOT NULL COMMENT '學號', -> `stu_name` varchar(20) DEFAULT NULL COMMENT '學生姓名', -> `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '建立時間', -> `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改時間', -> PRIMARY KEY (`increment_id`), -> UNIQUE KEY `uk_stu_id` (`stu_id`) -> ) ENGINE=FEDERATED DEFAULT CHARSET=utf8 COMMENT='學生表' CONNECTION='test_link/s2'; Query OK, 0 rows affected (0.01 sec)
基於MySQL5.7.23版本,筆者在源端及目標端實驗了多種DDL及DML,現簡單總結以下,有興趣的同窗能夠試試看。code
目前FEDERATED引擎使用範圍還很少,若確實有跨實例訪問的需求,建議作好規範,我的總結最佳實踐以下: