温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MySQL存储引擎--------Federated最佳实战

发布时间:2020-07-11 15:44:19 来源:网络 阅读:3339 作者:asd1123509133 栏目:数据库

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


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


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI