Connecting MySQL Client to a Remote MySQL Server

Once in a while I need to connect to my remote MySql server using the gui tools. However, Secure Internet reachable MySql installations should be blocking the port. Luckily, SSH provides a way to connect to a remote MySql without exposing the MySql port to insecure connections…

Port Forwarding

Route your local port 3306 to the remote port 3306 on the remote host,

ssh -L 3306:localhost:3306 user@server.com -v

Connection

Now you can access the MySql instance just like you would if it were to be running on your local computer. For the MySql command line client,

mysql -h 127.0.0.1 -u <mysql_username> -d <db_name> -p

For the GUI tools, use 127.0.0.1 for the host name. Recent versions of the GUI tools makes this much easier as you can configure SSH connections directly in the GUI.