====== 【Laravel】The server requested authentication method unknown to the client と出るとき ======
MySQL8 からデフォルトのログイン認証方式が caching_sha2_password になりました。\\
しかし、PDO ではこの認証方式がサポートされていないため以下のエラーがでる。
The server requested authentication method unknown to the client
対処法は、MySQLにログインして認証方式を mysql_native_password に変更するだけ。
# 現在の設定を確認
select user, host, plugin from mysql.user;
+----------+-----------+-----------------------+
| user | host | plugin |
+----------+-----------+-----------------------+
| hogefuga | localhost | caching_sha2_password |
+----------+-----------+-----------------------+
# 設定変更
alter user hogefuga@localhost identified with mysql_native_password by 'パスワード';
# 変更されたか確認
select user, host, plugin from mysql.user;
+----------+-----------+-----------------------+
| user | host | plugin |
+----------+-----------+-----------------------+
| hogefuga | localhost | mysql_native_password |
+----------+-----------+-----------------------+
ちなみに上記方法では、新規 MySQL ユーザを作成すると毎回同じ現象に遭遇することになるのでデフォルトの認証方式を mysql_native_password に変更してしまう方法もある。
vi /etc/my.cnf
[mysqld]
default-authentication-plugin=mysql_native_password
# 再起動
sudo service mysqld restart