How To Connect To MySQL Using SSH Tunnel

What is SSH?
Secure Shell or SSH is a network protocol that allows data to be exchanged using a secure channel between two networked devices. Used primarily on Linux and Unix based systems to access shell accounts, SSH was designed as a replacement for TELNET and other insecure remote shells, which sent information, notably passwords, in plaintext, leaving them open to interception.   The encryption used by SSH provides confidentiality and integrity of data over an insecure network, such as the Internet.  For additional information see http://en.wikipedia.org/wiki/Secure_Shell

What do we need SSH for?

Basically we use SSH to login to a secure server on the internet and provide a pipe from your system to a port on the server so you appear to be accessing the port from the server it's self. Connecting to the SSH server first gets you connected to the server and encrypts all traffic so if it is intercepted it cannot be read.  In the process of connecting the client sets up a tunnel from a post on your local system to a port on the server. With SSH Tunneling you can use applications locally and access MySQL on the server as if it was installed on your local system. 

What you need.

  1. If you do not already have one on your home system pick download and install an SSH Client.  Most people and this document use PuTTY.  PuTTY can be download from the link below:

http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

  1. You will also need a login to the SSH server which you can get from your systems administrator or hosting company. The things you will need are:

 

Setting up Putty your ssh client

When you first run Putty you will be presented with the session manager screen.  

Putty Session

Under hostname put the URL or IP address of your server and under Saved Sessions put something that will tell you what this session connects to such as Office or Web Server.

Putty
            ssh category

Next under the lowest sub heading Connection click the plus sign next SSH .  This will open an additional list of Sub-Categories.  Select Tunnels from this sub-category under SSH.

 
Putty Tunnels

To add a tunnel to connect to mysql (default port 3306) from your local system to the server so that it appears to come from the server on localhost you will enter 3306 in the box next to Source port and localhost:3306 under Destination.


Putty with mysql tunnel

Once you click Add you should see the information on the forwarded port showing in the white block labeled Forwarded Ports:.




Finally scroll back up in the Category list and click back on Session and then select SAVE to save the settings to be used with this session.

Putty Save Session