SSH tunneling to an RDS MySQL server from Rails App

by: Peter Shoukry

I recently needed to connect my local development machine to an RDS MySQL staging server to do some debugging.
The setup consisted of a multi-hop ssh tunnel to get to the frontend and then connect to the MySQL server from the frontend server.
## First things First…
The first step was to actually create the tunnel to the front end and to do that edit ~/.ssh/config to add the connection details as follows
```config
Host intermediate User <myuser>
HostName intermediate.domain.com
ForwardAgent yes Host frontend
User <myuser>
ProxyCommand ssh intermediate nc <frontend_ip> 22
Localforward 3306 mysql.domain.com:3306
ForwardAgent yes
```
With the current setup you can connect to your local 3306 port as if you are connecting to your MySQL server.
I usually have all my development environment set up using docker.
So, a cleaner approach, would be to have a docker container act as a simple proxy server listening on port 3306 and forwarding all traffic to the remote server.
To do that:
1. copy the following two files to your .ssh folder
Config
```config
Host intermediate
User <myuser>
HostName intermediate.domain.com
ForwardAgent yes
Host frontend
User <myuser>
ProxyCommand ssh intermediate nc <frontend_ip> 22
Localforward 3307 mysql.domain.com:3306
ForwardAgent yes
```
Dockerfile
```yaml
FROM buildpack-deps:jessie
ADD . /root/.ssh
RUN chown -R root:root /root/.ssh
RUN apt-get update
RUN apt-get install -y mysql-client socat
expose 3306
CMD socat tcp-l:3306,fork,reuseaddr tcp:127.0.0.1:3307 & ssh -N frontend
```
2. Build and start the docker container