How to Connect to MySQL via SSH Tunnel in Linux
When you need to connect to a MySQL server from outside your local network, using an SSH tunnel can provide a secure and convenient method for access. In this guide, we will walk you through the steps to connect to a MySQL server via SSH tunnel on a Linux system. This method can be particularly useful if you are trying to access a remote MySQL server hosted by a service provider like Quintagroup.
Prerequisites
Before you begin, make sure you have the following:
-
SSH login credentials: You should have your SSH login details, including the username and server address. You can typically find these details in your Quintagroup account.
-
MySQL database access details: You’ll need the MySQL username, database name, and password. You can obtain these details from your MySQL access information in your Quintagroup account.
Steps to Connect
-
Open a Terminal Window: Begin by opening a terminal window on your Linux system. You’ll use this terminal to execute commands and create the SSH tunnel.
-
Build the SSH Tunnel: Use the following command to establish an SSH tunnel. Replace
loginwith your SSH username andserverwith your server’s address (e.g., login.quintagroup.com):1ssh login@server -L 3306:127.0.0.1:3306 -NYou will be prompted to enter your SSH password. Go ahead and enter it.
-
Background the SSH Tunnel: To allow you to run other commands while keeping the SSH tunnel active, press
Ctrl+Zto pause the tunnel process. You should see a message like:[1]+ Stopped ssh login@server -L 3306:127.0.0.1:3306 -NTo move this process to the background, type
bgand press Enter:[1]+ ssh login@server -L 3306:127.0.0.1:3306 -N & -
Connect to MySQL: Now that the SSH tunnel is established, you can connect to the MySQL server as if it were running on your local system (localhost:3306). Use the following command, replacing
userwith your MySQL username anddatabasewith your database name:1mysql -h 127.0.0.1 -p -u user databaseYou will be prompted to enter the password for your MySQL database, which can also be found in your Quintagroup account.
-
You’re In!: Congratulations, you are now connected to the remote MySQL server via the SSH tunnel. You can execute SQL commands and queries as needed.
-
Closing the Connection: When you’re done, close the application you used to access the MySQL server. If you were using a command-line tool (as mentioned in step 4), you can exit it by typing
Ctrl+D. -
Returning to the SSH Tunnel: To bring the SSH tunnel back to the foreground, type
fg. If you need to stop the tunnel, you can do so by pressingCtrl+C.
By following these steps, you can securely connect to a remote MySQL server via an SSH tunnel on your Linux system. This method ensures your data remains encrypted during the transfer and is a reliable way to access your databases remotely.