Connecting to a Specific MySQL Server Through an SSH Tunnel
When managing multiple databases hosted at different locations, it may be necessary to access them through an SSH tunnel. While creating the tunnel is straightforward, connecting to a specific MySQL server beyond the tunnel can be confusing.
To specify the MySQL hostname after establishing the SSH tunnel:
Modify the SSH Tunnel Command:
When creating the SSH tunnel, include the desired MySQL hostname as an additional parameter. For example:
ssh -f [email protected] -L 3307:mysql1.example.com:3306 -N
In this command, the tunnel is being forwarded to port 3307 on the local machine and then to port 3306 on the mysql1.example.com server.
Connect to the MySQL Server:
Once the tunnel is created, use the following command to connect to the MySQL server:
mysql -h 127.0.0.1 -P 3307
By specifying 127.0.0.1 as the host and 3307 as the port, the MySQL client will connect to the localhost port 3307, which is the local end of the SSH tunnel. This will then forward the connection to the mysql1.example.com server.
The above is the detailed content of How to Connect to a Specific MySQL Server Through an SSH Tunnel?. For more information, please follow other related articles on the PHP Chinese website!