MySQL下的DB link

前言:html

在實際工做中,咱們可能會遇到須要操做其餘數據庫實例的部分表,但又不想系統鏈接多庫。此時咱們就須要用到數據表映射。如同Oracle中的DBlink通常,使用過Oracle DBlink數據庫連接的人都知道能夠跨實例來進行數據查詢,一樣的,Mysql自帶的FEDERATED引擎完美的幫咱們解決了該問題。本篇文章介紹FEDERATED引擎的開啓和使用。mysql

1.開啓FEDERATED引擎

若須要建立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引擎就開啓了。shell

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: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)

3.使用CREATE SERVER建立FEDERATED表

若是要在同一服務器上建立多個FEDERATED表,或者想簡化建立FEDERATED表的過程,則可使用該CREATE SERVER語句定義服務器鏈接參數,這樣多個表可使用同一個server。app

CREATE SERVER建立的格式是:ide

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引擎使用範圍還很少,若確實有跨實例訪問的需求,建議作好規範,我的總結最佳實踐以下:

  1. 源端專門建立只讀權限的用戶來供目標端使用。
  2. 目標端建議用CREATE SERVER方式建立FEDERATED表。
  3. FEDERATED表不宜太多,遷移時要特別注意。
  4. 目標端應該只作查詢使用,禁止在目標端更改FEDERATED表。
  5. 建議目標端表名及結構和源端保持一致。
  6. 源端表結構變動後 目標端要及時刪除重建。

參考:
https://dev.mysql.com/doc/refman/5.7/en/federated-create.html
https://dev.mysql.com/doc/refman/5.7/en/create-server.html

公衆號.jpg

相關文章
相關標籤/搜索