未捕获的PDOException:PDO :: __ construct():php_network_getaddresses:getaddrinfo failed:名称或服务未知

时间:2017-01-19 18:05:05

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

在我的AWS EC2服务器中,我有两个项目:

  1. 连接此RDS DB的Laravel Project。我没有连接它的pb
  2. Wordpress Project连接他自己的数据库(也在RDS中),也在插件中,我添加了一个自定义连接到我的Laravel数据库,所以我可以轻松插入数据。
  3. 当我在localhost中时,此代码有效,但是当我放入真正的主机时,它失败了:

    $capsule->addConnection(array(
     'driver' => 'mysql',
     'host' => 'kendozone.cp90iogazqzc.us-west-2.rds.amazonaws.com',
     'database' => 'mydb',
     'username' => 'myuser',
     'password' => 'mypas',
     'charset' => 'utf8',
     'collation' => 'utf8_unicode_ci',
     'prefix' => 'ken_',
     'port'  => '3306'
    

    ));

    这是错误消息:

    Uncaught PDOException: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known in ...
    

    我错过了什么????

1 个答案:

答案 0 :(得分:2)

<?php
$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);
?>

您错过了该端口。 AWS REFERENCE