Creating an SSH Tunnel

When connecting to MySQL sometimes the host does not allow for external connections to MySQL remotely for security reasons, but they do usually offer SSH access.

With SSH access you are able to create an SSH tunnel to your site/server and then connect to the port locally using your MySQL client of choice, mine being Sequel Ace on macOS.

In order to create the SSH tunnel, you would do so by opening your terminal and running the following command to create the tunnel.

ssh -L 3306:127.0.0.1:3306 [email protected]_ip -p ssh_port

After running the previous command via the terminal (I am personally a fan of iTerm2 over the default Terminal.app on macOS) you should now have a tunnel between your computer and the remote site/server.

Now that the tunnel has been created you can create a new connection in Sequel Ace as shown below to get connected to the database, of course filling out the proper database information.

Sequel Ace connection settings

This is of course the long way around it as Sequel Ace has an option built in to provide the SSH settings to bypass manually creating the tunnel, but the tunnel does open up a lot of doors when it comes to creating scripts that interact with the remote database via the command line.