Laravel 8 - SQLSTATE[HY000] [2002] No such file or directory

Viewed 2549

I'm trying to make a project using Laravel and Sail but I'm getting errors when I'm trying to migrate or do anything related to the project's database (for example, seeding).

More specifically, when using artisan sail migrate or anything related, I'm getting the following errors:

When DB_HOST=localhost in my .env file:

SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables where table_schema = noway and table_name = migrations and table_type = 'BASE TABLE')

When DB_HOST=127.0.0.1 in my .env file:

SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = noway and table_name = migrations and table_type = 'BASE TABLE')

What I've tried:

  • changing the DB_HOST to localhost and 127.0.0.1 (most common answer online, no succes)
  • making sure SQL is running
  • clearing cache for sail artisan migrate
  • ./vendor/bin/sail down --rmi all -v to remove all images and volumes
  • changing username and password and putting them between '' or ""

None of the above have been of succes.

2 Answers

did you run ./vendor/bin/sail up after creating and setting .env ? if not please execute ./vendor/bin/sail down --rmi all -v to remove all images and volumes and then just ran ./vendor/bin/sail up to recreate the images and volumes.

Also as per laravel docs https://laravel.com/docs/8.x/sail you can connect to db by setting env DB_HOST to mysql

T had the same problem with pretty much the same conditions: -I started with a laravel project then moved it to sail

the way I solved this was by adding an entry DB_PORT=3306 in the .env file and changed DB_HOST=localhost to DB_HOST=mysql as described in laravel docs : https://laravel.com/docs/8.x/sail#mysql

  • don't forget to check the new credentials that sail uses by default
Related