警告:PDO :: __ construct():[2002]在以下目录中没有这样的文件或目录(试图通过unix:///tmp/mysql.sock进行连接) [英] Warning: PDO::__construct(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in

查看:54
本文介绍了警告:PDO :: __ construct():[2002]在以下目录中没有这样的文件或目录(试图通过unix:///tmp/mysql.sock进行连接)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我的代码运行良好,今天突然不希望连接到我的数据库.我没有更改任何设置,也没有更改代码,也没有更新任何软件.我要做的就是这个:

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:

警告:PDO :: __ construct():[2002]在...中没有这样的文件或目录(试图通过unix:///tmp/mysql.sock连接)

Warning: PDO::__construct(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in ...

问题是:我显然不是在尝试使用unix套接字进行连接,而是在使用TCP/IP.我究竟做错了什么?这里有我想念的东西吗?

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套接字.在读取"localhost"时,MySQL客户端库不会将其解释为TCP主机"localhost"并解析该名称,而是使用默认的Socket位置.要在本地计算机上使用TCP,必须使用127.0.0.1作为主机名.

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.1 as hostname.

要指定过去,请在DSN中使用unix_socket而不是host.用于localhost的套接字的位置可以在编译时定义,也可以在某些版本的PHP中使用php.ini中的pdo_mysql.default_socket来定义.

To specify the past use unix_socketinstead of host in the DSN. The location of the socket used for localhost can be defined at compile time or in some versions of PHP using pdo_mysql.default_socket in the php.ini.

这篇关于警告:PDO :: __ construct():[2002]在以下目录中没有这样的文件或目录(试图通过unix:///tmp/mysql.sock进行连接)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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