0

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?

Digital Ninja
  • 3,415
  • 5
  • 26
  • 51
  • Where are you running the migrate command - [in the container, or on the host](https://stackoverflow.com/questions/46725734/php-artisan-migrate-error-nodename-nor-servname-provided-or-not-know)? After setting your DB_HOST to the name of the container, [did you try clearing the cache](https://stackoverflow.com/questions/33260172/sqlstatehy000-2002-php-network-getaddresses-getaddrinfo-failed-nodename-no)? – Don't Panic May 01 '21 at 07:28
  • Yup, it was because i was running it on the host... what do we do now, do i delete this question, or do you wanna post that as an answer so i can accept? – Digital Ninja May 01 '21 at 09:32
  • Glad you got it working. And thanks, but I guess it is really a duplicate so I think it should be closed as a dup. – Don't Panic May 01 '21 at 10:19

0 Answers0