无法从PHP脚本连接到AWS数据库

时间:2016-08-09 01:08:51

标签: php mysql amazon-web-services pdo tcp

我正在尝试连接到我创建的aws rds。我可以使用MySQL Workbench连接没有问题,但不能从我上传到服务器的PHP脚本连接。我觉得这与权限有关,但我不确定。有人能指出我正确的方向。下面是我正在使用的PHP代码

<?php
//Database configuration

$dbhost = $_SERVER['https://address.to.aws.rds/rds/home?region=us-west-2'];
$dbport = $_SERVER['3306'];
$dbname = $_SERVER['test_db_name'];

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

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

我上传并运行此脚本时收到的错误是

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[28000] [1045] Access denied for user 'root'@'localhost' (using password: NO)' in /home2/betamath/public_html/graspe/db.php:12 Stack trace: #0 /home2/betamath/public_html/graspe/db.php(12): PDO->__construct('mysql:host=;por...', NULL, NULL) #1 {main} thrown in /home2/betamath/public_html/graspe/db.php on line 12

第12行是$dbh = new PDO($dsn, $username, $password);

我为aws rds设置的权限是

HTTP            TCP      80      0.0.0.0/0

SSH             TCP      22      0.0.0.0/0

MySQL/AURORA    TCP      3306    0.0.0.0/0

HTTPS           TCP      443     0.0.0.0/0

再次,非常感谢任何帮助。感谢

1 个答案:

答案 0 :(得分:2)

尝试使用:

$dbname = "database_name";
$dbuser = "database_user"; 
$dbpass = "database_password"; 
$dbhost01 = "user.regioninfo.rds.amazonaws.com"; // did not use http or https; info provided from AWS
$dbport = 3306;

$dbh = new PDO("mysql:host=$dbhost01;dbname=$dbname", $dbuser, $dbpass);