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. 总结


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


相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...