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

  1. 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.

  2. Build the SSH Tunnel: Use the following command to establish an SSH tunnel. Replace login with your SSH username and server with your server’s address (e.g., login.quintagroup.com):

    1
    
    ssh login@server -L 3306:127.0.0.1:3306 -N

    You will be prompted to enter your SSH password. Go ahead and enter it.

  3. Background the SSH Tunnel: To allow you to run other commands while keeping the SSH tunnel active, press Ctrl+Z to pause the tunnel process. You should see a message like:

    [1]+ Stopped        ssh login@server -L 3306:127.0.0.1:3306 -N

    To move this process to the background, type bg and press Enter:

    [1]+ ssh login@server -L 3306:127.0.0.1:3306 -N &
  4. 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 user with your MySQL username and database with your database name:

    1
    
    mysql -h 127.0.0.1 -p -u user database

    You will be prompted to enter the password for your MySQL database, which can also be found in your Quintagroup account.

  5. 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.

  6. 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.

  7. 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 pressing Ctrl+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.

0%