Ubuntu: MySQL SSH Tunnel via the Terminal CodeUnit 04 FEB 2011

If you have a closed machine that doesn’t allow MySQL connections from the outside world but does allow SSH access in to the box, say hello to the world of SSH tunneling.

The concept is simple. Bind one of your local machine’s port via a SSH connection to another box on the outside. For example:

ssh -N -L 3307: userName@remoteHost &

The above is simply linking the remote machine’s MySQL server on port 3306 to your local machine’s port 3307. In other words, you can now connect to the MySQL server on the remote machine by connecting to localhost:3307.


Related Posts:

About Craig Lotter

Software developer, husband and dad to two little girls. Writer behind An Exploring South African. I don’t have time for myself any more.

  • gringo guy

    Hey, thanks for this. Nifty indeed. After playing with this some tonight, a couple of notes.

    1. my ISP hides the DB off on another server that can only be accessed after creating the ssh tunnel, so I have to use the IP or hostname of the DB server like this for it to work:

    ssh -L 3307:DB_SERVER:3306 userName@remoteHost &

    2. also, I’m using Ubuntu, I have PuTTY installed, and I’m using the Windows version of PasswordSafe running in Wine. I added a symlink in the Wine c: dir to /usr (some say this is a security issue) which allows me to enter a ‘Run Cmd’ entry in Password Safe’s ‘Additional’ tab like so:

    c:usrbinputty -l ${u} -pw $p -L 3307:DB_SERVER:3306 remoteHost

    This way, I simply click on the PSafe entry, click the ‘run’ icon (or right-click the entry and select ‘Run’) to open a terminal window and create the ssh tunnel. The user and password are added from the PSafe entry.

    So now, after using either tunneling method, both MySQL Admin and Query Browser work like a charm, and I can easily backup my data base to my local PC. Tonight was the first time ever, since my ISP makes DB admin so very hard to do.

    • Thanks for the extra bit of info, sure it will come in handy for guys sitting in the same situation! :)