php artisan migrate→Illuminate\Database\QueryException : SQLSTATE

php artisan migrate実行時にphp artisan migrate→Illuminate\Database\QueryException : SQLSTATEのエラーが出る場合は、プロジェクトディレクトリ以外の階層でコマンドを叩いている可能性がある。

事象

データベースにテーブルを登録する時点でエラーになる場合
php artisan migrate
を実行すると

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 = default and table_name = migrations and table_type = 'BASE TABLE')
at /Users/yourname/Documents/laravelapp/testapp/vendor/laravel/framework/src/Illuminate/Database/Connection.php:669
665|         // If an exception occurs when attempting to run a query, we'll format the error
666|         // message to include the bindings with SQL, which will make this exception a
667|         // lot more helpful to the developer instead of just the database's errors.
668|         catch (Exception $e) {
> 669|             throw new QueryException(
670|                 $query, $this->prepareBindings($bindings), $e
671|             );
672|         }
673|
Exception trace:
1   PDOException::("PDO::__construct(): php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known")
/Users/yourname/Documents/laravelapp/testapp/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70
2   PDO::__construct("mysql:host=mysql;port=3306;dbname=default", "default", "secret", [])
/Users/yourname/Documents/laravelapp/testapp/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70
Please use the argument -v to see more details.

こんなエラー。

解決方法

落ち着け、大丈夫や
コンテナの中で叩かないといけないコマンドをターミナル上で叩いてしまっているんや
例えばlaradockならlaradockフォルダに移動して、コンテナに入るコマンドを叩いてから
php artisan migrateすればうまくいく

root@b19d0264766d:/var/www# php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (0.12 seconds)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (0.12 seconds)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (0.05 seconds)
Migrating: 2020_12_14_041818_create_stocks_table
Migrated:  2020_12_14_041818_create_stocks_table (0.04 seconds)

それでもうまくいかない時は、すでにデータベースが登録されている。
コマンドを叩くのが2回目だったり、環境を作り直した場合などに起こり得る。

無制限に質問可能なプログラミングスクール!

万が一転職できない場合は、転職保障全額返金できるコースもあり!!

無制限のメンター質問対応

 

DMMウェブキャンプでプログラミングを学習しませんか?

独学より成長スピードをブーストさせましょう!

 

まずは無料相談から!

おすすめの記事