MySQL cross-server data mapping
In daily development, data is often queried across databases.
To query across databases under the same server, the data can be queried by adding the database name before the table.
When the data exceeds the load of the server, it is often necessary to store the data in different servers. At this time, it involves the business of operating across servers.
Querying across servers mysql provides the FEDERATED engine to map tables and then query.
Environmental preparation
1. The first is to check the federated engine on/off status of the database
show engines;
The mysql database federated engine is closed,
- start federated
- Find your own mysq>>>my.ini
- Add federated under [mysqld] to enable the FEDERATED engine and save the file
2. Open the task manager and restart the mysql service
3. Check the FEDERATED engine status again, the engine has started
Mapping implementation
Create the table that needs to be mapped in the remote server database in mysql. The name of the mapping table can be named freely, but the data structure must be the same.
CREATE TABLE `hn_user` (
`id` varchar(32) NOT NULL,
`name` varchar(20) DEFAULT NULL,
`phone` varchar(11) DEFAULT NULL,
`idcard` varchar(18) DEFAULT NULL,
`update_time` bigint(13) DEFAULT NULL,
`add_time` bigint(13) DEFAULT NULL,
PRIMARY KEY (`id`) //mysql://用户名:密码@服务器ip:端口/数据库名/表名
) ENGINE=FEDERATED CONNECTION='mysql://root:[email protected]:3306/db/user';
Note: ENGINE=FEDERATED, use the federated engine, modify the user name, password, address, port number, database, table,
so that the remote user table data can be mapped to the hn_user table in real time, and mysql cross-server query data can be realized.
conclusion of issue
Encountered during use:
After creating the table, the following error will pop up when opening.
The reason is that the mysql version I installed is version 8, and the higher version of ssl is enabled by default. We just need to turn off ssl,
log in to mysql, and execute the command
SHOW VARIABLES LIKE '%ssl%';
You can see that the ssl on my side is enabled.
Enter the my.ini file and add it under [mysqld]
skip_ssl
Restart the mysql service