php 警告:PDO::__construct(): [2002] 中没有这样的文件或目录(试图通过 unix:///tmp/mysql.sock 连接)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1819592/
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
Warning: PDO::__construct(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in
提问by johannes
My code was working all fine yesterday and today it suddenly just don't want to connect to my database. I have changed no settings on it or on the code and I haven't updated any software either. All I do is this:
我的代码昨天一切正常,今天突然不想连接到我的数据库。我没有更改它或代码上的设置,也没有更新任何软件。我所做的就是这样:
new PDO('mysql:host=localhost;port=3306;dbname=test', 'username', 'password');
And I get a nice exception message saying this:
我收到一条很好的异常消息,说:
Warning: PDO::__construct(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in ...
警告:PDO::__construct(): [2002] 中没有这样的文件或目录(试图通过 unix:///tmp/mysql.sock 连接)...
The thing is: I'm clearly not trying to connect using a unix socket but using TCP/IP. What am I doing wrong? Is there something I'm missing here?
问题是:我显然不是在尝试使用 unix 套接字进行连接,而是使用 TCP/IP。我究竟做错了什么?有什么我在这里想念的吗?
Thanks for any help.
谢谢你的帮助。
回答by johannes
You are using a Unix socket. When reading "localhost" MySQL client libraries don't interpret it as TCP host "localhost" and resolve that name but use the default Socket location. For using TCP on the local machine you have to use 127.0.0.1as hostname.
您正在使用 Unix 套接字。读取“localhost”时,MySQL 客户端库不会将其解释为 TCP 主机“localhost”并解析该名称,而是使用默认的 Socket 位置。要在本地机器上使用 TCP,您必须使用127.0.0.1主机名。
To specify the past use unix_socketinstead of hostin the DSN. The location of the socket used for localhostcan be defined at compile time or in some versions of PHP using pdo_mysql.default_socketin the php.ini.
指定过去使用unix_socket而不是host在 DSN 中。用于插座的位置localhost可以在编译时或在使用PHP的一些版本中定义pdo_mysql.default_socket的php.ini。
回答by Ilie Pandia
From the PHP documentation about connection to MySQL using PDO: PDO_MYSQL DNS
来自关于使用 PDO 连接到 MySQL 的 PHP 文档:PDO_MYSQL DNS
The note at the very end says:
最后的注释说:
Unix only:
When the host name is set to "localhost", then the connection to the server is made thru a domain socket. If PDO_MYSQL is compiled against libmysql then the location of the socket file is at libmysql's compiled in location. If PDO_MYSQL is compiled against mysqlnd a default socket can be set thru the pdo_mysql.default_socket setting.
仅限 Unix:
当主机名设置为“localhost”时,通过域套接字连接到服务器。如果 PDO_MYSQL 是针对 libmysql 编译的,则套接字文件的位置位于 libmysql 的编译位置。如果 PDO_MYSQL 是针对 mysqlnd 编译的,则可以通过 pdo_mysql.default_socket 设置来设置默认套接字。
So in order to fix this you would have to properly configure in php.ini the location of your mysql.sock
因此,为了解决这个问题,您必须在 php.ini 中正确配置 mysql.sock 的位置
Find your mysql.sock file. Common locations:
- /tmp/mysql.sock
- /tmp/mysql/mysql.sock
- /var/mysql/mysql.sock
- or if you use MAMP or LAMP look for inside the tmp folder for mysql
Edit your php.ini file and properly set the value for pdo_mysql.default_socket
Restart your Apache server to pickup the changes in the php.ini file
找到您的 mysql.sock 文件。常见地点:
- /tmp/mysql.sock
- /tmp/mysql/mysql.sock
- /var/mysql/mysql.sock
- 或者如果您使用 MAMP 或 LAMP 在 tmp 文件夹中查找 mysql
编辑您的 php.ini 文件并正确设置 pdo_mysql.default_socket 的值
重新启动 Apache 服务器以获取 php.ini 文件中的更改
回答by Danial
On Ubuntu, you can use this setting in php.ini
在 Ubuntu 上,您可以在 php.ini 中使用此设置
pdo_mysql.default_socket=/var/run/mysqld/mysqld.sock
回答by dean cameron
I just added this line:
我刚刚添加了这一行:
'unix_socket' => '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock',
and all was well.
一切都很好。

