前言: 在實際工作中,我們可能會遇到需要操作其他資料庫實例的部分表,但又不想系統連接多庫。此時我們就需要用到數據表映射。如同Oracle中的DBlink一般,使用過Oracle DBlink資料庫鏈接的人都知道可以跨實例來進行數據查詢,同樣的,Mysql自帶的FEDERATED引擎完美的幫我們解決了 ...
前言:
在實際工作中,我們可能會遇到需要操作其他資料庫實例的部分表,但又不想系統連接多庫。此時我們就需要用到數據表映射。如同Oracle中的DBlink一般,使用過Oracle DBlink資料庫鏈接的人都知道可以跨實例來進行數據查詢,同樣的,Mysql自帶的FEDERATED引擎完美的幫我們解決了該問題。本篇文章介紹FEDERATED引擎的開啟和使用。
1.開啟FEDERATED引擎
若需要創建FEDERATED引擎表,則目標端實例要開啟FEDERATED引擎。從MySQL5.5開始FEDERATED引擎預設安裝 只是沒有啟用,進入命令行輸入show engines;
FEDERATED行狀態為NO。
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)
2.使用CONNECTION創建FEDERATED表
使用CONNECTION創建FEDERATED引擎表通用模型:
CREATE TABLE (......)
ENGINE =FEDERATED CONNECTION='mysql://username:password@hostname:port/database/tablename'
簡單創建測試:
# 源端表結構及數據
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:[email protected]: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)
3.使用CREATE SERVER創建FEDERATED表
如果要在同一伺服器上創建多個FEDERATED表,或者想簡化創建FEDERATED表的過程,則可以使用該CREATE SERVER語句定義伺服器連接參數,這樣多個表可以使用同一個server。
CREATE SERVER創建的格式是:
CREATE SERVER fedlink
FOREIGN DATA WRAPPER mysql
OPTIONS (USER 'fed_user', PASSWORD '123456', HOST 'remote_host', PORT 3306, DATABASE 'federated');
之後創建FEDERATED表可採用如下格式:
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)
4.FEDERATED使用總結
基於MySQL5.7.23版本,筆者在源端及目標端實驗了多種DDL及DML,現簡單總結如下,有興趣的同學可以試試看。
- 目標端建表結構可以與源端不一樣 推薦與源端結構一致
- 源端DDL語句更改表結構 目標端不會變化
- 源端DML語句目標端查詢會同步
- 源端drop表 目標端結構還在但無法查詢
- 目標端不能執行DDL語句
- 目標端執行DML語句 源端數據也會變化
- 目標端truncate表 源端表數據也會被清空
- 目標端drop表對源端無影響
5.FEDERATED引擎最佳實踐
目前FEDERATED引擎使用範圍還不多,若確實有跨實例訪問的需求,建議做好規範,個人總結最佳實踐如下:
- 源端專門創建只讀許可權的用戶來供目標端使用。
- 目標端建議用CREATE SERVER方式創建FEDERATED表。
- FEDERATED表不宜太多,遷移時要特別註意。
- 目標端應該只做查詢使用,禁止在目標端更改FEDERATED表。
- 建議目標端表名及結構和源端保持一致。
- 源端表結構變更後 目標端要及時刪除重建。
參考:
https://dev.mysql.com/doc/refman/5.7/en/federated-create.html
https://dev.mysql.com/doc/refman/5.7/en/create-server.html