AWS Cloud9 (ubuntu linux) + Laravel + Mysql : php artisan migrate error

Updated: 2020/8/17

 

環境:

AWS Cloud9 EC2 platform: ubuntu linux

Laravel: 5.8

Mysql: 5.7

 

AWS Cloud9 上でphp artisan migrateをすると下記のエラー

SQLSTATE[HY000] [1698] Access denied for user 'root'@'localhost' (SQL: select * from information_schema.tables where table_schema = laravel and table_name = migrations)

 

原因:

mysql 5.7ではroot ユーザーは管理者権限(sudo)がないとmysqlにログインできない。

恐らくこれが原因。

  • 管理者権限でmysqlにログイン

  sudo mysql -u root

  • ユーザーを作成

  create User 'NewUser'@'localhost' identified by 'PASSWORD';

  • ユーザーにsudo無しでログインできるようにする

Alter User 'NewUser'@'localhost' identified with mysql_native_password by 'PASSWORD';

  • ユーザーにMysql操作権限を付与

grant all privileges on *.* to 'NewUser'@'localhost' with grant option;

flush privileges;

exit;

 

これで新しく作ったユーザーがsudo無しでmysqlにログインできるか確認

 

Laravel側.envを正しく設定

php artisan config:clearで変更を正しく反映させる。

*セキュリティについて未考慮。正しい方法かはわからない。

 

php artisan migrate >>> successfully migrated!!

 

ref:

docs.aws.amazon.com

https://note.mu/junf/n/na40fbca9e6ea

https://www.lucidar.me/en/aws-cloud9/install-mysql-and-phpmyadmin-on-aws-cloud9/