哈尔滨cms建站,游戏开发难还是网站开发难,德骏网站建设,微信公众号和网站建设的意义点击上方蓝字关注我 在使用MySQL数据库时#xff0c;很多同学经常会问#xff0c;我能跨服务器访问另一库的数据么#xff1f;得到的答案很多时候是让人失望的。那么如果真的需要访问#xff0c;又不想使用拷贝表及数据的方式#xff0c;可以实现么#xff0c;又该如何实… 点击上方蓝字关注我 在使用MySQL数据库时很多同学经常会问我能跨服务器访问另一库的数据么得到的答案很多时候是让人失望的。那么如果真的需要访问又不想使用拷贝表及数据的方式可以实现么又该如何实现呢 1. 如何实现 先说结论在MySQL数据库中是可以实现跨实例跨服务器访问另一个库中表的。 实现方法MySQL数据库的其中一个优点就是插件式管理因此可以使用 FEDERATED 存储引擎来实现来实现。 开启FEDERATED存储引擎 开启的方式是在配置文件中添加FEDERATED配置即 [mysqld]
federated 开启后如下 可见已经支持FEDERATED存储引擎 2. 具体案例 下面列举具体示例来演示 2.1 具体案例 需求 假设服务器A实例中的testdb1库里有一张表tb1,现在想在服务器B的testdb2中直接访问testdb1中的tb1表的数据 实现步骤 1 在服务器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: 0mysql 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) 2 在服务器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)- )ENGINEFEDERATED- CONNECTIONmysql://t_user:Test2023.com127.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.2 其他操作 除了查询如果创建FEDERATED引擎表的账号如本文用的t_user有增删改的权限那么也可以通过操作B服务器的testdb2.testdb2_tb1对远程表服务器A上的testdb.tb1进行相应的操作例如 1 在服务器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: 0mysql 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) 2 在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) 3) 其他操作 mysql delete from testdb2_tb1 where id1;
Query OK, 1 row affected (0.01 sec)mysql update testdb2_tb1 set c1bb where id2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0mysql 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 doesnt have this optionmysql create index idx_c1 on testdb2_tb1(c1);
ERROR 1031 (HY000): Table storage engine for testdb2_tb1 doesnt 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引擎表表可以实现库实例跨服务器的数据访问及处理这极大的方便了数据间的关联、对比及数据治理。关于其实现原理及优劣势可以在以后的课程合集中细说感兴趣的也可以多实验了解。 往期精彩回顾 1. MySQL高可用之MHA集群部署 2. mysql8.0新增用户及加密规则修改的那些事 3. 比hive快10倍的大数据查询利器-- presto 4. 监控利器出鞘PrometheusGrafana监控MySQL、Redis数据库 5. PostgreSQL主从复制--物理复制 6. MySQL传统点位复制在线转为GTID模式复制 7. MySQL敏感数据加密及解密 8. MySQL数据备份及还原一 9. MySQL数据备份及还原二 扫码关注