1. 程式人生 > 其它 >MYSQL建立dblink資料同步MySQL資料庫檢視的建立

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 EXISTSe_eledata;
CREATE TABLEe_eledata(
IDbigint(20) unsigned NOT NULL auto_increment COMMENT ‘ID’,
E_ELEMETERHEAD_IDbigint(20) default NULL COMMENT ‘電錶表頭ID’,
DAQDTtimestamp NULL default NULL COMMENT ‘資料採集時間’,
DLDTtimestamp NULL default NULL COMMENT ‘資料入庫時間’,
APCURRENTdecimal(10,3) default NULL COMMENT ‘A相電流。單位:A。’,
BPCURRENTdecimal(10,3) default NULL COMMENT ‘B相電流。單位:A。’,
CPCURRENTdecimal(10,3) default NULL COMMENT ‘C相電流。單位:A。’,
APVOLTAGEdecimal(10,3) default NULL COMMENT ‘A相電壓。單位:V。’,
BPVOLTAGEdecimal(10,3) default NULL COMMENT ‘B相電壓。單位:V。’,
CPVOLTAGEdecimal(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 EXISTSe_eledata_link;
CREATE TABLEe_eledata_link(
IDbigint(20) unsigned NOT NULL auto_increment COMMENT ‘ID’,
E_ELEMETERHEAD_IDbigint(20) default NULL COMMENT ‘電錶表頭ID’,
DAQDTtimestamp NULL default NULL COMMENT ‘資料採集時間’,
DLDTtimestamp NULL default NULL COMMENT ‘資料入庫時間’,
APCURRENTdecimal(10,3) default NULL COMMENT ‘A相電流。單位:A。’,
BPCURRENTdecimal(10,3) default NULL COMMENT ‘B相電流。單位:A。’,
CPCURRENTdecimal(10,3) default NULL COMMENT ‘C相電流。單位:A。’,
APVOLTAGEdecimal(10,3) default NULL COMMENT ‘A相電壓。單位:V。’,
BPVOLTAGEdecimal(10,3) default NULL COMMENT ‘B相電壓。單位:V。’,
CPVOLTAGEdecimal(10,3) default NULL COMMENT ‘C相電壓。單位:V。’,
PRIMARY KEY (ID)
) ENGINE=FEDERATEDAUTO_INCREMENT=1DEFAULT CHARSET=utf8 COMMENT=‘電路資料表’

CONNECTION='mysql://usrname:[email protected]: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:[email protected]:3307/t2_cpv2/view_bt_input_bank_info';

engine=federated connection='mysql://使用者:密碼@IP地址:埠/庫名稱/表名稱';

完成後就可以本地資料庫查到遠端庫資料:SELECT * FROM dblink_view;