sailインストールしたら
$ php artisan migrate
ではなく
$ sail artisan migrate
しないとdockerコンテナのDB_HOST=mysql (.env) に接続できないよってお話。
前者は、phpをインストールしているmacローカルからmysqlに接続しようとしてるので、コンテナ内のホストが解決できないのは当たり前…(詳しくなかったら知らないのも無理ない)
コンテナに入って実行すればもちろん言うまでもなく前者で実行可能。
なお DB_HOST=127.0.0.1 にすれば前者でも接続できる。
けど、migrateのために書き換えて実行は面倒ですよね。エラー解決できなくて、しばしそれでやってたけど。
だって、ドキュメントのところ "php artisan" って書いてあるから、詳しくないとそうするよね…
以上
Illuminate\Database\QueryException
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for mysql failed: nodename nor servname provided, or not known (SQL: select * from information_schema.tables where table_schema = hoge and table_name = migrations and table_type = 'BASE TABLE')
at vendor/laravel/framework/src/Illuminate/Database/Connection.php:742
738▕ // If an exception occurs when attempting to run a query, we'll format the error
739▕ // message to include the bindings with SQL, which will make this exception a
740▕ // lot more helpful to the developer instead of just the database's errors.
741▕ catch (Exception $e) {
➜ 742▕ throw new QueryException(
743▕ $query, $this->prepareBindings($bindings), $e
744▕ );
745▕ }
746▕ }
+35 vendor frames
36 artisan:37
Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))