37

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.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345

5 Answers5

94

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.

chiborg
  • 26,978
  • 14
  • 97
  • 115
johannes
  • 15,807
  • 3
  • 44
  • 57
  • But isn't there a way to disable this? I mean, it was working exactly as-is yesterday. And how can I enable the socket on the server so that it works? –  Nov 30 '09 at 14:30
  • I had PHP scripts working with 'localhost' in the config file, and after a RAM upgrade & reboot, they stopped working when triggered from the console -- still worked in the browser. Changing 'localhost' to '127.0.0.1' fixed it. – Jason Preston May 25 '11 at 20:23
  • 1
    Seems this has stopped working. Using 127.0.0.1 seems to still default to the socket. – Cfreak Apr 17 '12 at 19:22
  • This is annoying af, had this problem over the years under different guises and I always forget about this nitpick and spend some considerable time searching for a solution. That "design desicion" to use a socket if host == 'localhost' seems to be absolute garbage to me. – gvlasov Oct 09 '18 at 15:51
9

From the PHP documentation about connection to MySQL using PDO: 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.

So in order to fix this you would have to properly configure in php.ini the location of your mysql.sock

  1. 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
  2. Edit your php.ini file and properly set the value for pdo_mysql.default_socket

  3. Restart your Apache server to pickup the changes in the php.ini file

Ilie Pandia
  • 1,829
  • 14
  • 16
3

On Ubuntu, you can use this setting in php.ini

pdo_mysql.default_socket=/var/run/mysqld/mysqld.sock
Danial
  • 1,496
  • 14
  • 15
0

There's an update to the docs for Drush which is documented here.

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
Mike Gifford
  • 641
  • 1
  • 8
  • 17
0

I just added this line:

'unix_socket'   => '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock',

and all was well.

Nikhil
  • 16,194
  • 20
  • 64
  • 81