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

WordPressのインストールをしようとしたら、Error:SQLSTATE[HY000] [2054] The server requested authentication method unknown to the clientというエラーが出たので対処法となります。
データベース関連
データベースの情報は以下の通りです
- ミドルウェア:MySQL
- バージョン:8.0
ユーザー情報
- ユーザー名:logw
- パスワード:nyi9rGK3)*%K&?
- ホスト名1:locahost
- ホスト名2:127.0.0.1
- ポート番号:3306
ホスト名は1でも2でもどちらでも問題ありません。
症状
SSHからの接続はできますが、PHPからの接続ができなくエラーとなります。
[c]
# mysql -u logw -p
Enter password:上記のパスワードをいれる
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 203
Server version: 8.0.18 MySQL Community Server - GPL
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
[/c]
問題無く接続できました。
phpから接続
[php]
<html>
<head><title>PHP TEST</title></head>
<body>
<?php
$dsn = 'mysql:dbname=logw;host=127.0.0.1';
$user = 'logw';
$password = 'nyi9rGK3)*%K&?';
try{
$dbh = new PDO($dsn, $user, $password);
print('
');
if ($dbh == null){
print('接続に失敗しました。
');
}else{
print('接続に成功しました。
');
}
}catch (PDOException $e){
print('Error:'.$e->getMessage());
die();
}
$dbh = null;
?>
</body>
</html>
[/php]
エラー文:Error:SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client
となります。phpのバージョンは7.3となります。
原因
MySQL8では、デフォルトの認証方式がcaching_sha2_passwordとなっていますが、PHPのライブラリが対応していないことでエラーとなります。PHPの認証方式はmysql_native_passwordのため、これに戻す事で解決します。
設定確認
まずはSSHで設定を確認します。
[c]
# mysql -u root -p
Enter password:rootのパスワードをいれる
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 203
Server version: 8.0.18 MySQL Community Server - GPL
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
#認証方式を確認する
mysql> SELECT user, host, plugin FROM mysql.user;
+------------------+-----------+-----------------------+
| user | host | plugin |
+------------------+-----------+-----------------------+
| logw | localhost | caching_sha2_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session | localhost | caching_sha2_password |
| mysql.sys | localhost | caching_sha2_password |
| root | localhost | mysql_native_password |
+------------------+-----------+-----------------------+
#認証方式がcaching_sha2_passwordとなっています。
[/c]
認証方式が違うので、PHPだとエラーとなります。今回は既存ユーザーとなるので、既存ユーザーの修正コマンドをいれます。
既存ユーザー修正
[c]
mysql> ALTER USER 'logw'@'localhost' IDENTIFIED WITH mysql_native_password BY 'nyi9rGK3)*%K&?';
Query OK, 0 rows affected (0.01 sec)
[/c]
これで設定完了です。
※パスワードは自身の使っているパスワードに置き換えて下さい
中身を確認します
[c]
mysql> SELECT user, host, plugin FROM mysql.user;
+------------------+-----------+-----------------------+
| user | host | plugin |
+------------------+-----------+-----------------------+
| logw | localhost | mysql_native_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session | localhost | caching_sha2_password |
| mysql.sys | localhost | caching_sha2_password |
| root | localhost | mysql_native_password |
+------------------+-----------+-----------------------+
[/c]
無事に変更されました。これで接続できます
個人支援・寄付について
サイトラボでは個人支援・寄付を受けております。ご協力いただける方はお願いいたします。当サイトではビットコインで受け付けております。
- ビットコイン:3LHnADwZwUbic2L45EnVJEykiG6KfbqrwS