Tunnel to locally running mysql server using ssh


Running and administrating mysql can sometimes be a hassle especially if you’re running a semi-secure environment. This usually means that your mysql server will not accept connections from outside and only localhost connections are allowed. There is a quick way of getting around this if you’re stuck somewhere and really need to use that graphical admin/browser tool to get to your DB server. All you really need to do is forward port 3306 on your local machine to port 3306 on the DB server through a ssh tunnel. Here is the ssh command you need to issue to start things up:
ssh -L 3306:127.0.0.1:3306 yoursshloginid@yourserver.yourdomain.com
Once you supply the password for the ssh session you’re in business, the encrypted tunnel is up and running. All you need now is to point Mysql Administrator graphical tool at host 127.0.0.1 (localhost) and port 3306 like the picture below:The only thing you want to make sure you get right is the 127.0.0.1, DO NOT use localhost. The tools you’re using automatically assume a local socket connection to the DB when you use “localhost” as the Server Hostname. Another thing is that all checks that mysql administrator does locally on the server files will not work (ie: the interface will report that the server is down since it can’t find mysqld.pid), but all users/schema manipulation works fine since they are network based.

If you have mysql daemon installed on your local machine (the machine you initiated ssh from) you need to change the local port to something else other than 3306 and the command will look something like this:
ssh -L 7777:127.0.0.1:3306 yoursshloginid@yourserver.yourdomain.com
In this case I’m using local port 7777 which means I also have to tell mysql administrator to connect through port 7777. You get the idea……

, , ,

One response to “Tunnel to locally running mysql server using ssh”

Leave a Reply