MySQL Cross-Server Select Queries: A Detailed Guide
When working with multiple MySQL servers that are dispersed geographically, the need for cross-server data manipulation arises. One such scenario is the requirement to select data from a table on one server and insert it into a table on another server located miles apart.
To address this challenge, MySQL provides the capability of establishing SSH tunnels to facilitate secure communication between servers. However, it is important to note that directly accessing tables across servers via SSH tunnels is not supported by MySQL.
Instead, a powerful feature called federated tables offers an elegant solution to this problem. With federated tables, you can create a local proxy table representing a remote table on another server. This allows you to execute queries against the local federated table as if the remote table were physically present on the local server.
Creating Federated Tables
To create a federated table, you must first define the remote table on the server where it resides. Once the remote table is established, you can proceed to create a federated table on the local server using the following syntax:
CREATE TABLE federated_table ( <column definitions> ) ENGINE=FEDERATED CONNECTION='mysql://<username>@<remote_host>:<port>/<database>/<table_name>';
In this example, the CONNECTION attribute specifies the details of the remote table, including the database name, table name, username, and password.
Executing Cross-Server Queries
Once the federated table has been created, you can execute cross-server select queries as if the remote table were local. For instance, consider the following query:
SELECT * FROM federated_table WHERE <condition>;
This query will retrieve data from the remote table specified in the CONNECTION attribute of the federated table. The results of the query will be available locally, as if they were obtained from a table on the local server.
Additional Considerations
The above is the detailed content of How Can I Efficiently Perform Cross-Server SELECT Queries in MySQL?. For more information, please follow other related articles on the PHP Chinese website!