当前位置: 移动技术网 > IT编程>数据库>Mysql > Mysql 开启Federated引擎的方法

Mysql 开启Federated引擎的方法

2017年12月12日  | 移动技术网IT编程  | 我要评论

小河北骂非主流,平顶山公交,萧煌奇简介

mysql中针对不同的功能需求提供了不同的存储引擎。所谓的存储引擎也就是mysql下特定接口的具体实现。

federated是其中一个专门针对远程数据库的实现。一般情况下在本地数据库中建表会在数据库目录中生成相应的表定义文件,并同时生成相应的数据文件。
但通过federated引擎创建的表只是在本地有表定义文件,数据文件则存在于远程数据库中(这一点很重要)。

通过这个引擎可以实现类似oracle 下dblink的远程数据访问功能。

使用show engines 命令查看数据库是否已支持federated引擎:

support 的值有以下几个:

yes 支持并开启
default 支持并开启, 并且为默认引擎
no 不支持
disabled 支持,但未开启

可以看出myisam为当前默认的引擎。
使用federated建表语句如下:
create table (......) engine =federated connection='mysql://[name]:[pass]@[location]:[port]/[db-name]/[table-name]'
创建成功后就可直接在本地查询相应的远程表了。

需要注意的几点:

1. 本地的表结构必须与远程的完全一样。
2.远程数据库目前仅限mysql
3.不支持事务
4.不支持表结构修改

以下是补充:

参考一下在windows下的解决办法,在my.cnf中增加一行

复制代码 代码如下:

federated

重启mysql服务后,

mysql> show engines;



federated存储引擎可以使你在本地数据库中访问远程数据库中的数据,针对federated存储引擎表的查询会被发送到远程数据库的表上执行,本地是不存储任何数据的。
简要介绍后,是不是发现它和oracle的database link(数据库链接)非常相似,它所实现的功能和db link类似,要在mysql下找寻db link替代品的,federated存储引擎是不二的选择。

1.   查看当前支持的存储引擎

sql>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         |  
| memory     | yes     | hash based, stored in memory, useful for temporary tables  | no           | no   | no         |  
| innodb     | default | supports transactions, row-level locking, and foreign keys | yes          | yes  | yes        |  
| myisam     | yes     | default engine as of mysql 3.23 with great performance     | no           | no   | no         |  
+------------+---------+------------------------------------------------------------+--------------+------+------------+  
5 rows in set (0.00 sec)

发现安装mysql时没有编译进来,只能现安装了。

2.   安装federated存储引擎

由于编译时没有选择federated,所以打算通过install plugin的方式安装,正常情况下,federated是支持动态安装的:
   === federated storage engine ===
  plugin name:      federated
  description:      connects to tables on remote mysql servers
  supports build:   static and dynamic
  configurations:   max, max-no-ndb

可是执行以下命令时报错:

sql>install plugin federated soname 'ha_federated.so';
error 1126 (hy000): can't open shared library '/usr/local/mysql/lib/mysql/plugin/ha_federated.so' (errno: 2 undefined symbol: dynstr_append_mem)
搜了一下,发现是个老问题,竟然到现在都没解决,可见mysql团队的效率和管理的混乱。http://bugs.mysql.com/bug.php?id=40942
没有办法了,只有重新编译mysql源码了, 加上--with-plugins=federated。从5.1.26开始,默认mysql不启用federated存储引擎,所以需要在my.cnf中加入federated选项或是在命令行用--federated选项启动mysqld。编译后的结果如下:

sql>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         |  
| federated  | yes     | federated mysql storage engine                                             | no           | no   | no         |  
| myisam     | yes     | default engine as of mysql 3.23 with great performance                     | no           | no   | no         |  
| innodb     | default | percona-xtradb, supports transactions, row-level locking, and foreign keys | yes          | yes  | yes        |  
| memory     | yes     | hash based, stored in memory, useful for temporary tables                  | no           | no   | no         |  
+------------+---------+----------------------------------------------------------------------------+--------------+------+------------+  
6 rows in set (0.00 sec)

至此,我们已经可以使用federated存储引擎了。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网