This is a very old question about Unable to connect to AWS RDS through PDO, but I had the exact same issue and wanted to document it here for anyone who finds this later.
The Problem
- You can connect to your database (Amazon RDS) manually from the command line.
- You can connect to your database via mysqli in PHP.
- You can not connect to your database via PDO in PHP.
Amazon RDS is a managed database service. It’s responsible for most management tasks. By eliminating tedious manual tasks, Amazon RDS frees you to focus on your application and your users. We recommend Amazon RDS over Amazon EC2 as your default choice for most database deployments.
In the following table, you can find a comparison of the management models in Amazon EC2 and Amazon RDS.
The Solution
For me, after trying almost everything, I randomly decided to try and create a new database user. This worked and I was now able to connect via PDO.
This prompted me to investigate the issue a little further and I was able to narrow the issue down to a backslash \
character in my MySQL password.
There seems to be some kind of conflict between ENV Vars (with \
), PHP and PDO.
example:
$db_host = "username.xxx.ap-xxx.rds.amazonaws.com"; $db_user = "devninja"; $db_pass = "Justtest\$_"; #using slash before special character $db_name = "db_devninja";
hope this article Unable to connect to AWS RDS will help you 🙂