无法通过PDO连接到AWS RDS [英] Unable to connect to AWS RDS through PDO
问题描述
我在AWS中设置了一个MySQL RDS实例.实例化实例后,我尝试从EC2的命令行连接该实例,并且该实例没有问题.但是,当我尝试通过PHP代码进行连接时,会遇到错误.下面是我尝试测试连接性的示例代码.
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')));
}
我得到{"outcome":false,"message":"Unable to connect"}
.我的原始代码使用的是 idiorm (使用PDO).与数据库连接的部分如下所示:
I get {"outcome":false,"message":"Unable to connect"}
. My original code is using idiorm (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')
更新
我更新了测试代码,以尝试使用mysql扩展名进行连接,并且它可以与mysql一起使用,但不适用于PDO
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";
现在,我正在尝试通过PDO连接时得到{"outcome":false,"message":"SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '\/var\/lib\/mysql\/mysql.sock' (2)"}
.我通过mysql的尝试是成功的.似乎问题出在PDO上.有什么我可以检查的吗?
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 ?
推荐答案
这是一个非常老的问题,但是我遇到了完全相同的问题,并希望在此记录,以便以后发现的人使用.
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.
问题
- 您可以从命令行手动连接到数据库(Amazon RDS).
- 您可以在PHP中通过mysqli连接到数据库.
- 您不能不通过PHP中的PDO连接到数据库.
- You can connect to your database (Amazon RDS) manually from the command line.
- You can connect to your database via mysqli in PHP.
- You can not connect to your database via PDO in PHP.
解决方案
对我来说,尝试了几乎所有内容之后,我随机决定尝试创建一个新的数据库用户.这有效,现在我可以通过PDO进行连接.
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.
这促使我进一步研究该问题,并且能够将问题缩小到MySQL密码中的反斜杠\
字符.
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.
ENV Vars(带有\
),PHP和PDO之间似乎存在某种冲突.
There seems to be some kind of conflict between ENV Vars (with \
), PHP and PDO.
这篇关于无法通过PDO连接到AWS RDS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!