I set up RDS (mysql) with Elastic Beanstalk/EC2 on AWS for my PHP API.

The API is reachable through http and https, but it seems the connection to the DB is having troubles, as I'm getting error:

"SQLSTATE[HY000] [2002] Connection timed out","error_data":

What I've checked:

  • My PHP app is load balanced, with a SSL cert attached, and served through https through cloudfront. The security group assigned to this EC2 instance is the same as I'm using for RDS, with inbound rules that accept http connection (it's also attached to the default RDS security group which allows it to be in authorized state)

enter image description here

  • RDS instance in authorized state given its security groups: enter image description here

  • I can connect to the DB instance through my credentials in sequel pro

  • I've double checked my elastic beanstalk env variables, which are the same as I used to log in successfully to sequel pro on the server

I checked the server logs and am not getting much more detail about why I cannot connect.

EDIT: added inbound settings for MYSQL

enter image description here

  • Can you connect to RDS from an EC2 instance started by EB? The security group that contains RDS must allow connections on the appropriate ports, which are labeled in the security group settings - eg MySQL. Have a look here.
    – Tim
    Apr 19 '17 at 5:16
  • @Tim ah so on the inbound settings, I need to add MYSQL as shown above in the added picture?
    – Growler
    Apr 19 '17 at 5:22
  • @Tim I've gone through your setup process (thanks for the link). Two things: 1) I've added the inbound settings for port 3306. 2) My VPC w/ subnets were set up for me when I created Elasticbeanstalk instance with RDS. Still getting the same SQL connection error
    – Growler
    Apr 19 '17 at 5:38
  • @Tim solved: had the port as part of the env variable for the DB host string :/
    – Growler
    Apr 19 '17 at 5:45
  • Ah I did a couple of days ago myself. Suggest you answer your own question, it may help someone out. Give a bit of detail, or link to docs.
    – Tim
    Apr 19 '17 at 9:07

My advice is as follows:

1- Note down the username, password and the hostname that is the endpoint of the RDS instance that you have created.

2- Assign the security groups and VPCs to your RDS that you will assign to the environment in your Elastic Beanstalk app.

3- In your EB environment, go to the configurations tab if the environment is already deployed. If it's not deployed you can go to the configuration options that lies at the bottom of the page while you are creating the environment. Then go to the software option. The hierarchy is as follows. Environment -> Configurations -> Software Down there in the page you will have an option for adding the environment variables in the form of key-value pairs there you have to add the following tags:

DB_HOST = You RDS endpoint

DB_NAME = You db name that you have given at the time of DB creation

DB_PASS = You password that you have given at the time of DB creation

DB_PORT = You port that you have specified in most cases it is 3306 for mysql.

DB_USER = You username that you have given at the time of DB creation

Save the configurations and run the environment, hopefully this will solve the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.