Connecting to a Specific MySQL Server via SSH Tunnel
Connecting to multiple MySQL databases hosted at different addresses through an SSH tunnel can be a cumbersome task. This becomes apparent when attempting to specify a particular MySQL server after creating the tunnel.
To overcome this challenge, it's crucial to understand that the SSH tunnel should be established while connecting to the desired MySQL host. The following command demonstrates this process:
ssh -f [email protected] -L 3307:mysql1.example.com:3306 -N
By specifying mysql1.example.com:3306 in the -L flag, the tunnel is configured to forward port 3307 on the localhost to port 3306 on the intended MySQL server.
Once the tunnel is established, connecting to the MySQL server is as simple as:
mysql -h 127.0.0.1 -P 3307
This command should now successfully establish a connection to the specified MySQL host, allowing you to access the database as intended.
The above is the detailed content of How to Connect to a Specific MySQL Server via SSH Tunnel?. For more information, please follow other related articles on the PHP Chinese website!