MySQL數據庫如何實現跨服務器訪問數據
在使用MySQL數據庫時,很多同學經常會問,我能跨服務器訪問另一庫的數據么?得到的答案很多時候是讓人失望的。那么如果真的需要訪問,又不想使用拷貝表及數據的方式,可以實現么,又該如何實現呢?
1、如何實現
先說結論:在MySQL數據庫中,是可以實現跨實例(跨服務器)訪問另一個庫中表的。
實現方法:MySQL數據庫的其中一個優點就是插件式管理,因此,可以使用 FEDERATED 存儲引擎來實現來實現。
開啟FEDERATED存儲引擎:
開啟的方式是在配置文件中添加FEDERATED配置,即:
[mysqld]
federated
開啟后如下:
可見,已經支持FEDERATED存儲引擎。
2、具體案例
下面列舉具體示例來演示。
(1)具體案例
需求: 假設服務器A實例中的testdb1庫里有一張表tb1,現在想在服務器B的testdb2中直接訪問testdb1中的tb1表的數據。
實現步驟:
在服務器A中創建表
mysql> create database testdb1;
Query OK, 1 row affected (0.00 sec)
mysql> use testdb1;
Database changed
mysql> create table tb1(id int primary key ,c1 varchar(20));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into tb1 values(1,'a');
Query OK, 1 row affected (0.01 sec)
mysql> insert into tb1 values(2,'b'),(3,'ca'),(4,'tc');
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from tb1;
+----+------+
| id | c1 |
+----+------+
| 1 | a |
| 2 | b |
| 3 | ca |
| 4 | tc |
+----+------+
4 rows in set (0.00 sec)
因為需要遠程訪問A服務器上的表的權限,因此需創建一個數據庫用戶用來遠程訪問。
mysql> create user t_user identified by 'Test2023.com';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on testdb1.* to t_user;
Query OK, 0 rows affected (0.01 sec)
在服務器B的數據庫testdb2上創建FEDERATED存儲引擎表
mysql> create database testdb2;
Query OK, 1 row affected (0.00 sec)
mysql> use testdb2;
Database changed
mysql> create table testdb2_tb1(
-> id INT PRIMARY KEY ,
-> c1 varchar(20)
-> )ENGINE=FEDERATED
-> CONNECTION='mysql://t_user:Test2023.com@127.0.0.1:3306/testdb1/tb1';
Query OK, 0 rows affected (0.00 sec)
mysql> show tables;
+-------------------+
| Tables_in_testdb2 |
+-------------------+
| testdb2_tb1 |
+-------------------+
1 row in set (0.00 sec)
mysql> select * from testdb2_tb1;
+----+------+
| id | c1 |
+----+------+
| 1 | a |
| 2 | b |
| 3 | ca |
| 4 | tc |
+----+------+
4 rows in set (0.02 sec)
創建后可以直接訪問到A服務器中的tb1表的數據。
(2)其他操作
除了查詢,如果創建FEDERATED引擎表的賬號(如本文用的t_user)有增刪改的權限,那么也可以通過操作B服務器的testdb2.testdb2_tb1對遠程表(服務器A上的testdb.tb1)進行相應的操作,例如:
在服務器B上新增數據
mysql> select * from testdb2_tb1;
+----+------+
| id | c1 |
+----+------+
| 1 | a |
| 2 | b |
| 3 | ca |
| 4 | tc |
+----+------+
4 rows in set (0.00 sec)
mysql> insert into testdb2_tb1 values(5,'cc'),(6,'ty');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from testdb2_tb1;
+----+------+
| id | c1 |
+----+------+
| 1 | a |
| 2 | b |
| 3 | ca |
| 4 | tc |
| 5 | cc |
| 6 | ty |
+----+------+
6 rows in set (0.00 sec)
在A服務器上查看數據情況:
mysql> use testdb1;
Database changed
mysql> select * from tb1;
+----+------+
| id | c1 |
+----+------+
| 1 | a |
| 2 | b |
| 3 | ca |
| 4 | tc |
+----+------+
4 rows in set (0.00 sec)
mysql> select * from tb1;
+----+------+
| id | c1 |
+----+------+
| 1 | a |
| 2 | b |
| 3 | ca |
| 4 | tc |
| 5 | cc |
| 6 | ty |
+----+------+
6 rows in set (0.00 sec)
其他操作
mysql> delete from testdb2_tb1 where id=1;
Query OK, 1 row affected (0.01 sec)
mysql> update testdb2_tb1 set c1='bb' where id=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from testdb2_tb1;
+----+------+
| id | c1 |
+----+------+
| 2 | bb |
| 3 | ca |
| 4 | tc |
| 5 | cc |
| 6 | ty |
+----+------+
5 rows in set (0.00 sec)
mysql> alter table testdb2_tb1 add key idx_c1(c1);
ERROR 1031 (HY000): Table storage engine for 'testdb2_tb1' doesn't have this option
mysql> create index idx_c1 on testdb2_tb1(c1);
ERROR 1031 (HY000): Table storage engine for 'testdb2_tb1' doesn't have this option
mysql> truncate table testdb2_tb1;
Query OK, 0 rows affected (0.03 sec)
mysql> select * from testdb2_tb1;
Empty set (0.00 sec)
可見:增刪改查均可以,但是不支持ALTER TABLE操作,可以支持truncate table操作。
3、小結
MySQL數據庫使用FEDERATED引擎表表,可以實現庫實例(跨服務器)的數據訪問及處理,這極大的方便了數據間的關聯、對比及數據治理。關于其實現原理及優劣勢可以在以后的課程合集中細說,感興趣的也可以多實驗了解。