MYSQL建立dblink数据同步MySQL数据库视图的创建

MYSQL建立dblink数据同步MySQL数据库视图的创建

MySQL数据库视图的创建,需要整合两个位于不同服务器上数据库的内容,就遇到了远程访问数据库的问题。在cracle中可以通过dblink来实现跨本地数据库来访问另外一个数据库中的数据。通过在网上查找,发现可以通过MySQL中的federated插件来实现类似的功能。

操作环境:

宿主机为win8系统,MySQL数据库,ip:192.168.1.98;从机为VMware虚拟机中的Linux系统,版本为CentOS6.5,MySQL数据库,ip:192.168.1.106。

实现功能:

可以在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插件

  1. 安装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插件已经启用

  1. 配置/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 |
±-------------------±--------±---------------------------------------------------------------±-------------±-----±-----------+

已经设置为默认启动了。

  1. 在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=‘电路数据表’;

  1. 在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=FEDERATEDAUTO_INCREMENT=1DEFAULT 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端运行

  1. 实现跨本地数据库的访问

在target端通过访问e_eledata_link表来访问source端e_eledata表

mysql> select *from e_eledata_link;

 

 

MYSQL建立dblink数据同步MySQL数据库视图的创建

 

mysql的dblink就是访问目标数据库,通过dblink连接的方式来访问到源数据库的数据。

这里源端为远程服务器,目标端为本地

查看本地mysql的有没有federated 引擎:
mysql> show engines;

+--------------------+---------+------------------------------------------------
----------------+--------------+------+------------+
| Engine | Support | Comment
| Transactions | XA | Savepoints |
+--------------------+---------+------------------------------------------------
----------------+--------------+------+------------+
| InnoDB | DEFAULT | Supports transactions, row-level locking, and f
oreign keys | YES | YES | YES |
| MRG_MYISAM | YES | Collection of identical MyISAM tables
| NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for tempor
ary tables | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to
it disappears) | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine
| NO | NO | NO |
| CSV | YES | CSV storage engine
| NO | NO | NO |
| ARCHIVE | YES | Archive storage engine
| NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema
| NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine
| NULL | NULL | NULL |
+--------------------+---------+------------------------------------------------
----------------+--------------+------+------------+

安装了federated,但没启动。(如果没有安装FEDERATED 引擎        执行install plugin federated soname 'ha_federated.so';)

在mysql目录下的my.ini文件里添加一行

federated

重启mysql服务,然后查看federated 

mysql> show engines;

+--------------------+---------+------------------------------------------------
----------------+--------------+------+------------+
| Engine | Support | Comment
| Transactions | XA | Savepoints |
+--------------------+---------+------------------------------------------------
----------------+--------------+------+------------+
| InnoDB | DEFAULT | Supports transactions, row-level locking, and f
oreign keys | YES | YES | YES |
| MRG_MYISAM | YES | Collection of identical MyISAM tables
| NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for tempor
ary tables | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to
it disappears) | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine
| NO | NO | NO |
| CSV | YES | CSV storage engine
| NO | NO | NO |
| ARCHIVE | YES | Archive storage engine
| NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema
| NO | NO | NO |
| FEDERATED | YES | Federated MySQL storage engine
| NO | NO | NO |
+--------------------+---------+------------------------------------------------
----------------+--------------+------+------------+

看到已开启

在本地创建和远程数据库的表的表结构一致的表,以及远程数据库的连接:例如

CREATE TABLE dblink_view (
bank_name VARCHAR(50),
sys_bank_code VARCHAR(12),
PROV_INPUT_NAME VARCHAR(20),
NAME VARCHAR(10)
)ENGINE=FEDERATED CONNECTION='mysql://bankinfo:bankinfo@192.168.3.42:3307/t2_cpv2/view_bt_input_bank_info';

 engine=federated connection = 'mysql://用户:密码@IP地址:端口/库名称/表名称';

完成后就可以本地数据库查到远程库数据:SELECT * FROM dblink_view;

原文地址:https://www.cnblogs.com/xinxihua/p/15407615.html