LoginSignup
40
23

More than 3 years have passed since last update.

【Laravel】migrateができないのは環境変数が間違っているからかも!あるあるエラーを.envと共に振り返る。

Last updated at Posted at 2019-12-22

Laravel's error.png

どうも、たかふみです。

Laravelで開発を行っていると、必ず使うであろう php artisan migrateコマンド。僕も何度もお世話になったコマンドです。

今回は php artisan migrate を実行したときに出会ったエラーと共に解決策を書きたいと思います。

あるあるエラー1:Connection refused(Dockerの場合)

エラーメッセージ

  Illuminate\Database\QueryException  : SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = sample and table_name = migrations and table_type = 'BASE TABLE')

  at /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:665
    661|         // If an exception occurs when attempting to run a query, we'll format the error
    662|         // message to include the bindings with SQL, which will make this exception a
    663|         // lot more helpful to the developer instead of just the database's errors.
    664|         catch (Exception $e) {
  > 665|             throw new QueryException(
    666|                 $query, $this->prepareBindings($bindings), $e
    667|             );
    668|         }
    669| 

  Exception trace:

  1   PDOException::("SQLSTATE[HY000] [2002] Connection refused")
      /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

  2   PDO::__construct("mysql:host=127.0.0.1;port=3306;dbname=sample", "root", "password", [])
      /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

  Please use the argument -v to see more details.

   Whoops\Exception\ErrorException  : Module 'zip' already loaded

  at Unknown:0
    1| 

  Exception trace:

  1   {main}()
      /var/www/html/artisan:0

解決策:DB_HOSTの設定を見直す。

.envファイルにあるDB_HOSTの値を確認したところ、DB_HOST=127.0.0.1となっていました。調べると、dockerの場合はDBのコンテナ名に設定する必要があるとのことです。

【修正後】
DB_HOST=mysql

これで解決しました。

エラー2:Connection refused

 Illuminate\Database\QueryException  : SQLSTATE[HY000] [1049] Unknown database 'sample' (SQL: select * from information_schema.tables where table_schema = sample and table_name = migrations and table_type = 'BASE TABLE')

  at /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:665
    661|         // If an exception occurs when attempting to run a query, we'll format the error
    662|         // message to include the bindings with SQL, which will make this exception a
    663|         // lot more helpful to the developer instead of just the database's errors.
    664|         catch (Exception $e) {
  > 665|             throw new QueryException(
    666|                 $query, $this->prepareBindings($bindings), $e
    667|             );
    668|         }
    669| 

  Exception trace:

  1   PDOException::("SQLSTATE[HY000] [1049] Unknown database 'sample'")
      /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

  2   PDO::__construct("mysql:host=mysql;port=3306;dbname=sample", "root", "password", [])
      /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

  Please use the argument -v to see more details.

解決策:DB_DATABASEの値を確認

エラーメッセージから「'sample'というDBは存在しません。」ということが分かります。
この場合は、接続しようとしているDBの名前を見直す必要があります。

僕の場合はDB「bookmark」に接続をしたかったので、.envファイルの該当箇所を下記のように修正しました。

【修正後】
DB_DATABASE=bookmark

これで接続できました。

エラー3:修正しても接続できない。

解決策:envファイルが複数存在していないか確認

このエラー(?)が起きたときに一番ハマりました。笑 修正してもキャッシュを削除してもエラーが起きるため、もう一度ファイルを確認しようとファイル名で検索したところ、「.env」が2つあることに気づきました。

不要なenvファイルを削除したら上手くいったのでこれが原因だったようです。

migrateに成功すれば晴れて下記の状態になるはずです。このババババッと実行される感じが気持ちいいんですよね。

root@592de04c5bde:/var/www/html/web# php artisan migrate       
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (0.03 seconds)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (0.01 seconds)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (0.01 seconds)

まとめ:エラーメッセージを読んでenvファイルを見直そう。

こうするとmigrateが上手くいかないのは.envファイルの値、つまり環境変数が間違っていることが多かったように感じます。DBが無いと開発に着手できないので、ここはスムーズに乗り越えたいですね。それでは!

40
23
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
40
23