MySQL存储引擎--------Federated最佳实战
admin
2023-05-22 14:22:33
0

1. 背景

   * 本地MySQL数据库要访问远程MySQL数据库的表中的数据, 必须通过FEDERATED存储引擎来实现. 

  * 有点类似Oracle中的数据库链接(DBLINK). 要允许这个存储引擎, 当构建MySQL时使用--with-federated-storage-engine来configure.  

   * 当创建一个FEDERATED表的时候, 服务器在数据库目录创建一个表定义文件. 文件由表的名字开始, 并有一个.frm扩展名. 

   * 无其它文件被创建, 因为实际的数据在一个远程数据库上. 


2. 相关特性

  * 允许本地访问远程 MySQL 数据库中表的数据

   * 本地不存储任何数据文件

   * 仅支持 MySQL 对 MySQL 的访问

   * 不支持异构数据库的访问

   * MySQL默认不开启Federated存储引擎


3. 环境

     两个MySQL 5.7实例

   serverA 3306

     serverB    3307

[root@MySQL ~]# mysql -S /tmp/mysql.sock1 -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.18 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.18    |
+-----------+
1 row in set (0.00 sec)

[root@MySQL ~]# mysql -S /tmp/mysql.sock2 -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.18 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.18    |
+-----------+
1 row in set (0.00 sec)

   

4. Federated存储引擎设置

   * 查看Federated是否开启 [ FEDERATED 中Support状态NO表明引擎未开启 ]

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         |
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
| 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.01 sec)


   * 配置文件指定开启Federated存储引擎 [ /etc/my.cnf ]

[mysqld]
federated


   * 重启MySQL 再次查看 [ FEDERATED 中Support状态成YES表明引擎开启成功 ]

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         |
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
| FEDERATED          | YES     | Federated MySQL storage engine                                 | NO           | NO   | NO         |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)


5. 部署 

   * 在ServerA上有一个数据库dbtestA,在ServerB上有数据库dbtestB,要在ServerB的数据库dbtestB上建立ServerA的数据库dbtestA上的表tabletestA的数据表链接remote_tabletestA,通过普通用户test连接。


   * ServerA创建数据库

mysql> create database dbtestA;
Query OK, 1 row affected (0.02 sec)


   * ServerA在dbtestA数据库中创建tabletestA表

mysql> create table tabletestA(
id INT PRIMARY KEY NOT NULL AUTO_INCREMENT
)ENGINE=INNODB;
Query OK, 0 rows affected (0.11 sec)


   * ServerA中创建普通用户并授权dbtestA数据库相关权限

mysql> create user 'test'@'127.0.0.1' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> grant select on dbtestA.* to 'test'@'127.0.0.1';
Query OK, 0 rows affected (0.00 sec)


   * ServerB 中创建数据库 dbtestB

mysql> create database dbtestB;
Query OK, 1 row affected (0.00 sec)


   * ServerB 在dbtestB中创建链接表remote_tabletestA, 使用普通用户test

mysql> use dbtestB;
Database changed
mysql> create table remote_tabletestA(
    -> id INT PRIMARY KEY NOT NULL AUTO_INCREMENT
    -> )ENGINE=FEDERATED
    -> CONNECTION='mysql://test:123456@127.0.0.1:3306/dbtestA/tabletestA';
Query OK, 0 rows affected (0.09 sec)


   * 在ServerA dbtestA库tableA表中插入数据

Database changed
mysql> use dbtestA;
Database changed
mysql> insert into tabletestA select NULL;
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into tabletestA select NULL;
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into tabletestA select NULL;
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from tabletestA;
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
+----+
3 rows in set (0.01 sec)


   * ServerB dbtestB库链接表remote_tabletestA查看

mysql> use dbtestB;
Database changed
mysql> select * from remote_tabletestA;
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
+----+
3 rows in set (0.01 sec)


   * ServerB服务器中查看链接表remote_tablestestA相关文件

   .frm 表定义文件 [ Federated链接库本地不产生数据文件 ]

[root@MySQL ~]# ll /data/mysql_data2/dbtestB/
total 16
-rw-r----- 1 mysql mysql   65 Jun 25 10:40 db.opt
-rw-r----- 1 mysql mysql 8556 Jun 25 10:42 remote_tabletestA.frm


6. 总结


以需求驱动技术,技术本身没有优略之分,只有业务之分。


相关内容

热门资讯

美军被曝群发邮件“众筹”对付伊... 据凤凰卫视援引美国有线电视新闻网8月3日报道,美军中央司令部近期在内部广泛征集新的对伊朗施压与惩戒策...
因凡蒂诺求助特朗普保其职位?国... 国际足联媒体部门8月4日在社交媒体平台X回应英国《泰晤士报》报道称,国际足联主席因凡蒂诺近日未同美国...
江西德兴一自来水主管道破裂,城... 8月4日,江西省德兴市一处自来水主管道因施工破裂,现场喷涌出十余米高自来水,目前正在积极抢修。受此影...
制胜|第六集《联战联胜》大图速... 思想解读类融媒体片《制胜》今日收官第六集《联战联胜》震撼上新2016年2月1日习主席向五大战区授予军...
我国经济动能向新结构向优(读数... 四川省达州市通川区北山镇丰登村的麦田间,农民将刚收割的小麦装车。邓良奎摄(人民视觉)数据来源:国家统...
“意大利、德国还好意思指责西班... 一边高调批评西班牙的移民大赦政策“吸引非法移民”,一边却悄悄为本国非法入境者大开绿灯——意大利、德国...
电脑如何与打印机连接 打印机的种类很多,按打印元件对纸是否有击打动作,分击打式打印机与非击打式打印机。那么电脑如何与打印机...
怎么在电脑上添加打印机步骤 在现代社会,打印机是一个非常重要的办公设备,它能够将电子文件转化为实体纸质文件。然而,许多人在次使用...
电脑如何添加打印机驱动程序 电脑在跟打印机一起使用的`时候,我们要在电脑上安装好相应的驱动程序才行。那么电脑如何添加打印机驱动程...
韦唯回忆10年跨国婚姻:完全就... 近日,韦唯直言嫁给瑞典钢琴家完全就是上当,没爱过他。结婚前不知道对方比自己大25岁,表面光鲜的10年...