Use the MySQL federated storage engine (FEDERATED) to implement operations on remote server tables, or to implement distributed storage of database data

I. Overview

When a table is created using a standard storage engine, the table consists of a table definition and associated data . Using federated storage engines when creating tables, the table definition is the same for the local and remote databases , but the physical storage of the data is handled on the remote server . This enables data to be accessed from remote MySQL databases without using replication or clustering technologies. Querying the local table automatically pulls data from the remote (federated) table. No data is stored on local tables. By using the MySQL joint storage engine, it is possible to operate tables on remote servers or realize distributed storage of database data. The joint table structure is as follows:

2. Enable the joint storage engine

1. Check whether the engine is enabled

Use the following command on the MySQL command line to view the engine status:

SHOW ENGINES

If FEDERATED support is: NO , the engine needs to be enabled.

If the value is YES, the engine is enabled.

2. Enable the engine

The specific method is:

Add a line under [mysqld] in the MySQL configuration file my.cnf (or my.ini):

[mysqld]
federated

Click Save.

3. Restart the MySQL service

service mysql restart #在linux服务器

3. A way to create a joint table

1. Create a table on the remote server

The following is an example of a create statement:

CREATE TABLE `tablename` (

  ......(此处填写对应创建语句)

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;

 2. Create the table on the local server

The following is an example of a create statement:

CREATE TABLE `tablename` (

  ......(此处填写对应创建语句)

) 
ENGINE=FEDERATED
DEFAULT CHARSET=utf8mb3
CONNECTION='mysql://用户名:密码@你的IP:端口/数据库名/数据表名';

The format of the connection string on the last line of the create statement above is:

scheme://user_name[:password]@host_name[:port_num]/db_name/tb_name

scheme:公认的连接协议。此时仅支持将其作为scheme值 mysql
user_name:连接的用户名。此用户必须是在远程服务器上创建的,并且必须具有适当的权限才能对远程表执行所需的操作(SELECT、INSERT、UPDATE 等)
password:(可选)user_name的相应密码。
host_name:远程服务器的主机名或 IP 地址
port_num:(可选)远程服务器的端口号,默认值为 3306
db_name:保存远程表的数据库的名称
tb_name:远程表的名称。本地表和远程表的名称不必匹配

The following is a sample connection string:

CONNECTION='mysql://username:password@hostname:port/database/tablename'
CONNECTION='mysql://username@hostname/database/tablename'
CONNECTION='mysql://username:password@hostname/database/tablename'

4. Test

Operate the table locally, and then query the corresponding table in the remote database. If the data is consistent, the creation is successful.

For more information and precautions, please refer to:

The FEDERATED Storage Engine

Guess you like

Origin blog.csdn.net/qq_44667259/article/details/123531397