87156

Unable to connect to AWS RDS through PDO

Question:

I set up a MySQL RDS instance in AWS. After instantiating the instance I tried to connect it from my EC2's command line and it works with no issue. However, when I try connecting through my PHP code I get erros. Below is the sample code that I tried to test my connectivity.

<?php try{ $dbh = new pdo( 'aws-test-db.hibizibi.us-west-2.rds.amazonaws.com;dbname=test', 'root', 'password', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); die(json_encode(array('outcome' => true))); } catch(PDOException $ex){ die(json_encode(array('outcome' => false, 'message' => 'Unable to connect'))); }

I get {"outcome":false,"message":"Unable to connect"}. My original code is using <a href="http://idiorm.readthedocs.io/en/latest/" rel="nofollow">idiorm</a> (which uses PDO). The portion that connects with database looks like below:

# configure ORM ORM::configure('mysql:host='.DB_SERVER.';dbname='.DB_NAME); ORM::configure('username', DB_SERVER_USERNAME); ORM::configure('password', DB_SERVER_PASSWORD);

From the above I get the below error trace:

SQLSTATE[HY000] [2005] Unknown MySQL server host 'aws-test-db.hibizibi.us-west-2.rds.amazonaws.com:3306' (11) #0 /var/www/html/main/admin/applications/vendors/idiorm/idiorm.php(255): PDO->__construct('mysql:host=aws-...', 'root', 'password', NULL) #1 /var/www/html/main/admin/applications/vendors/idiorm/idiorm.php(237): ORM::_setup_db('default')

<em>UPDATE</em>

I updated my test code to try connecting using mysql extension and it worked with mysql but not PDO

<?php $servername = "aws-test-db.hizibizi.us-west-2.rds.amazonaws.com"; $username = "root"; $password = "password"; $dbname='test'; try{ $dbh = new pdo( 'mysql:'.$servername.';dbname='.$dbname.';', $username, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION) ); die(json_encode(array('outcome' => true))); } catch(PDOException $ex) { echo json_encode(array('outcome' => false, 'message' => $ex->getMessage()))."\n"; } // Create connection $conn = mysqli_connect($servername, $username, $password); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully\n";

Now, I am getting {"outcome":false,"message":"SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '\/var\/lib\/mysql\/mysql.sock' (2)"} while trying t connect through PDO. My attempt through mysql is successful. Seems like the problem is specifically with PDO. Is there anything I can check ?

Answer1:

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.

<strong>The Problem</strong>

<ol><li>You can connect to your database (Amazon RDS) manually from the command line.</li> <li>You can connect to your database via mysqli in PHP.</li> <li>You can <strong>not</strong> connect to your database via PDO in PHP.</li> </ol>

<strong>The Solution</strong>

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.

Recommend

  • get checkbox in form element
  • How to access parsed C++11 attributes via clang tooling
  • Mapping words to numbers with respect to definition
  • Jenkins CopyArtifact step - Unable to find project for artifact copy
  • How to Force Docker to release storage space after manual delete of file in volumes and containers?
  • ODBC connection to an .accdb file
  • Pass multiple lines of stdin input to interactive Java command line program, non-interactively
  • plot dirac function in matlab
  • Corda: How to implement hierarchical relationships between state data persisted to H2
  • didSelectItemAtIndexPath of UICollectionView inside of a UIScrollView is not getting called
  • Simultaneous animation when entering editing mode of UITableViewCell
  • jQuery Ajax call to WCF service returning “Method not allowed (405)”
  • Firestore: Version history of documents
  • Circular Left Rotation Algorithm in C#
  • Keep rows with certain values always at the bottom while sorting in jquery tablesorter plugin
  • Calculate time from document
  • Conflicting declaration using constexpr and auto in C++11
  • Amazon Elastick BeanStalk error: Failed to create the AWS Elastic Beanstalk application version
  • DataTables move rows between tables
  • How to specify generic type when the type is only known at runtime?
  • How to change user identity when git pushing via ssh?
  • read part of h5 dataset python
  • Debug `Unexpected end of JSON input Error` on content script
  • Functions by reference or by variable, which to use when?
  • Update cell query for Excel ADO from Delphi
  • Why my AngularJS async test in Jasmine 1.3.x is not working?
  • Capture SIGFPE from SIMD instruction
  • How to call different template for different category archive page in woocommerce
  • customize soft keyboard key preview
  • Using Service Component Runtime
  • Sql - ON DUPLICATE KEY UPDATE
  • How to use FirstOrDefault inside Include
  • How do I use TagLib-Sharp to write custom (PRIV) ID3 frames?
  • WPF custom control and direct content support
  • PHP Permalinks.. how to change?
  • media foundation H264 decoder not working properly
  • CAS 4 - Not able to retrieve the LDAP groups after successful authentication
  • Running R's aov() mixed effects model from Python using rpy2
  • Access to a Matlab gui from the web