Home » Unable to connect to AWS RDS through PDO

Unable to connect to AWS RDS through PDO

  • by

This is a very old question, but I had the exact same issue and wanted to document it here for anyone who finds this later.

The Problem

  1. You can connect to your database (Amazon RDS) manually from the command line.
  2. You can connect to your database via mysqli in PHP.
  3. You can not connect to your database via PDO in PHP.

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 helps

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *

eight − four =