How to access MySQL through ssh tunnel

If you have already configured ssh to connect to the database from a particular client machine for database maintenance or other purposes, you can use the configuration described in this note to connect to the database via ssh from the Exploratory Desktop installed on that client machine.

If you do not have any of the above circumstances, we recommend that you set up an SSL port on the database and connect to the database with the port from Exploratory.

Generate SSH key

If you already connect to a MySQL server with MySQL Workbench through SSH, you already have the SSH key. So you can re-use it.

if it's not in a default location (i.e. ~/.ssh), you can add it from a Terminal (in case of mac) or from gitbash (in case of Win)

$ ssh-add <ssh_key_file_path>

Create a SSH tunnel

If the DB is running on SSH server

On your Terminal on Mac or Putty on Windows, use a ssh command with syntax explained below.

ssh -P {SSH_PORT} -N -L {LOCAL_PORT}:{LOCAL_HOST}:{DB_PORT} {SSH_OS_USER}@{SSH_HOST_WHERE_DB_IS_RUNNING)}

Let's assume the MySQL is running on example.com on port 3306 where it accepts SSH connection on port 20022. And you want to create a tunnel on your local machine (127.0.0.1) with port 3307.

So ssh command would look like this.

ssh -p 20022 -N -L 3307:127.0.0.1:3306 some_user@example.com

example:

If the DB is running on another server than SSH server

On your Terminal on Mac or Putty on Windows, use a ssh command with syntax explained below.

ssh -P {SSH_PORT} -N -L {LOCAL_PORT}:{DB_SERVER_HOST}:{DB_PORT} {SSH_OS_USER}@{SSH_HOST_WHERE_DB_IS_RUNNING)}

Let's assume the MySQL is running on db.example.com on port 3306 and a SSH server, which can connect to the DB, accepts a SSH connection on port 20022. And you want to create a tunnel on ssh.example.com with port 3307.

So ssh command would look like this.

ssh -p 20022 -N -L 3307:db.example.com:3306 some_user@ssh.example.com

Create a Database Connection on Exploratory Desktop

From Project Header Menu, select Data Connections.

Click Add button.

Select MySQL like below.

Enter 127.0.0.1 to the Host and 3307 (which is used to create a ssh tunnel) for Port. Enter Database name and Username and Passwords, if required.