Reputation: 5079
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 --version
and 5.6.17 prints.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?
Upvotes: 0
Views: 1155
Reputation: 588
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';
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
Upvotes: 0
Reputation: 11
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
Upvotes: 1