mysql基于federated存儲引擎訪問遠(yuǎn)程數(shù)據(jù)庫表
這個是有一個前置條件:兩個數(shù)據(jù)庫都必須是mysql數(shù)據(jù)庫,且被訪問的表必須是可以通過網(wǎng)絡(luò)訪問到的
(同一臺機(jī)器也好,同一個局域網(wǎng)也好,走外網(wǎng)映射也好,總之要能被訪問就是了)
假設(shè)有test和test1兩個數(shù)據(jù)庫。
test1庫想訪問test庫的表student
1、先開啟test1庫的federated存儲引擎查看數(shù)據(jù)庫的federated存儲引擎是否開啟;
打開mysql的命令行客戶端,輸入show engines;
mysql> show engines;
+--------------------+---------+
----------------------------------------------------------------+--------------+-----+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+
----------------------------------------------------------------+--------------+-----+------------+
| FEDERATED | YES | Federated MySQL storage engine | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | 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 |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
+--------------------+---------+
----------------------------------------------------------------+--------------+-----+------------+
9 rows in set (0.05 sec)
FEDERATED官方簡介:
FEDERATED存儲引擎訪問在遠(yuǎn)程數(shù)據(jù)庫的表中的數(shù)據(jù),而不是本地的表。這個特性給某些開發(fā)應(yīng)用帶來了遍歷,你可以直接在本地構(gòu)建一個federated表來連接遠(yuǎn)程數(shù)據(jù)表,配置好了之后本地表的數(shù)據(jù)可以直接跟遠(yuǎn)程數(shù)據(jù)表同步。
實(shí)際上這個引擎里面是不真實(shí)存放數(shù)據(jù)的,所需要的數(shù)據(jù)都是連接到其他MySQL服務(wù)器上。
測試步驟和環(huán)境說明
驗(yàn)證test庫下student表
mysql>use test;
mysql> select * from student;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1| wang | 18 |
| 2| li | 19 |
| 3| wu | 19 |
| 4| xie | 20 |
+----+------+-----+
4 rows in set (0.09 sec)
創(chuàng)建遠(yuǎn)程存儲引擎表student1
mysql> CREATE TABLE `student1` (
`id` int(11) NOT NULL,
`name` varchar(25) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`))
-> ENGINE=FEDERATED DEFAULT CHARSET=utf8?connection='mysql://root:mysql@192.168.0.145/test/student';
Query OK, 0 rows affected (0.09 sec)
注意:本地創(chuàng)建的表名必須在遠(yuǎn)程服務(wù)器存在,創(chuàng)建的字段也必須是遠(yuǎn)程表中的字段,可以比遠(yuǎn)程表的字段少,但是不能多,本地存儲引擎選擇:ENGINE =FEDERATED,
CONNECTION選項(xiàng)中的連接字符串的一般形式如下:scheme://user_name[:password]@host_name[:port_num]/db_name/tbl_name
