连接到远程MySQL服务器时PDO创建失败

时间:2014-04-15 23:56:33

标签: php mysql sql-server exception pdo

我的朋友和我正在一个网站上工作,我正在尝试在我的机器上安装它的构建。它已经在服务器上运行,我似乎无法从我的本地机器连接。当我这样做时,我收到以下错误:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [1045] 
Access denied for user 'myusername'@'adsl-client.example.net' (using password: YES)' in /Users/myname/Sites/mysitefolder/req/connect.php:25 
Stack trace: #0 /Users/myname/Sites/mysitefolder/req/connect.php(25): PDO->__construct('mysql:host=mysq...', 'myusername', 'mypassword') 
#1 /Users/myname/Sites/burn-after-reading/inc/header.php(9): ConnectionFactory->getConnection() 
#2 /Users/myname/Sites/burn-after-reading/index.php(79): include('/Users/myna...') 
#3 {main} thrown in /Users/myname/Sites/mysitefolder/req/connect.php on line 25

这是我尝试连接的文件,connect.php

<?php

class ConnectionFactory
{
    var $dbhost = 'mysql.mysitename.io';
    var $dbname = 'my_database_name';
    var $dbusername = 'myusername';
    var $dbpassword = 'mypassword';

    private static $factory;
    public static function getFactory()
    {
        if (!self::$factory)
            self::$factory = new ConnectionFactory();
        return self::$factory;
    }

    public function getConnection() 
    {
        $conn = new PDO("mysql:host={$this->dbhost};dbname={$this->dbname}", 
            $this->dbusername,
            $this->dbpassword);
        return $conn;
    }

    public function getConnectionNoName()
    {
        $connNoName = new PDO(
            "mysql:host={$this->dbhost}", 
            $this->dbusername,
            $this->dbpassword
        );
        return $connNoName;
    }
}

1 个答案:

答案 0 :(得分:-1)

出于安全原因,您需要将IP地址添加到允许的远程mysql连接列表中。