Search code examples
phplaravelinertiajslaravel-jetstream

DB connection issue with Laravel, Jetstream and Inertia


I've set up a new project using Laravel with Sail (Docker) and Jetstream with Inertia.js, I've gone through the setup but getting this error when accessing the site locally:

Illuminate \ Database\ QueryException
PHP 8.1.3
9.3.1
SQLSTATE[HY000] [2002] Connection refused

    select * from `sessions` where `id` = KdN2zl3PoYshRyfBJOCQi4nNLPnezUJZJi6MgXGR limit 1

This can be fixed by changing the DB_HOST from 127.0.0.1 to mysql, I can then access the site locally, but when when I do something like php artisan migrate I get the error:

Illuminate\Database\QueryException

  SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known (SQL: select * from information_schema.tables where table_schema = db and table_name = migrations and table_type = 'BASE TABLE')

Where with the DB_HOST set to 127.0.0.1, php artisan migrate works fine, so by fixing one it breaks the other, please help


Solution

  • If you use Laravel Sail you have to use sail artisan migrate instead of php artisan migrate.