dblink(Database Link)数据库链接顾名思义就是数据库的链接,就像电话线一样,是一个通道,当我们要跨本地数据库,访问另外一个数据库表中的数据时,本地数据库中就必须要创建远程数据库的dblink,通过dblink本地数据库可以像访问本地数据库一样访问远程数据库表中的数据。
当整合两个位于不同服务器上数据库的内容,就遇到了远程访问数据库的问题。在oracle中可以通过dblink来实现跨本地数据库来访问另外一个数据库中的数据。MySQL中的federated插件来实现类似的功能。
实现功能:
可以在Linux系统中MySQL数据库(target端)中建立宿主机MySQL数据库(source端)中某个表的link,当在Linux中读取link表时,就相当于直接读取宿主机中的原始表内容。
实现步骤:
1. 查看target端(Linux虚拟机中)是否安装了federated插件:
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 |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
显示没有安装federated插件
2. 安装federated插件:
mysql>install plugin federated soname 'ha_federated.so';
ERROR 1125 (HY000): Function 'federated' already exists
说明已经安装过了,但没有启用
[root@localhost etc]# service mysql stop
[root@localhost etc]# mysqld_safe --federated &
[root@localhost etc]# 140811 01:20:21 mysqld_safe Logging to '/var/lib/mysql/localhost.localdomain.err'.
140811 01:20:22 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
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 |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| FEDERATED |YES | Federated MySQL storage engine | NULL | NULL | NULL |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
federated插件已经启用
3. 配置/etc/my.conf,设置federated为默认启动
[root@localhost etc]# vi /etc/my.conf
在文件中加入一行:
federated
重启mysql服务
service mysql restart
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 |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| FEDERATED | YES | Federated MySQL storage engine | NULL | NULL | NULL |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
已经设置为默认启动了。
4. 在source端建立测试表,我是通过Navicat建立表的,其sql文件为:
DROP TABLE IF EXISTS `e_eledata`;
CREATE TABLE `e_eledata` (
`ID` bigint(20) unsigned NOT NULL auto_increment COMMENT 'ID',
`E_ELEMETERHEAD_ID` bigint(20) default NULL COMMENT '电表表头ID',
`DAQDT` timestamp NULL default NULL COMMENT '数据采集时间',
`DLDT` timestamp NULL default NULL COMMENT '数据入库时间',
`APCURRENT` decimal(10,3) default NULL COMMENT 'A相电流。单位:A。',
`BPCURRENT` decimal(10,3) default NULL COMMENT 'B相电流。单位:A。',
`CPCURRENT` decimal(10,3) default NULL COMMENT 'C相电流。单位:A。',
`APVOLTAGE` decimal(10,3) default NULL COMMENT 'A相电压。单位:V。',
`BPVOLTAGE` decimal(10,3) default NULL COMMENT 'B相电压。单位:V。',
`CPVOLTAGE` decimal(10,3) default NULL COMMENT 'C相电压。单位:V。',
PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='电路数据表';
5. 在target端建立link表,可以直接改写source表的sql脚本文件为:
DROP TABLE IF EXISTS `e_eledata_link`;
CREATE TABLE `e_eledata_link` (
`ID` bigint(20) unsigned NOT NULL auto_increment COMMENT 'ID',
`E_ELEMETERHEAD_ID` bigint(20) default NULL COMMENT '电表表头ID',
`DAQDT` timestamp NULL default NULL COMMENT '数据采集时间',
`DLDT` timestamp NULL default NULL COMMENT '数据入库时间',
`APCURRENT` decimal(10,3) default NULL COMMENT 'A相电流。单位:A。',
`BPCURRENT` decimal(10,3) default NULL COMMENT 'B相电流。单位:A。',
`CPCURRENT` decimal(10,3) default NULL COMMENT 'C相电流。单位:A。',
`APVOLTAGE` decimal(10,3) default NULL COMMENT 'A相电压。单位:V。',
`BPVOLTAGE` decimal(10,3) default NULL COMMENT 'B相电压。单位:V。',
`CPVOLTAGE` decimal(10,3) default NULL COMMENT 'C相电压。单位:V。',
PRIMARY KEY (`ID`)
) ENGINE=FEDERATED AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='电路数据表'
CONNECTION='mysql://usrname:password@192.168.1.98:3306/databasename/table //宿主机的数据库
其中:
usrname为宿主机中MySQL的用户名
password为相应的密码
(要保证该用户具有远程登陆的权限,可以通过以下命令来设置:
mysql>GRANT ALL PRIVILEGES ON *.* TO 'usrname'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
其中*.*是指对用户开放所有数据库和表的权限,如果只开放某一个数据库的一个表为databasename.table;'%'指的是该用户可以从任意的一个ip地址来远程访问数据库,包括本地,如果要限制用户从特定的ip来访问,将其改为'ip地址')
192.168.1.98是source数据库的ip,这里为我宿主机的ip
3306为数据库的端口,默认一般为3306
database 和table分别为source端数据库的名称和表名称
将该sql脚本在target端运行
6. 实现跨本地数据库的访问
在target端通过访问e_eledata_link表来访问source端e_eledata表
mysql> select *from e_eledata_link;