MySQL创建远程链接服务器LinkServer步骤

环境准备


mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
| MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.35 sec)

情况1: 没有FEDERATED这个引擎

install plugin federated soname 'ha_federated.so';

show plugin;

show engines; 
# 这时候会进入情况2:

情况2: FEDERATED| NO FEDERATED引擎已安装,但未启用(最常见)

...

[mysqld]
federated
default-time_zone = '+8:00'
character_set_server = utf8mb4
... ...

情况3: FEDERATED| YES 已完成准备

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         |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+

创建链接服务器


CREATE SERVER {servername}
FOREIGN DATA WRAPPER mysql
OPTIONS (HOST '{远程域名或ip}',PORT {端口号},DATABASE ' {dbname}',USER '{用户名}', PASSWORD '{密码}');

# 示例:

CREATE SERVER my02testdb
FOREIGN DATA WRAPPER mysql
OPTIONS (HOST '172.10.2.02',PORT 3308,DATABASE 'testdb',USER 'testdb_reader', PASSWORD 'Abcde.123456');

使用链接服务器


create database dbremote;
use dbremote;

create table t1(`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id',
  `user_no` varchar(50) NOT NULL DEFAULT '' COMMENT '用户工号',
  `user_name` varchar(50) NOT NULL DEFAULT '' COMMENT '用户姓名',
    `creation_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `modified_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  PRIMARY KEY (`id`) 
)  ENGINE=FEDERATED CONNECTION='my02testdb';

总结

>> Home

51ak

2024/09/05

Categories: mysql 常用脚本 Tags: 原创

《数据库工作笔记》公众号
扫描上面的二维码,关注我的《数据库工作笔记》公众号