Support / KnowledgeBase

 
Search the KnowledgeBase Search

How can I tunnel a local MySQL server through SSH?

  • Applies to: (gs)

  • Difficulty: Medium

  • Time needed: 5 minutes

  • Tools needed: ssh

 

This article details how to tunnel MySQL connections through SSH. This is handy when trying to execute MySQL commands from a location other than your server or when developing applications remotely.


Step 1 - Set up the tunnel

First we must set up a local tunnel on a port so local commands will be passed through to the remote server. If you are running Windows or any other operating system that doesn't have SSH built in you should download a SSH utility that supports tunneling. We suggest Putty which has documentation on how to use it to forward port 3306.


On an OSX or *nix machine this command will work:

ssh -L 3306:<xxx.xxx.xxx.xxx>:3306 username@mt-example.com

Replace the fields surrounded by '<>' as follows:

  • xxx.xxx.xxx.xxx is the MySQL server you wish to execute commands on. If you are using a (gs) Grid-Service you can find this domain in the Server Guide under 'Database connections' (it will look something like internal-db.s#####.gridserver.com). If you are using a (dv) or (dp) product this is your server's IP address.
  • username@mt-example.com is the username you must provide for ssh access. If you have a (gs) Grid-Service this field will be something like 'serveradmin@mt-example.com'
  • mt-example.com is the domain of the server you wish to tunnel through. In most cases this will be the same as the machine that is hosting MySQL. If you have a (gs) Grid-Service this will be the domain name of your (gs)

Here are some sample commands:

  • Connect to a (gs) Grid-Service
    ssh -L3306:external-db.s#####.gridserver.com:3306 mt-example.com@mt-example.com
  • Connect to a (dv) Dedicated-Virtual Server
    ssh -L3306:xxx.xxx.xxx.xxx:3306 username@mt-example.com

Step 2 - Connect to MySQL

Now that the tunnel is open any applications on your desktop that try to connect to a local MySQL server will be forwarded through a tunnel to the remote server. This includes scripts that you may have developed or any other programs that execute locally. To test the connection on OSX or *nix you can use the following command:

mysql -h127.0.0.1 -udb##### -p

Replace db##### with your real username and provide a password when asked. If everything went well you should be connected to the remote MySQL server through the encrypted connection set up in step 1.

 

FAQs

How can I connect with Sequel Pro?

Please visit the following KnowledgeBase article: Connect to MySQL on the (gs) Grid-Service using Sequel Pro.


Revisions:

07-14-2009: Minor fixes

User Comments

No visitor comments posted. Post a comment

Fields marked with an asterisk(*) are required. Comment on this article

Fill out the form below if you would like to comment on this article.
 
 
 

(code is not case-sensitive)
 
Submit
 
 

Continue