警告: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

查看:27
本文介绍了警告: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天全站免登陆