自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

MySQL數(shù)據(jù)庫如何實(shí)現(xiàn)跨服務(wù)器訪問數(shù)據(jù)

數(shù)據(jù)庫 MySQL
MySQL數(shù)據(jù)庫使用FEDERATED引擎表表,可以實(shí)現(xiàn)庫實(shí)例(跨服務(wù)器)的數(shù)據(jù)訪問及處理,這極大的方便了數(shù)據(jù)間的關(guān)聯(lián)、對(duì)比及數(shù)據(jù)治理。關(guān)于其實(shí)現(xiàn)原理及優(yōu)劣勢(shì)可以在以后的課程合集中細(xì)說,感興趣的也可以多實(shí)驗(yàn)了解。

在使用MySQL數(shù)據(jù)庫時(shí),很多同學(xué)經(jīng)常會(huì)問,我能跨服務(wù)器訪問另一庫的數(shù)據(jù)么?得到的答案很多時(shí)候是讓人失望的。那么如果真的需要訪問,又不想使用拷貝表及數(shù)據(jù)的方式,可以實(shí)現(xiàn)么,又該如何實(shí)現(xiàn)呢?

1、如何實(shí)現(xiàn)

先說結(jié)論:在MySQL數(shù)據(jù)庫中,是可以實(shí)現(xiàn)跨實(shí)例(跨服務(wù)器)訪問另一個(gè)庫中表的。

實(shí)現(xiàn)方法:MySQL數(shù)據(jù)庫的其中一個(gè)優(yōu)點(diǎn)就是插件式管理,因此,可以使用 FEDERATED 存儲(chǔ)引擎來實(shí)現(xiàn)來實(shí)現(xiàn)。

開啟FEDERATED存儲(chǔ)引擎:

開啟的方式是在配置文件中添加FEDERATED配置,即:

[mysqld]
federated

開啟后如下:

圖片

可見,已經(jīng)支持FEDERATED存儲(chǔ)引擎。

2、具體案例

下面列舉具體示例來演示。

(1)具體案例

需求: 假設(shè)服務(wù)器A實(shí)例中的testdb1庫里有一張表tb1,現(xiàn)在想在服務(wù)器B的testdb2中直接訪問testdb1中的tb1表的數(shù)據(jù)。

實(shí)現(xiàn)步驟:

在服務(wù)器A中創(chuàng)建表

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)

因?yàn)樾枰h(yuǎn)程訪問A服務(wù)器上的表的權(quán)限,因此需創(chuàng)建一個(gè)數(shù)據(jù)庫用戶用來遠(yuǎn)程訪問。

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)

在服務(wù)器B的數(shù)據(jù)庫testdb2上創(chuàng)建FEDERATED存儲(chǔ)引擎

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)

創(chuàng)建后可以直接訪問到A服務(wù)器中的tb1表的數(shù)據(jù)。

(2)其他操作

除了查詢,如果創(chuàng)建FEDERATED引擎表的賬號(hào)(如本文用的t_user)有增刪改的權(quán)限,那么也可以通過操作B服務(wù)器的testdb2.testdb2_tb1對(duì)遠(yuǎn)程表(服務(wù)器A上的testdb.tb1)進(jìn)行相應(yīng)的操作,例如:

在服務(wù)器B上新增數(shù)據(jù)

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服務(wù)器上查看數(shù)據(jù)情況:

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、小結(jié)

MySQL數(shù)據(jù)庫使用FEDERATED引擎表表,可以實(shí)現(xiàn)庫實(shí)例(跨服務(wù)器)的數(shù)據(jù)訪問及處理,這極大的方便了數(shù)據(jù)間的關(guān)聯(lián)、對(duì)比及數(shù)據(jù)治理。關(guān)于其實(shí)現(xiàn)原理及優(yōu)劣勢(shì)可以在以后的課程合集中細(xì)說,感興趣的也可以多實(shí)驗(yàn)了解。

責(zé)任編輯:姜華 來源: 數(shù)據(jù)庫干貨鋪
相關(guān)推薦

2023-11-29 07:34:25

2011-07-28 17:02:59

MYSQL數(shù)據(jù)庫跨表更新數(shù)據(jù)并合

2019-09-18 08:00:00

MySQL數(shù)據(jù)庫服務(wù)器

2011-03-30 10:15:14

Mysql數(shù)據(jù)庫服務(wù)器

2011-04-07 15:17:40

MySQL數(shù)據(jù)庫服務(wù)器

2010-05-28 10:03:33

MySQL數(shù)據(jù)庫

2010-06-10 17:05:28

2009-11-16 13:24:34

Oracle數(shù)據(jù)庫服務(wù)

2010-06-12 09:46:05

MySQL數(shù)據(jù)庫

2011-03-31 17:02:19

MySQL數(shù)據(jù)庫遠(yuǎn)程連接

2019-08-21 08:57:25

MySQL數(shù)據(jù)庫服務(wù)器

2011-08-29 15:21:30

2011-05-12 13:48:07

MySql數(shù)據(jù)庫本地移植

2010-03-02 15:16:23

Ubuntu Post

2009-09-17 16:16:29

wsus服務(wù)器

2010-06-01 14:58:03

2010-06-07 14:28:44

MySQL數(shù)據(jù)庫

2011-03-14 13:51:16

LAMPMySQL

2011-07-28 14:49:40

2011-04-01 16:00:35

SQL Server 數(shù)據(jù)庫服務(wù)器
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)