Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
454 views
in Technique[技术] by (71.8m points)

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

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 ...

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?

Thanks for any help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...