This is a completely fresh installation of Laravel (because i was hoping that would fix the issue), which now makes it version 8.40.00
. And to run it, i go through sail
, by doing: ./vendor/bin/sail up -d
.
Even though the initial .env
file has database credentials which don't work (sail:password), the database connection works after quickly changing it to (root:"").
This makes it so that my app, when accessed through the browser, can connect to the database(!) I have successfully created a new table, and can read and display data from it on the page.
However, when running php artisan migrate
, it outputs:
Illuminate\Database\QueryException
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known (SQL: select * from information_schema.tables where table_schema = laravel_hello and table_name = migrations and table_type = 'BASE TABLE')
at vendor/laravel/framework/src/Illuminate/Database/Connection.php:678 674▕ // If an exception occurs when attempting to run a query, we'll format the error 675▕ // message to include the bindings with SQL, which will make this exception a 676▕ // lot more helpful to the developer instead of just the database's errors. 677▕ catch (Exception $e) { ➜ 678▕ throw new QueryException( 679▕ $query, $this->prepareBindings($bindings), $e 680▕ ); 681▕ } 682▕
+33 vendor frames
A lot of existing questions about this error (just the 2002
error in general; not mentioning artisan) seem to suggest changing the DB_HOST
to MySQL's container name from Docker will solve it. But in my case, both the (initial) value of "mysql"
and the name of the docker container produce the same result, and generate the same error.
So, when accessing the app through the browser, the database connection works; when running an artisan command, it outputs a 2002
error.
What is causing this? And how is it correctly dealt with?