Connect to MySQL on Amazon RDS from local machine

Max Kimambo
Thoughts of a software fundi
1 min readJan 27, 2017

For security reasons you should not have your database available to the internet just protected by some password, it should be available only from within your VPC.

But there are times when you would like to connect to MySQL hosted on RDS remotely.

In this case you can create a tunnel through your bastion host and access the db.

The process is as follows.

First setup the tunnel

In your .ssh/config I assume you have put up your identity key and configuration for accessing the bastion like so.

```
host bastion
hostname 62.59.xx.xx
User ubuntu
Port 22
IdentityFile ~/.ssh/my_key.pem
```

Then setup the tunnel
```
ssh -N -L 3306:mysql.rds.host.aws:3306 ubuntu@bastion
```
Breaking it down

First 3306 port — is local port that will be forwarded for connection to remote host.
Then the host your are forwarding the connection to
and lastly you need to ssh to the host through which this tunnel will be setup.

After this is done, open a second terminal and

```
mysql -h 127.0.0.1 -u username -pMypa55
```
This will connect you to the locally available port to the remote server.

Use the local ip instead of localhost as mysql will use the socket connection by default when connecting to localhost.

--

--