More information about the Underscore mailing list

[_] Accessing hosted MySQL databases

James Fidell james at cloud9.co.uk
Thu Jun 5 10:05:19 BST 2008

Jason Nevin wrote:

> An SSH tunnel is like a VPN right? Do hosting companies normally setup such things with their clients?

Not really quite the same thing.

SSH has a feature that allows you to forward connections on a local
machine to a remote one (and vice-versa) and deliver the data to an
address from there.  To make it work for mysql I'd probably do something
like this (on Linux):

   ssh -N -L 3306:localhost:3306 user at server.example.com

to redirect port 3306 on the local machine to localhost:3306 on the
remote system.  It should then be possible to do

   mysql -h 127.0.0.1 -u <user> ...

and connect to the remote mysql server.  (mysql -h localhost tries to
open the named pipe to the server, iirc, rather than a TCP connection,
so you need to use the address instead.)

If you're already running mysql locally as well then you won't be able
to bind to port 3306 because it'll be in use, so you have to use a
different one, say 3307:

   ssh -N -L 3307:localhost:3306 user at server.example.com

and then connect using the mysql client:

   mysql -h 127.0.0.1 -P 3307 -u <user> ...

James