Accessing remote databases securely is crucial for many web developers and system administrators. One of the most effective methods for achieving this is by establishing an SSH tunnel. This guide provides a concise, step-by-step approach to setting up an SSH tunnel and connecting to your remote database, ensuring your data remains protected during transit. Whether you’re new to tunneling or looking for a quick refresher, this guide will equip you with the essential knowledge to establish a secure connection.
Add Your Public Key
You should have your public key added to the account you’re trying to connect to in the SSH Access section of your hosting control panel.
Connect Via SSH
Enter the following into your terminal where :
- <username> is the username of the account
- <remote_port> is the remote port number
- <local_port> is the local port you use to connect to SQL (usually 3306)
- <remote_ip> is the remote IP of your hosting
- <ssh_port> is the SSH port number
ssh -p <ssh_port> -L <remote_port>:localhost:<local_port> <username>@<remote_ip>This forwards your local MySQL port to the remote connection.
Connect WordPress
Connect your local website via the new port. Change your .env or wp_config.php file with the following DB Host.
DB_HOST=127.0.0.1:<local_port>
# For example if local port is 3307
# DB_HOST=127.0.0.1:3307You’re connected! Keep the connection open while you’re working on this site.