Skip to content
困った時に思い出したい

【MySQL】PHPで接続できないとき SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client

db1 min read

環境

  • Mac OS X 10.14.6
  • MySQL 8.0.17
  • PHP 7.1.23

状況

PHPにMySQLの接続設定を記述して接続した際にブラウザにて

SQLSTATE[HY000][2054] The server requested authentication method unknown to the client

といったエラーメッセージが表示されました。

原因

MySQL 8〜ではデフォルトの認証方法が変更になっているようです。

解決

まずは現在の認証方法を確認しておきます。

MySQLへログインして

sudo mysql -u root -p

データベースを切り替えて

mysql > use mysql

各ユーザーの認証方法をチェックします。

mysql > select user, host, plugin from user;

とすると、こんな感じになります。

userhostplugin
hogelocalhostcaching_sha2_password
mysql.infoschemalocalhostcaching_sha2_password
mysql.sessionlocalhostcaching_sha2_password
mysql.syslocalhostcaching_sha2_password
rootlocalhostcaching_sha2_password

hogeの認証方法を変更します。

mysql> alter user 'hoge'@'localhost' identified with mysql_native_password by 'ここにパスワード';

こうすると

mysql> mysql> select user, host, plugin from user;
userhostplugin
hogelocalhostmysql_native_password
mysql.infoschemalocalhostcaching_sha2_password
mysql.sessionlocalhostcaching_sha2_password
mysql.syslocalhostcaching_sha2_password
rootlocalhostcaching_sha2_password

これでPHPからの接続がうまくできるようになりました。

こちらを参考にさせていただきました。ありがとうございました。

MySQL8.0 認証方式を変更する(Laravel5) – Qiita

[MySQL 8.0へ接続時に”SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client”になる場合の対処法 – マゴトログ シュミニイキル

© 2021 by 困った時に思い出したい. All rights reserved.
Theme by LekoArts