LoginSignup
12
8

More than 3 years have passed since last update.

Laravel マイグレーション時にエラーが出て困った話

Posted at

目的

  • 初回マイグレーション時にエラーが出力されて解決した話をまとめる

エラー内容

  • エラーSQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost'を筆頭としたエラーが出力された。
  • 下記にマイグレーション時のコマンドとエラー内容を記載する。
$ php artisan migrate

   Illuminate\Database\QueryException 

  SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) (SQL: select * from information_schema.tables where table_schema = calculation_drill_app and table_name = migrations and table_type = 'BASE TABLE')

  at 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| 

      +34 vendor frames 
  35  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

エラーの原因

  • Mysqlのrootのアカウントのパスワードとlaravelアプリ側の.envなどに記載したパスワードが一致していなかった。

解決方法

  1. アプリ名ディレクトリで下記コマンドを実行して「.env」ファイルを開きDB_PASSWORD=の部分の編集を行った。

    $ vi .env
    
  2. アプリ名ディレクトリで下記コマンドを実行して「database.php」ファイルを開き'password' => env('DB_PASSWORD',の部分の編集を行った。

    $ vi config/database.php
    
  3. MySQLを再起動しマイグレーションを実施したところエラーは解消された。

自分宛てメッセージ

  • Laravelだけに限らないがマイグレーション時のエラーは自分の経験上大体MySQLのユーザとパスワードの入力ミスの事が多いので記入時には注意する。
12
8
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
12
8