[英]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. 我正在尝试通过EC2实例中的PDO连接到我的MySQL RDS实例。 However the console (on all browsers) shows a 'status 500' error.
但是,控制台(在所有浏览器上)显示“状态500”错误。 Here is my code (checkit.php), as taken from their tutorial :
这是我的代码(checkit.php),摘自他们的教程 :
<?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);
删除
$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. (其他涉及
$dbh
删除了错误500警告,因此我知道这与PDO语句有关。
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. php --version
和5.6.17打印。 The PHP code above is AJAX'ed using this: 上面的PHP代码使用了AJAX:
$('#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. 导航到
http://<ip address>/checkit.php
返回错误500状态,因此我怀疑与AJAX无关,但是尽管如此,我还是发布了代码以防万一。
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. 这是由于PDO使用与RDS服务器不同的连接方法,默认情况下系统中不允许这样做。 Running the following should fix it:
运行以下命令可以解决此问题:
setsebool -P httpd_can_network_connect=1
There is some problem with PDO actually. PDO实际上存在一些问题。 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';
授予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 : 它给出了
ERROR 1045 (28000): Access denied for user 'dbadmin'@'%' (using password: YES)
而是给诸如以下权限:
GRANT SELECT ON * . * TO 'dbuser'@'111.11.111.11';
for all priviligies : SELECT
, INSERT
, DELETE
, UPDATE
, ALTER
, CREATE
对于所有特权:
SELECT
, INSERT
, DELETE
, UPDATE
, ALTER
, CREATE
refs : https://www.technlg.net/aws/create-mysql-user-aws-rds/ , https://stackoverflow.com/a/46589327/8255365 裁判: https ://www.technlg.net/aws/create-mysql-user-aws-rds/,https: //stackoverflow.com/a/46589327/8255365
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.