从 Zend 教程开始 - Zend_DB_Adapter 抛出异常:“SQLSTATE[HY000] [2002] No such file or directory" [英] Starting with Zend Tutorial - Zend_DB_Adapter throws Exception: "SQLSTATE[HY000] [2002] No such file or directory"

查看:26
本文介绍了从 Zend 教程开始 - Zend_DB_Adapter 抛出异常:“SQLSTATE[HY000] [2002] No such file or directory"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始通过德语《Zend Framework in Action》一书学习 Zend Framework.

就在它开始变得有趣的地方,我的 PHP 单元测试抛出了这个错误:Zend_Db_Adapter_Exception: SQLSTATE[HY000] [2002] 没有这样的文件或目录"

我无法通过 Google 搜索找到任何提示.我所做的一切都像书中所说的那样.谁能给我一个关于在哪里寻找故障的提示?

这是初学者常见的错误吗?

解决方案

我会说你在从 PHP 连接到 MySQL 时遇到了问题...

PHP 之类的东西试图找到某个套接字文件,但没有找到,也许?
(我遇到过几次这个问题——不过不确定我得到的错误正是这个)


如果你正在运行一些基于 Linux 的系统,那么在某处应该有一个 my.cnf 文件,用于配置 MySQL -- 在我的 Ubuntu 上,它在 /etc/mysql/.

在这个文件中,可能有这样的东西:

socket =/var/run/mysqld/mysqld.sock


PHP 需要使用相同的文件——并且,根据您的发行版,默认文件可能与 MySQL 使用的文件不同.

在这种情况下,将这些行添加到您的 php.ini 文件可能会有所帮助:

mysql.default_socket =/var/run/mysqld/mysqld.sockmysqli.default_socket =/var/run/mysqld/mysqld.sockpdo_mysql.default_socket =/var/run/mysqld/mysqld.sock

(您需要重新启动 Apache 以便考虑对 php.ini 的修改)

最后一个对于 Zend Framework 使用的 PDO 应该足够了 -- 但前两个不会有任何危害,并且可以用于其他应用程序.


如果这没有帮助:您能否在另一个脚本中使用 PDO 连接到您的数据库,这完全独立于 Zend Framework?

即做这样的事情(引用):

$dsn = 'mysql:dbname=testdb;host=127.0.0.1';$user = 'dbuser';$password = 'dbpass';尝试 {$dbh = new PDO($dsn, $user, $password);} catch (PDOException $e) {echo '连接失败:'.$e->getMessage();}

如果不是,则问题肯定不是 ZF 的问题,而是 PHP 的配置/安装问题.

如果是...那么,这意味着您的 ZF 有问题,您需要向我们提供有关您的设置的更多信息(例如您的 DSN?)

I have started to learn Zend Framework with the Book "Zend Framework in Action" in German.

Right there where it starts to get interesting, my PHP Unit Test throws this Error: "Zend_Db_Adapter_Exception: SQLSTATE[HY000] [2002] No such file or directory"

I can't find any hints through Google searches. I did everything like it is in the book. Can anyone give me a hint as to where to search for the fault?

Is this a common beginner mistake?

解决方案

I would say that you have a problem connecting from PHP to MySQL...

Something like PHP trying to find some socket file, and not finding it, maybe ?
(I've had this problem a couple of times -- not sure the error I got was exactly this one, though)


If you are running some Linux-based system, there should be a my.cnf file somewhere, that is used to configure MySQL -- on my Ubuntu, it's in /etc/mysql/.

In this file, there might be something like this :

socket = /var/run/mysqld/mysqld.sock


PHP need to use the same file -- and, depending on your distribution, the default file might not be the same as the one that MySQL uses.

In this case, adding these lines to your php.ini file might help :

mysql.default_socket = /var/run/mysqld/mysqld.sock
mysqli.default_socket = /var/run/mysqld/mysqld.sock
pdo_mysql.default_socket = /var/run/mysqld/mysqld.sock

(You'll need to restart Apache so the modification to php.ini is taken into account)

The last one should be enough for PDO, which is used by Zend Framework -- but the two previous ones will not do any harm, and can be useful for other applications.


If this doesn't help : can you connect to your database using PDO, in another script, that's totally independant of Zend Framework ?

i.e. does something like this work (quoting) :

$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';

try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

If no, the problem is definitly not with ZF, and is a configuration / installation problem of PHP.

If yes... Well, it means you have a problem with ZF, and you'll need to give us more informations about your setup (like your DSN, for instance ? )

这篇关于从 Zend 教程开始 - Zend_DB_Adapter 抛出异常:“SQLSTATE[HY000] [2002] No such file or directory"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆