If you are seeing this message:  SQLSTATE[HY000] [2002] No such file or directory here i show to fix it!

What is "SQLSTATE[HY000] [2002] No such file or directory"  error?

most likely you have problem with configuration of database connection in your .env file.

How to fix PDOException SQLSTATE[HY000] [2002] No such file or directory

To fix it you have to be sure that correct DB_HOST  is set in env file and the value of it should be correct. If you are in your local environment try to change from localhost to 127.0.0.1 

                    DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
...
                  

And after you make this change do not forget to clear the config cache

                    php artisan config:clear
                  

But in some cases, this could also be wrong. And after you make this change you will get another error:

SQLSTATE[HY000] [2002] Connection refused (SQL: select * from `sessions` where `id` = ...

 

This error is not really explaining the real problem, but the main  problem now is that your DB_HOST IP address is definitely wrong and Laravel actually cannot make connection to this database. In order to fix it double check your DB host IP and port, and update it accordingly.

If you are using XAMPP, open it and check on which IP is working because latest version are using different local IP:

In this case you have to change DB_HOST to 

                    DB_HOST=192.168.64.2
                  

do not forget to run php artisan config:clear  to apply the  change.

And boom the error is gone!

If you are using some Docker or some other virtual development environment, double check the MySQL IP and port and set it correctly in .env file

 

So, basically this can be fixed in these 3 steps:

  • Check the correct IP address of your MySQL (or just switch localhost to 127.0.0.1)
  • update DB_HOST in .env file
  • run php artisan config:clear to clear the cache and apply your change

I hope some of the solutions mentioned above can solve the sqlstate[hy000] [2002] no such file or directory  error message that you are facing