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

2

5

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.

Therine answered 14/10, 2021 at 19:15 Comment(0)
M
5

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

Maestas answered 16/10, 2021 at 10:5 Comment(0)
J
1

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
Jilly answered 20/12, 2021 at 9:8 Comment(1)
especialy when you using DOCKER host can be/is a service name in docker-compose fileGumbotil

© 2022 - 2024 — McMap. All rights reserved.