Linux 未捕获的异常 'PDOException' 带有消息 'SQLSTATE[HY000] [2003] 无法连接到 MySQL 服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3677004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught Exception 'PDOException' with message 'SQLSTATE[HY000] [2003] Can't connect to MySQL server
提问by Paula
I am using Zend Framework and my application works on localhost but produces the can't connect to MySQL server error when I'm trying to connect to the database I've uploaded on a live server. I've tried handling exceptions and Zend_Exception catches it("perhaps factory() failed to load the specified Adapter class"). I've emailed the webmaster and he told me that Phpmyadmin is working fine so there should be no problem with connecting php to mysql. What else can I do? The lines that produce the error are the following:
我正在使用 Zend Framework,我的应用程序在 localhost 上运行,但是当我尝试连接到我上传到实时服务器上的数据库时,会产生无法连接到 MySQL 服务器的错误。我尝试过处理异常并且 Zend_Exception 捕获了它(“也许 factory() 未能加载指定的适配器类”)。我给网站管理员发了电子邮件,他告诉我 Phpmyadmin 工作正常,因此将 php 连接到 mysql 应该没有问题。我还可以做些什么?产生错误的行如下:
$db = Zend_Registry::get('db');
$sql = 'SELECT * FROM department';
$result = $db->fetchAll($sql);
Is there something specific I can ask the webmaster to look at?
有什么具体的东西我可以请网站管理员查看吗?
EDIT:
编辑:
in my bootstrap:
在我的引导程序中:
public function _initDB()
{
$dbOptions = $this->getOption('db');
$db = Zend_Db::factory($dbOptions['adapter'], $dbOptions['params']);
Zend_Registry::set('db', $db);
Zend_Db_Table_Abstract::setDefaultAdapter($db);
}
in my application.ini:
在我的 application.ini 中:
db.adapter = PDO_MYSQL
db.params.host = agila.upm.edu.ph
db.params.username = FacultyDB
db.params.password = *********
db.params.dbname = FacultyDB
回答by Iznogood
Since you are convinced the settings are ok in application.ini are you initialising your db in bootstrap?
既然您确信 application.ini 中的设置没问题,您是否在引导程序中初始化您的数据库?
protected function _initDb() {
$registry = Zend_Registry::getInstance();
$db = Zend_Db::factory($registry->config->db->adapter,
$registry->config->db->database->toArray());
Zend_Db_Table::setDefaultAdapter($db);
Zend_Registry::set('db', $db);
}
try that in application.ini:
在 application.ini 中试试:
[db]
adapter = PDO_MYSQL
database.host = agila.upm.edu.ph
database.username = FacultyDB
database.password = *********
database.dbname = FacultyDB
Try configuring with that and my bootstrap code and see if it now works. I remember something about zend_db being picky about configuration from application.ini. I could be wrong its vague.
尝试使用它和我的引导代码进行配置,看看它现在是否有效。我记得 zend_db 对 application.ini 中的配置很挑剔。我可能错了它的含糊不清。