Tunnel local MySQL server through SSH

  • Applies to: Grid
    • Difficulty: Medium
    • Time Needed: 10
    • Tools Required: SSH
  • Applies to: Legacy DV & VPS Hosting
    • Difficulty: Easy
    • Time Needed: 10
    • Tools Required: SSH


This article explains how to tunnel a MySQL connection through SSH. This is useful when you want to execute MySQL commands or develop applications remotely from your server.


Before you start, you'll need:

  • SSH accessSSH access
  • SSH client
  • MySQL internal hostname: Be sure to replace 00000 with your site number.
  • IP address for your VPS


Configure the tunnel

First, let's set up a local tunnel on a port. This ensures that local commands will be passed through to the remote server.


These instructions are for Terminal on Mac OS X. For Windows, we recommend PuTTY and the instructions to forward port 3306.

Run the following command in Terminal:

  • ssh -L

    Be sure to replace with your own database server name, and with your SSH login. You should replace with your primary domain. You can find more database information within your Server Guide.

  • ssh -L

    You will need to replace with your own IP address or server name, and with your own login.

Keep the SSH session running while you work. This will keep the tunnel open.

Explanation of the command:

  • ssh - You are using SSH to forward the port.
  • -L - This option lets you specify a local port that will be forwarded to your remote host and remote port.
  • 3306: - Local port 3306 will be forwarded over your SSH connection. 3306 is standard for MySQL.
  • The IP address or hostname of the remote MySQL server.
    • For a Grid, you should use your database server name. You can find this in your Server Guide in the Database section.
    • Use your IP on your VPS server.
  • :3306 - Remote port 3306 will have your local port mapped onto it. 3306 is standard for MySQL.
  • is your standard SSH login, incorporating your username and server. See Connecting via SSH to your serverConnecting via SSH to your server for details.

Connect to MySQL

Now any MySQL commands that you run locally will be tunneled through SSH to your remote MySQL server. This includes commands in scripts and applications that you may have developed and any other programs that execute locally.

Here's a quick test command that you can run in a new tab in Terminal. Remember: Don't close your tunnel session. Make sure you replace the server name and username with your own information. The server name is listed after -h, and the username is listed after -u.

  • mysql -h -u db00000 -p

    Please see How do I create a database? and Database users on the Grid for help with your settings.

  • mysql -h -u admin -p`cat /etc/psa/.psa.shadow`


    Don't change the IP address. -h is specifying a local MySQL connection, which is what you want. See How do I create a database? for assistance with your username and password.


    The 'admin' user's password is a hashed version of the admin password used to login to the Plesk control panel. This hashed version can be found in /etc/psa/.psa.shadow

  • Enter your password at the prompt. You will now be connected to the remote MySQL server through the encrypted tunnel opened in the previous section.

Additional reading

Was this article helpful?
0 out of 0 found this helpful