LoginSignup
4

More than 1 year has passed since last update.

FuelPHPでのMySQL接続時にThe server requested authentication method unknown to the client [caching_sha2_password] となった場合に対処してみた

Last updated at Posted at 2019-12-16

概要

FuelPHPでMySQLに接続時、掲題のエラーが発生した場合の対応法です。

環境

  • MySQL v8.0
  • PHP 7.3.12

エラー事由

MySQL8.0でのデフォルトの認証方式は「caching_sha2_password」であるが、PHP側では対応がされていない。
そのため、認証方式が異なるためにエラーが発生する。

対応方法

MySQLに接続するユーザーの認証方式を「mysql_native_password」に変更します。

1.ユーザー単位での認証方式の変更

先ず現状の認証方式を確認します。

mysql> SELECT user, host, plugin FROM mysql.user;
+------------------+-----------+-----------------------+
| user             | host     | plugin                |
+------------------+-----------+-----------------------+
| admin            | %     &nbp;   | caching_sha2_password |
| mysql.infoschema | localhost | mysql_native_password |
| mysql.session    | localhost | mysql_native_password |
| mysql.sys        | localhost | mysql_native_password |
| root             | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+
5 rows in set (0.00 sec)

現状を確認後、指定したユーザーの認証方式を変更します。

mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
Query OK, 0 rows affected (0.19 sec)

2.デフォルトの認証方式の変更

MySQLへのデフォルトの認証方式を変更します。

/etc/mysql/conf.d/my.conf
[mysqld]
# デフォルトの認証方式を変更(デフォルトは「caching_sha2_password 」)
default-authentication-plugin=mysql_native_password

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
What you can do with signing up
4