Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

alex29's avatar

How do I connect my app to my AWS RDS instance from my AWS EC2 instance?

I can't seem to figure out. When I connect to my RDS instance from either my local server or my EC2 instance itself using the mysql client, it works just fine. It only stops working when I try and run php artisan migrate. I've been going at this for a few hours now and I'm running out of ideas so if anyone can point out what I'm missing, I'd really appreciate it.

My .env file

DB_CONNECTION=mysql
DB_HOST=mydbidentifier.foo.myregion.rds.amazonaws.com
DB_PORT=3306
DB_DATABASE=mydb
DB_USERNAME=user
DB_PASSWORD=password

And the error I'm getting

Illuminate\Database\QueryException  : SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known (SQL: select * from information_schema.tables where table_schema = mydb and table_name = migrations and table_type = 'BASE TABLE')
0 likes
6 replies
rodrigo.pedra's avatar

I use the exact same configuration and it works fine. Might not be a PHP/Laravel configuration but a AWS configuration.

Is your RDS instance visible from your EC2 instance? Are they in the same vpc? Is there any security goups/policies that could prevent the connection?

To check if the RDS is accessible from the EC2 instance run the following command on the EC3 terminal (thorugh SSH):

mysql mydb -h mydbidentifier.foo.myregion.rds.amazonaws.com -u user -p

If it does not connect it is a network/security issue. If it does it might be a PHP problem, you could try reinstalling the php-mysql extension.

For reference:

https://aws.amazon.com/premiumsupport/knowledge-center/rds-cannot-connect/

alex29's avatar

hi there, yes that is exactly what i meant by i can connect to it using the mysql client and the weird thing is after a few hours i tried again without any change and it just worked all of a sudden

rodrigo.pedra's avatar

Weird, never had that problem before. Did you tried to connect to a newly created RDS instance? It might been provisioning while you tried to connect.

Either way, glad it worked.

1 like
alex29's avatar

it couldn't have been provisioning either since i was able to connect using the mysql client. it's really weird and it's really unsettling. i might just spin up a new ec2 and rds instance just to see if it happens again lol. anyway, thanks!

clem's avatar

I had the same error and it was caused by a wrong DB_HOST value.

It worked well through HTTP requests but not from the console because the RDS environnements variables were not set. So I updated my .env file with the proper key/values and it worked.

1 like
BrainyT's avatar

Check the security group to make sure the inbound rules are not limited to some IP addresses.

Please or to participate in this conversation.