Connecting to the MySQL database remotely (via an SSH Tunnel)

To make a connection to the database server from a remote machine you need to setup an SSH tunnel. Direct connections to the database server are firewalled for security reasons. The SSH tunnel provides a secure and authenticated means of talking to the database server.

The SSH tunnel maps a port on your machine to the same port on the database server.

Setting up an SSH tunnel using Putty

Startup Putty on your local machine.

Go to the Connection -> SSH -> Tunnels menu.

  1. In the section 'Port forwarding' set the source port to 3306
  2. Set the destination to 127.0.0.1:3306
  3. Check the 'Local' option.
  4. Click the 'Add' button.

Initialise an SSH connection to your web hosting account. Even if you are not using putty, the putty session needs to remain open to connect to the database.

Setting up an SSH Tunnel via the command line

From your terminal run the command:

ssh USERNAME@DOMAINNAME -L 3306:127.0.0.1:3306

Where:

  1. USERNAME is the username you use to connect to the server
  2. DOMAINNAME is your website's domain

Once this has been done you will be able to connect directly to the database as if it was running on your existing machine. To do this you will need to use the hostname localhost and the standard MySQL port 3306. In addition to this, you will also need your MySQL username, password and database name.

This can be found on the server by checking the .my.cnf file in your home directory.

If you require any assistance with this, please contact support at support@anchor.com.au or 1300 883 979.