注:mysql8 已弃用,不再提供官方支持
my.ini 开启
[mysqld]
federated = 1
#创建连接选项
CREATE SERVER remote_server
FOREIGN DATA WRAPPER mysql
OPTIONS (USER 'user', PASSWORD 'password', HOST '192.168.1.1', DATABASE 'db_name');
#删除连接选项
DROP SERVER <server_name>;
#更改连接选项
ALTER SERVER <server_name>
OPTIONS (
option_name1 'option_value1',
option_name2 'option_value2',
...
);
#建表语句使用默认db
CREATE TABLE my_federated_table (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(50),
value INT(11),
PRIMARY KEY (id)
) ENGINE=FEDERATED
DEFAULT CHARSET=utf8mb4
CONNECTION='remote_server/my_table';
#建表语句指定 db
CREATE TABLE my_federated_table (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(50),
value INT(11),
PRIMARY KEY (id)
) ENGINE=FEDERATED
DEFAULT CHARSET=utf8mb4
CONNECTION='remote_server/my_db/my_table';