本帖最后由 温柔的笑猫 于 2016-3-3 12:15 编辑
要求:我们要在IP1(192.168.1.117)上的DB1数据库上访问IP2(192.168.1.118)上DB2数据库内的tb2表
方法:
这里DB1(192.168.1.117)作为本地数据库需要开启federated引擎才可以访问远程的DB2(192.168.1.118)内的表tb2,具体开启方法如下:
本地IP1数据库DB1开启FEDERATED引擎:
1.查看是否开启Federated:
MYSQL命令行,查看FEDERATED引擎是否开启,默认是不开启;
mysql>show engines;
(可以在上图中看出本地数据库没有开启federated引擎)
我们可先尝试以安装mysql的插件(plugin)模式来启动federated存储引擎的支持,这样就无需重新编译。
mysql> show engines;
mysql> show plugins;
(注:Debian/Ubuntu安装mysql-server默认已经启用federated引擎,但RedHat/CentOS则无。
新版Centos6在其RPM的src资源中称已经“Fix broken "federated" storage engine plugin”。)
检查MySQL的插件目录:
# /bin/ls -l /usr/lib/mysql/plugin/
# /bin/ls -l /usr/lib/mysql/plugin/ha_federated.so
mysql> help plugins
mysql> help install plugin
显示如下即可:
--------------------------------------------------------------------------------
Name: 'INSTALL PLUGIN'
Description:
Syntax:
INSTALL PLUGIN plugin_name SONAME 'shared_library_name'
--------------------------------------------------------------------------------
然后,可以用插件模式在您的mysqld中安装federated支持:
mysql> install plugin federated soname 'ha_federated.so';
插件安装好之后,还不能立即使用,因为MySQL服务器还没有开启支持选项:
mysql> show plugins;
+----------------+-------------+-----------------------+--------------------+---------+
| Name | Status | Type | Library | License |
+-------------- -+-------------+-----------------------+--------------------+----------+
| FEDERATED | DISABLED | STORAGE ENGINE | ha_federated.so | GPL |
+----------------+-------------+-----------------------+--------------------+----------+
mysql> show engines;
+---------------+---------+-----------------------------------------+--------------+--------+
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL |
+---------------+---------+-----------------------------------------+--------------+--------+
2.如何开启:
配置my.cnf
在[mysqld]下,添加
feterated
3.停止mysqld服务进程,并测试带federated选项启动mysqld服务进程:
# service mysqld stop;
# /usr/bin/mysqld_safe --federated &
正常执行完这一步,再启动mysql:
# service mysqld start
就能看到如下所示:
+---------------+---------+-----------------------------------------+--------------+--------+
| FEDERATED | YES | Federated MySQL storage engine | NULL | NULL |
+---------------+---------+-----------------------------------------+--------------+--------+
但是通常会出现如下错误:
[1] 3678
[root@ml2 ~]# 160302 12:02:00 mysqld_safe Logging to '/var/log/mysqld.log'.
160302 12:02:00 mysqld_safe Starting mysqld daemon with databases from /data/mysql
160302 12:02:00 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
解决方法:
cd /var/run/
#ls
mysqld目录不存在,创建它:
#mkdir /var/run/mysqld
#cd /var/run/mysqld
创建文件mysqld.pid:
#touch mysqld.pid
#cd..
#chown -R mysql mysqld . <---(有那个点【.】{:4_99:})
# cd /usr/local/mysql/
# bin/mysqld_safe --user=mysql &
nohup: ignoring input and redirecting stderr to stdout
Starting mysqld daemon with databases from /var/lib/mysql
能正常启动
又出现错误:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
分析:
是/tmp/mysql.sock 不存在
# cd /var/lib/mysql/
由于mysql 默认的mysql.sock 是在/var/lib/mysql/mysql.sock,
创建软连接:
# ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
以下是关于,配置好并开启federated后,如何使用federated引擎实现远程访问数据库:
1.登陆数据库(192.168.1.118)用户并授权:
mysql> grant select on discuz.* to 'test'@'192.168.1.117' identified by '123456';
2.在DB2里面找到建立表tb2的语句:
mysql> show create table DB2.tb2
显示如下:将建表语句修改成如下,复制粘贴:在本地DB1数据库就可以看到远程DB2中表tb2里面的内容了。
参考资料:
http://www.linuxidc.com/Linux/2012-09/69965p2.htm
http://www.educity.cn/wenda/400366.html
http://www.2cto.com/database/201412/358397.html
要求:我们要在IP1(192.168.1.117)上的DB1数据库上访问IP2(192.168.1.118)上DB2数据库内的tb2表
方法:
这里DB1(192.168.1.117)作为本地数据库需要开启federated引擎才可以访问远程的DB2(192.168.1.118)内的表tb2,具体开启方法如下:
本地IP1数据库DB1开启FEDERATED引擎:
1.查看是否开启Federated:
MYSQL命令行,查看FEDERATED引擎是否开启,默认是不开启;
mysql>show engines;
(可以在上图中看出本地数据库没有开启federated引擎)
我们可先尝试以安装mysql的插件(plugin)模式来启动federated存储引擎的支持,这样就无需重新编译。
mysql> show engines;
mysql> show plugins;
(注:Debian/Ubuntu安装mysql-server默认已经启用federated引擎,但RedHat/CentOS则无。
新版Centos6在其RPM的src资源中称已经“Fix broken "federated" storage engine plugin”。)
检查MySQL的插件目录:
# /bin/ls -l /usr/lib/mysql/plugin/
# /bin/ls -l /usr/lib/mysql/plugin/ha_federated.so
mysql> help plugins
mysql> help install plugin
显示如下即可:
--------------------------------------------------------------------------------
Name: 'INSTALL PLUGIN'
Description:
Syntax:
INSTALL PLUGIN plugin_name SONAME 'shared_library_name'
--------------------------------------------------------------------------------
然后,可以用插件模式在您的mysqld中安装federated支持:
mysql> install plugin federated soname 'ha_federated.so';
插件安装好之后,还不能立即使用,因为MySQL服务器还没有开启支持选项:
mysql> show plugins;
+----------------+-------------+-----------------------+--------------------+---------+
| Name | Status | Type | Library | License |
+-------------- -+-------------+-----------------------+--------------------+----------+
| FEDERATED | DISABLED | STORAGE ENGINE | ha_federated.so | GPL |
+----------------+-------------+-----------------------+--------------------+----------+
mysql> show engines;
+---------------+---------+-----------------------------------------+--------------+--------+
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL |
+---------------+---------+-----------------------------------------+--------------+--------+
2.如何开启:
配置my.cnf
在[mysqld]下,添加
feterated
3.停止mysqld服务进程,并测试带federated选项启动mysqld服务进程:
# service mysqld stop;
# /usr/bin/mysqld_safe --federated &
正常执行完这一步,再启动mysql:
# service mysqld start
就能看到如下所示:
+---------------+---------+-----------------------------------------+--------------+--------+
| FEDERATED | YES | Federated MySQL storage engine | NULL | NULL |
+---------------+---------+-----------------------------------------+--------------+--------+
但是通常会出现如下错误:
[1] 3678
[root@ml2 ~]# 160302 12:02:00 mysqld_safe Logging to '/var/log/mysqld.log'.
160302 12:02:00 mysqld_safe Starting mysqld daemon with databases from /data/mysql
160302 12:02:00 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
解决方法:
cd /var/run/
#ls
mysqld目录不存在,创建它:
#mkdir /var/run/mysqld
#cd /var/run/mysqld
创建文件mysqld.pid:
#touch mysqld.pid
#cd..
#chown -R mysql mysqld . <---(有那个点【.】{:4_99:})
# cd /usr/local/mysql/
# bin/mysqld_safe --user=mysql &
nohup: ignoring input and redirecting stderr to stdout
Starting mysqld daemon with databases from /var/lib/mysql
能正常启动
又出现错误:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
分析:
是/tmp/mysql.sock 不存在
# cd /var/lib/mysql/
由于mysql 默认的mysql.sock 是在/var/lib/mysql/mysql.sock,
创建软连接:
# ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
以下是关于,配置好并开启federated后,如何使用federated引擎实现远程访问数据库:
1.登陆数据库(192.168.1.118)用户并授权:
mysql> grant select on discuz.* to 'test'@'192.168.1.117' identified by '123456';
2.在DB2里面找到建立表tb2的语句:
mysql> show create table DB2.tb2
显示如下:
- CREATE TABLE `tb2` (
- `ID` bigint(20) NOT NULL COMMENT 'ID',
- `CODE` varchar(30) DEFAULT NULL COMMENT '编码',
- `NAME` varchar(50) DEFAULT NULL COMMENT '名称',
- `IP` varchar(15) DEFAULT NULL COMMENT 'IP地址',
- `STATUS` char(1) DEFAULT NULL COMMENT '状态',
- `CRTR` varchar(50) DEFAULT NULL COMMENT '创建者',
- `CRTDT` timestamp NULL DEFAULT NULL COMMENT '创建时间',
- `UPDR` varchar(50) DEFAULT NULL COMMENT '修改者',
- `UPDDT` timestamp NULL DEFAULT NULL COMMENT '修改时间',
- PRIMARY KEY (`ID`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='上位机表'
- mysql> CREATE TABLE `tb2` (
- `ID` bigint(20) NOT NULL COMMENT 'ID',
- `CODE` varchar(30) DEFAULT NULL COMMENT '编码',
- `NAME` varchar(50) DEFAULT NULL COMMENT '名称',
- `IP` varchar(15) DEFAULT NULL COMMENT 'IP地址',
- `STATUS` char(1) DEFAULT NULL COMMENT '状态',
- `CRTR` varchar(50) DEFAULT NULL COMMENT '创建者',
- `CRTDT` timestamp NULL DEFAULT NULL COMMENT '创建时间',
- `UPDR` varchar(50) DEFAULT NULL COMMENT '修改者',
- `UPDDT` timestamp NULL DEFAULT NULL COMMENT '修改时间',
- PRIMARY KEY (`ID`)
- ) ENGINE=federated connection="mysql://test:123456@192.168.1.118:3306/DB2/tb2";
参考资料:
http://www.linuxidc.com/Linux/2012-09/69965p2.htm
http://www.educity.cn/wenda/400366.html
http://www.2cto.com/database/201412/358397.html
编辑回复