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

612,365

Solution 1

The error message indicates that a MySQL connection via socket is tried (which is not supported).

In the context of Laravel (artisan), you probably want to use a different / the correct environment. Eg: php artisan migrate --env=production (or whatever environment). See here.

Solution 2

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

Laravel 5+: Change "DB_HOST" in the .env file from "localhost" to "127.0.0.1"

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

Not sure why "localhost" doesn't work by default but I found this answer in a similar question solved in a symfony2 post. https://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 https://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 "127.0.0.1" 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.

Solution 3

I got the same problem and I'm running 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:

[PDOException]
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:

    /tmp/mysql.sock
    
  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!

Solution 4

I encountered the [PDOException] SQLSTATE[HY000] [2002] No such file or directory error for a different reason. I had just finished building a brand new LAMP stack on Ubuntu 12.04 with Apache 2.4.7, PHP v5.5.10 and MySQL 5.6.16. I moved my sites back over and fired them up. But, I couldn't load my Laravel 4.2.x based site because of the [PDOException] above. So, I checked php -i | grep pdo and noticed this line:

pdo_mysql.default_socket => /tmp/mysql.sock => /tmp/mysql.sock

But, in my /etc/my.cnf the sock file is actually in /var/run/mysqld/mysqld.sock.

So, I opened up my php.ini and set the value for pdo_mysql.default_socket:

pdo_mysql.default_socket=/var/run/mysqld/mysqld.sock

Then, I restarted apache and checked php -i | grep pdo:

pdo_mysql.default_socket => /var/run/mysqld/mysqld.sock => /var/run/mysqld/mysqld.sock

That fixed it for me.

Solution 5

The answer from @stuyam solved the "No such file or directory" issue for me

Short answer: Change "host" in the /app/config/database.php file from "localhost" to "127.0.0.1"

But then I had a "Connection refused" error. If anyone had the same issue, my solution for this was to update the app/config/local/database.php file so the port is 8889:

'mysql' => array(
        'driver'    => 'mysql',
        'host'      => '127.0.0.1',
        'port'      => '8889',
        'database'  => 'databaseName',
        'username'  => 'root',
        'password'  => 'root',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),
Share:
612,365
Daniel Hollands
Author by

Daniel Hollands

Updated on July 08, 2022

Comments

  • Daniel Hollands
    Daniel Hollands almost 2 years

    I believe that I've successfully deployed my (very basic) site to fortrabbit, but as soon as I connect to SSH to run some commands (such as php artisan migrate or php artisan db:seed) I get an error message:

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

    At some point the migration must have worked, because my tables are there - but this doesn't explain why it isn't working for me now.