Rails mysql使用時エラー「Authentication plugin ‘caching_sha2_password’ cannot be loaded」が発生した場合の対処法

Rails mysql使用時エラー「Authentication plugin ‘caching_sha2_password’ cannot be loaded」が発生した場合の対処法

Railsでmysqlを使用時に、エラー「Authentication plugin ‘caching_sha2_password’ cannot be loaded」が発生した場合の対処法を記述してます。

環境

  • OS windows10 pro 64bit
  • ruby 2.6.6
  • rails 6.0.3.2
  • mysql 8.0.21

※windows10にrubyをインストールして実行する手順はこちら
※windows10にRuby on Railsをインストールして利用する手順はこちら

エラー全文

「rails s」実行後に発生。

Authentication plugin 'caching_sha2_password' cannot be loaded:

原因

MySQLの認証形式に対応していないため

対処法

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

対象のユーザーを「caching_sha2_password」から「mysql_native_password」に変更します。
※ここでは、ユーザー「testuser」パスワード「testpassword」の認証方式を変更してます。

ALTER USER testuser IDENTIFIED WITH mysql_native_password BY 'testpassword';

認証方式が変更されたことを確認します。

SELECT user, host, plugin FROM mysql.user;

<出力結果>
+------------------+-----------+-----------------------+
| user             | host      | plugin                |
+------------------+-----------+-----------------------+
| testuser         | %         | mysql_native_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
| root             | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+

変更後は、正常に接続されました。