php artisan migrateエラーになった時の解決メモ【Homestead/Laravel】

やろうとしたこと

HomesteadでLaravel開発環境を構築する際、MySQLに接続してマイグレーションする

 

問題

マイグレーションファイルを作成し、artisan migrateしたら以下のエラーになる

vagrant@homestead:~/code/myapp$ php artisan migrate

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

 

対応したこと

.envファイルをHomestead用に修正

【修正後】

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

【修正前】

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

 

成功しました

vagrant@homestead:~/code/myapp$ php artisan migrate

   INFO  Preparing database.  

  Creating migration table ............................................................................................................... 23ms DONE

   INFO  Running migrations.  

  2019_12_14_000001_create_personal_access_tokens_table .................................................................................. 85ms DONE
  2022_10_18_105932_create_posts_table ................................................................................................... 19ms DONE

 

前は最初からHomestead用の設定だったようですが、

2022年10月現在、自分の場合は違いました。