PDOException SQLSTATE[HY000] [2002] No such file or directory

Published on Author Code Father

Laravel 4: Change “host” in the app/config/database.php file from “localhost” to “”

Laravel 5: Change “DB_HOST” in the. env file from “localhost” to “”

I had the exact same problem. None of the above solutions worked for me. I solved the problem by changing the “host” in the /app/config/database.php file from “localhost” to “”.

Not sure why “localhost” doesn’t work by default but I found this answer in a similar question solved in a symfony2 post. http://stackoverflow.com/a/9251924/1231563

Update: Some people have asked as to why this fix works so I have done a little bit of research into the topic. It seems as though they use different connection types as explained in this post http://stackoverflow.com/a/9715164/1231563

The issue that arose here is that “localhost” uses a UNIX socket and can not find the database in the standard directory. However “” uses TCP (Transmission Control Protocol), which essentially means it runs through the “local internet” on your computer being much more reliable than the UNIX socket in this case.


Mac OS X 10.10 Yosemite. I have enabled the Apache Server and PHP that already comes with the OS. Then I just configured the mCrypt library to get started. After that when I was working with models and DB I got the error:

SQLSTATE[HY000] [2002] No such file or directory

The reason I found is just because PHP and MySQL can’t get connected themselves. To get this problem fixed, I follow the next steps:

  1. Open a terminal and connect to the mysql with:

    mysql -u root -p

  2. It will ask you for the related password. Then once you get the mysql promt type the next command:

    mysql> show variables like '%sock%'

  3. You will get something like this:

    +-----------------------------------------+-----------------+ | Variable_name | Value | +-----------------------------------------+-----------------+ | performance_schema_max_socket_classes | 10 | | performance_schema_max_socket_instances | 322 | | socket | /tmp/mysql.sock | +-----------------------------------------+-----------------+

  4. Keep the value of the last row:


  5. In your laravel project folder, look for the database.php file there is where you configure the DB connection parameters. In the mysql section add the next line at the end:

    'unix_socket' => '/tmp/mysql.sock'

  6. You must have something like this:

'mysql' => array( 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'SchoolBoard', 'username' => 'root', 'password' => 'venturaa', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'unix_socket' => '/tmp/mysql.sock', ),

Now just save changes, and reload the page and it must work! I hope it could be useful!



Categories PHP