This is a quick post on how to access your remote MySQL server, which only allow MySQL connections from our bastion host and we would like to access that MySQL via the Bastion Server with a SSH Tunnel from our workstation (or any other source)

Setup the SSH Tunnel

Establish the tunnel:

$ ssh -f -p 22 -L -N

Save the pid as an environment variable:

$ mysql_tunnel_pid=$(ps aux | grep '' | grep -v grep | awk '{print $2}')

Now that our tunnel has been established, access mysql via the ssh tunnel:

$ mysql -u user -p -h

Once you are done, close the ssh tunnel:

$ kill ${mysql_tunnel_pid}