Connecting to Remote MySQL Servers via SSH Tunnels: Specifying Non-Default Hostnames
The ability to securely access MySQL databases remotely using SSH tunnels is essential for seamless database management and data access. However, specifying a non-default MySQL hostname after creating the SSH tunnel can be a challenging task.
SSH Tunnel Creation
To establish the SSH tunnel, you can utilize the autossh tool, as mentioned in the referenced question. However, the key lies in designating the correct hostname when setting up the tunnel:
<code class="bash">ssh -f [email protected] -L 3307:mysql1.example.com:3306 -N</code>
Notice the inclusion of mysql1.example.com:3306 in the command. This notation specifies that port 3307 on your local machine will be forwarded to port 3306 on the remote MySQL host at mysql1.example.com.
MySQL Connection
Once the tunnel is established, you can proceed to connect to the MySQL database on the remote server as follows:
<code class="bash">mysql -h 127.0.0.1 -P 3307</code>
The -h option specifies the hostname to connect to, in this case, 127.0.0.1 (localhost), and the -P option specifies the port, which is 3307, the port that is mapped to the MySQL host's port 3306.
By following these steps, you can effectively specify a non-default MySQL hostname when connecting to remote databases via SSH tunnels. This enables you to manage and access multiple databases from a central location, enhancing your database administration capabilities.
The above is the detailed content of How to Connect to Remote MySQL Servers via SSH Tunnels with Non-Default Hostnames?. For more information, please follow other related articles on the PHP Chinese website!