LoginSignup
1
1

More than 3 years have passed since last update.

RubyOnRails、Authentication plugin 'caching_sha2_password' cannot be loadedエラーの解決法

Last updated at Posted at 2021-01-16

RubyOnRails、Authentication plugin 'caching_sha2_password' cannot be loadedエラーの解決法

Ruby on rails 開発時に、databaseをmysql 8.0に設定してしたく、database.ymlをmysql設定の上で、rails db:migrate を実行したところ、エラー「Authentication plugin ‘caching_sha2_password’ cannot be loaded」となった時の解決法を共有します。
*laravelにも同様のエラーが発生するとの。

環境の整理

  • 開発環境:Windows10 Home 64ビットオペレーティングシステム、x64ベースプロセッサ
  • Rails version 5系
  • Mysql
    • version 8.0
    • user: root
    • password: root(=今回の例)
  • RubyOnRailsのdatabase.yml設定状況 スクリーンショット 2021-01-16 213315.png

原因

mysql 8.0以降に、認証方式がcaching_sha2_passwordを使うようになったこと。
しかし、Railsはこれに対応していなかったこと。

方法

対象のユーザーの認証方式を「caching_sha2_password」から「mysql_native_password」に変更する。
今回をuserがrootの認証方式を変更する。

  1. mysql -u root -p でmysqlにアクセスする
  2. root (今回のpassword)を入力、Enterする。
  3. SELECT user, host, plugin FROM mysql.user; でユーザー一覧を表示させ、確認する。

    • root の認証方式が「caching_sha2_password」だ 111.png
  4. ALTER USER root IDENTIFIED WITH mysql_native_password BY 'root'; を実行する。

    • 構文:ALTER USER ユーザー名 IDENTIFIED WITH mysql_native_password BY 'password';
  5. SELECT user, host, plugin FROM mysql.user; でrootの認証方式が変更されていることを確認する。
    222.png

  6. exit でmysqlから抜ける

  7. rails db:migrate で本題のrailsのmigrateコマンドを実行する。

  8. エラーなく完了された。
    333.png

*補足:
my.ini(macだとmy.cnf)にて、
default_authentication_plugin=caching_sha2_password

default_authentication_plugin=mysql_native_password
に変更すると、エラーが消えるとの話もあったが、
変更したあとでmysqlを再起動しても変更されなかった。
ちなみに、my.iniの場所は「C:\ProgramData\MySQL\MySQL Server 8.0」である。

1
1
0

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
  3. You can use dark theme
What you can do with signing up
1
1