使用PDO

时间:2015-06-22 04:29:53

标签: php mysql database apache pdo

我有一个关于从我的网站使用远程服务器数据库的快速问题。 我的问题是我使用HieidiSQL中的密码,数据库,用户名和主机连接到数据库没有问题,但是当我使用PDO它会抛出一个错误,所以我可以远程连接,但是我很难接受这点。 错误抛出(清理以便于阅读):

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] Connection timed out' in /customers/0/1/9/doxramosgaming.org/httpd.www/functions.d/db/db.class.php:36
Stack trace:
#0 /customers/0/1/9/doxramosgaming.org/httpd.www/functions.d/db/db.class.php(36): PDO->__construct('mysql:host=104....', user', 'mypassword')
#1 /customers/0/1/9/doxramosgaming.org/httpd.www/functions.d/db/db.class.php(47): db->__construct()
#2 /customers/0/1/9/doxramosgaming.org/httpd.www/config.php(20): require_once('/customers/0/1/...')
#3 /customers/0/1/9/doxramosgaming.org/httpd.www/index.php(2): require_once('/customers/0/1/...')
#4 {main}
  thrown in /customers/0/1/9/doxramosgaming.org/httpd.www/functions.d/db/db.class.php on line 36

尝试解决:

GRANT ALL ON *.* TO myuser@'%' IDENTIFIED BY 'mypassword';
FLUSH PRIVILEGES;
sudo service mysql restart

的my.cnf

bind-address = 0.0.0.0

请注意,它未设置为127.0.0.1且未注释,因此请不要告诉我注释掉绑定地址。我可以通过我的PC上的HeidiSQL连接,告诉我已经允许远程连接。

连接文件:

class db extends pdo{
//Website Variables

public $sitedb = '';
public $siteconfig;
public $sitesettings = array(
    'host'      => _SITEHOST,
    'database'  => _SITEDB,
    'username'  => _SITEUSER,
    'password'  => _SITEPASS,
);
public $realmdb = '';
public $realmconfig;
public $realmsettings = array(
    'host'      => _REALMHOST,
    'database'  => _REALMDB,
    'username'  => _REALMUSER,
    'password'  => _REALMPASS,
);
public function __construct(){

    $this->sitedb = new PDO(
        "mysql:host={$this->sitesettings['host']};" .
        "dbname={$this->sitesettings['database']};" .
        "charset=utf8",
        "{$this->sitesettings['username']}",
        "{$this->sitesettings['password']}"
    );
     $this->realmdb = new PDO(
        "mysql:host={$this->realmsettings['host']};" .
        "dbname={$this->realmsettings['database']};" .
        "charset=utf8",
        "{$this->realmsettings['username']}",
        "{$this->realmsettings['password']}"
    );


    $this->sitedb->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    $this->realmdb->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

}


}

$db = new db();

密码和所有内容都在我的配置文件中设置,该文件加载db.connection文件。它可以在我的本地服务器上运行,而不是在我上传到我的网络服务器时。 所有数据库名称,密码等都是正确的,因此它不是问题。

正如另一项测试一样,我将mysql从我的家用电脑运行到服务器,并且它在瞬间连接没有任何问题。我不明白这里的问题是什么。

我修改了我的连接设置,看看我是否能找到发生了什么

try {
         $this->realmdb = new PDO(
            "mysql:host={$this->realmsettings['host']};" .
            "dbname={$this->realmsettings['database']};" .
            "charset=utf8",
            "{$this->realmsettings['username']}",
            "{$this->realmsettings['password']}"
        );
        }
        catch(PDOException $e) {
            echo "PDO Realm Failure:";
        }

编辑后我给出了:

  

PDO Realm Failure:致命错误:调用成员函数   字符串中的setAttribute()   /customers/0/1/9/doxramosgaming.org/httpd.www/functions.d/db/db.class.php   第49行

即使它在我的本地服务器上工作,它是否可以出于某种原因以这种方式构建数据库连接?

刚刚找到一些相关信息。当我连接到领域服务器并在本地运行它一切正常,所以它必须在我的网络主机,但我不知道在哪里看。

0 个答案:

没有答案