Connecting to a RDS Server from a Local Computer Using SSH Tunneling on a Mac
Solution
If you scour the internet there appear to two main solutions.
- Add your IP address to a whitelist on rds, but could be problematic since your IP address will probably change.
- Connect to the RDS instance using SSH tunneling.
I had not set up SSH tunneling before and for some reason had a hard time tracking it down. I use sequl pro to inspect our database sometimes, and I realize I was able to connect using SSH tunneling via their gui interface.
So, what is the solution?
There are two steps:
- Set up the SSH Tunnel
ssh -N -L 3306:your.rds.endpoint.rds.amazonaws.com:3306 sshuser@yourserver.com
-N only set up the tunnel
-L set up the forwarding
3306
that first number is the port on your local machine
your.rds.endpoint.amazonaws.com The name of the rds endpoing
3306 the port on the remote computer
sshuser@yourserver.com how you log in to your ec2 instance
2. Use the SSH Tunnel
mysql -u dbuser -p -h 127.0.0.1
This lets you connect to the remote rds instance. Note that you have to use the host here 127.0.0.1 explicitly and that it is not the host you set up earlier. This is because it is now forwarding all of the requests. That’s all.
To be clear on how the ports work, here is another example
ssh -N -L 1234:your.rds.endpoint.rds.amazonaws.com:3306 sshuser@yourserver.com
mysql -u dbuser -p -h 127.0.0.1 -P 1234
This says forward from port 1234 on my computer to port 3306 on the remote instance. I just used 3306 in both as the defaults.
No comments:
Post a Comment