現象
1 2 3 4 |
$ php artisan migtate Illuminate\Database\QueryException : SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client (SQL: select * from information_schema.tables where table_schema = backend and table_name = migrations and table_type = 'BASE TABLE') |
MySQL8では、ユーザー作成時に設定される認証方式がデフォルトでcaching_sha2_password
となっている。
この認証方式はPHPのPDOでサポートされていないため、LaravelがPDOでDB接続した際に、エラーとなる。
対処方法はMySQLにログインし、認証形式を変更すればOK。
docker環境なら以下で入れる。
1 2 3 4 5 |
$ docker exec -it [コンテナ名] bash $ mysql -u root -p Enter password: # MySQLのdefaultのパスワードはroot |
ログイン後は以下で設定を確認できる。
1 2 3 4 5 6 7 8 9 10 11 12 |
mysql> SELECT user, host, plugin from mysql.user; +------------------+-----------+-----------------------+ | user | host | plugin | +------------------+-----------+-----------------------+ | docker | % | caching_sha2_password | | root | % | caching_sha2_password | | mysql.infoschema | localhost | caching_sha2_password | | mysql.session | localhost | caching_sha2_password | | mysql.sys | localhost | caching_sha2_password | | root | localhost | caching_sha2_password | +------------------+-----------+-----------------------+ |
設定を変更する。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
mysql> alter user 'docker' identified with mysql_native_password by 'docker'; Query OK, 0 rows affected (0.00 sec) mysql> select user, plugin from mysql.user; +------------------+-----------------------+ | user | plugin | +------------------+-----------------------+ | docker | mysql_native_password | | root | caching_sha2_password | | mysql.infoschema | caching_sha2_password | | mysql.session | caching_sha2_password | | mysql.sys | caching_sha2_password | | root | caching_sha2_password | +------------------+-------------------- |
変わったことを確認できる。
ログアウトし、再度マイグレーションを実行する。
1 2 3 |
$ php artisan migrate Migrating: 2019_11_21_220712_create_likes_table Migrated: 2019_11_21_220712_create_likes_table (0.1 seconds) |
できた。
my.cnfの設定
初期値でmy.cnfに設定しておく方法もメモしておく。
1 2 3 4 |
[mysqld] default_authentication_plugin= mysql_native_password |
でOK!。