简体   繁体   中英

Connecting to an Amazon RDS via an EC2 Instance with PHP PDO (Error 500)

I'm trying to connect to my MySQL RDS instance via a PDO from my EC2 Instance. However the console (on all browsers) shows a 'status 500' error. Here is my code (checkit.php), as taken from their tutorial :

<?php
$error = 0;
try {
    $dbhost = $_SERVER['RDS_HOSTNAME'];
    $dbport = $_SERVER['RDS_PORT'];
    $dbname = $_SERVER['RDS_DB_NAME'];

    $dsn = "mysql:host={$dbhost};port={$dbport};dbname={$dbname}";
    $username = $_SERVER['RDS_USERNAME'];
    $password = $_SERVER['RDS_PASSWORD'];

    $dbh = new PDO($dsn, $username, $password);

    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
catch (Exception $e){$error = $e->getMessage();}
echo $error;
?>

Removing the line $dbh = new PDO($dsn, $username, $password); (and the other lines involving $dbh removes the error 500 warning so I know it's to do with the PDO statement.

I've searched around a while and the questions that seem to be duplicates of this do not help me:

  • PHP is definitely installed: bash php --version and 5.6.17 prints.
  • PDO's come pre-installed in this version of PHP (I believe but I've not tested this)
  • I can SSH connect to the database meaning that
    • The credentials are correct
    • The security group is set up correctly

The PHP code above is AJAX'ed using this:

$('#user').on('submit', function(e) {
  e.preventDefault();
  $.ajax({
    url: "checkit.php",
    type: "POST",
    data: $(this).serialize(),
    success: function(data) {
      errors = data;
    }
  });
});

And this form (simplified):

<form id="user" action="" method="POST" target="_self" autocomplete="on" novalidate>
  <input name="create" type="checkbox">
  <button type="sumbit" name="submit">Submit</button>
</form>

Navigating to http://<ip address>/checkit.php returns the error 500 status so I doubt it's anything to do with AJAX, but nonetheless I've posted the code just in case.

Any ideas on where to begin?

This is due to PDO using a different connection method to the RDS server that by default is not allowed in the system. Running the following should fix it:

setsebool -P httpd_can_network_connect=1

There is some problem with PDO actually. I created a new user in my database and grand all privilages to it. After with those login credentials i was able to connect to database.

By the way you can't grand all priviliges GRANT ALL PRIVILEGES ON * . * TO 'dbuser'@'111.11.111.11'; GRANT ALL PRIVILEGES ON * . * TO 'dbuser'@'111.11.111.11'; like this. It gives ERROR 1045 (28000): Access denied for user 'dbadmin'@'%' (using password: YES) Instead give permissions like :

GRANT SELECT ON * . * TO 'dbuser'@'111.11.111.11';

for all priviligies : SELECT , INSERT , DELETE , UPDATE , ALTER , CREATE

refs : https://www.technlg.net/aws/create-mysql-user-aws-rds/ , https://stackoverflow.com/a/46589327/8255365

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM