Accessing MySQL using a SSH-Tunnel
Due to security aspects MySQL server often only listen on localhost and accessing them remotely is not possible. With a simple trick you can access them with your favourite tool aswell.
ssh -l login_name -N -L [bind_address:]port:host:hostport remoteServer
login_name is your username on the remote machine
bind_address is the local interface’s address where the tunnel should bind to
port is the local port the tunnel should bind to
host is the remote host to which you want to connect
hostport is the remote port to which you want to connect
remoteServer is the server using which you want to etablish the tunnel
-N should disable starting the login shell
So for creating a tunnel for MySQL the command looks like this:
ssh -l user-remote -N -L 123456:localhost:3306 remoteServer
This create a tunnel to MySQL running on remoteServer. You are able to access MySQL on port 123456 on localhost. For example you can use the following command:
mysql --port=123456 -h 127.0.0.1 --user=mysqlUser --password=mysqlPassword database
June 4th, 2008 at 9:48 am
Thanks so much, that’s a wonderful trick! Talking of which, you might want to fix the invalid port 123456 in your example