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;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM