federated是默认关闭的,所以需要先开启它
首先找到mysql的安装目录。找到启动文件
my.ini
用编辑器打开my.ini在末尾添加
#开启federated
federated
重启mysql,以管理员权限启动cmd
C:\WINDOWS\system32>net stop mysql
MySQL 服务正在停止..
MySQL 服务已成功停止。
C:\WINDOWS\system32>net start mysql
MySQL 服务正在启动 .
MySQL 服务已经启动成功。
进入mysql
C:\WINDOWS\system32>mysql -uroot -p
Enter password: *********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
查看引擎
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+-
-----------+
| Engine | Support | Comment | Transactions | XA |
Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+-
-----------+
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES |
YES |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO |
NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary 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 |
+--------------------+---------+----------------------------------------------------------------+--------------+------+-
-----------+
9 rows in set (0.00 sec)
mysql>
FEDERATED 后变成YES ,已开启
建立本地库
mysql> create database local;
Query OK, 1 row affected (0.00 sec)
在远程数据库mysql上建立远程库remote库,可以使用innodb引擎
CREATE TABLE `remote` (
`id` int(11) NOT NULL,
`c1` varchar(10) NOT NULL,
`c2` varchar(10) NOT NULL
) ENGINE=InnoDB;
插入数据
insert into remote values(1,'2','2'),(2,'2','2');
查询远程数据库
mysql> select * from remote;
+----+----+----+
| id | c1 | c2 |
+----+----+----+
| 1 | 2 | 2 |
| 2 | 2 | 2 |
+----+----+----+
2 rows in set (1.55 sec)
然后在远程数据库上给予本地数据库crud的权限
mysql> grant select,update,insert ,delete on remote.remote to lock_link@'xxx.xxx.xxx' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.21 sec)
remote.remote 当前数据库remote库下的remote
lock_link为权限账户
123456为权限密码
xxx.xxx.xxx为本地数据库的外网ip
在本地建立存储引擎为federated表:
CREATE TABLE `remote` (
`id` int(11) NOT NULL,
`c1` varchar(10) NOT NULL,
`c2` varchar(10) NOT NULL
) ENGINE=federated connection='mysql://lock_link:123456@xxx.xxx.xxx:3306/remote/remote';
lock_link:123456 为在远程数据库上设置的权限账户和密码
此处xxx.xxx.xxx:3306为远端数据库的外网ip,请保证ping的通,数据库端口
remote/remote远程数据库下的remote库下的remote
然后没有插入,直接查看本地remote表
mysql> select * from remote;
+----+----+----+
| id | c1 | c2 |
+----+----+----+
| 1 | 2 | 2 |
| 2 | 2 | 2 |
+----+----+----+
2 rows in set (1.55 sec)
本地只储存了该表的数据结构,即以.frm为后缀的文件,数据储存在远端数据库里