Cannot connect RDS database with PDO

Cannot connect RDS database with PDO
0
try {
  $db = new PDO("mysql:host=xxxxx.us-east-1.rds.amazonaws.com;dbname=media;port=3306", USERNAME, PASSWORD);
  $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}  catch (Exception $e) {
    echo 'Unable to connect!';
    echo $e->getMessage();
    exit;
}

It is always showing Unable to connect!

I have already modified public access and added my IP in the RDS security inbound rule.

I would like to know what is the problem here? I want to connect to the database from my local computer and then try it with EC2 instance.

I can connect through the command line but I just can’t via PDO. Help would be appreciated!

1st answer I found on Google was having a \ character in the password:

I have no idea. But if you post the error message from the console it might help others with knowledge help better. Good luck!

That answer helped me. The error was
SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket

1 Like

Thanks for following up! I took a shot in the dark :rofl: glad it worked! But thanks again for posting the error code for the future people that search this error :slight_smile:

Happy coding!

Want add something more. If someone is looking at this in the future, you also need to comment out the pdo extension in the php.ini file.