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 [email protected] -L 3306:remote-mysql.mydomain.com:3306 -N

Save the pid as an environment variable:

$ mysql_tunnel_pid=$(ps aux | grep '3306:remote-mysql.mydomain.com:3306' | grep -v grep | awk '{print $2}')

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

$ mysql -u user -p -h 127.0.0.1
mysql> 

Once you are done, close the ssh tunnel:

$ kill ${mysql_tunnel_pid}