LoginSignup
1
1

More than 1 year has passed since last update.

djangoでmysqlを使うときに"Authentication plugin 'caching_sha2_password' cannot be loaded..."と出たとき

Posted at

目的

djangoでsqliteの代わりにmysqlをデータベースとして利用すること。

結論

djangoでmysqlを使うときに"Authentication plugin 'caching_sha2_password' cannot be loaded..."とエラーが出た場合は、
ユーザーの認証方法をmysqlclientが非対応だがデフォルトであるcaching_sha2_passwordからmysql_native_passwordに変更する必要がある。

実施環境

ハードウェア環境

項目 情報
OS macOS Catalina(10.15.7)
ハードウェア MacBook Air (11-inch, Early 2015)
プロセッサ 1.6 GHz デュアルコアIntel Core i5
メモリ 4 GB 1600 MHz DDR3
グラフィックス intel HD Graphics 6000 1536 MB

ソフトウェア環境

項目 情報
homebrew 3.3.8
mysql Ver 8.0.27 for macos10.15 on x86_64
python 3.8.12
django 3.1.2
anaconda 4.10.1
django 21.2.4
pip 3.1.2

経緯

djangoでsqliteの代わりにmysqlをデータベースとして利用するために、mysqlclientをインストールした。
その後modelを反映させようとしている。

python manage.py makemigrations
python manage.py migrate

マイグレーション完了といきたいところだが、

django.db.utils.OperationalError: (2059, "Authentication plugin 'caching_sha2_password' cannot be loaded: dlopen(/opt/anaconda3/envs/djangoenv/lib/plugin/caching_sha2_password.so, 2): image not found")

このようなエラーが表示された。

原因究明

どうやら現在はdjangoの認証方式としてcaching_sha2_passwordがデフォルトとして利用されているが、ドライバであるmysqlclientが対応できていないため、上記のエラーが吐き出されたよう。

解決

この記事を参考に
利用するデータベースをmysqlに指定し、

mysql> use mysql

各ユーザーの認証方法を確認。

mysql> select user, plugin from user;
+------------------+-----------------------+
| user             | plugin                |
+------------------+-----------------------+
| mysql.infoschema | caching_sha2_password |
| mysql.session    | caching_sha2_password |
| mysql.sys        | caching_sha2_password |
| root             | caching_sha2_password |
+------------------+-----------------------+

必要最低限のユーザーだけ認証方法をcaching_sha2_passwordからmysql_native_passwordに変更していく。

mysql> alter user 'test'@'localhost' identified WITH mysql_native_password by 'パスワード';

もう一度確認し、pluginが変更されていることを確認

mysql> select user, plugin from user;
+------------------+-----------------------+
| user             | plugin                |
+------------------+-----------------------+
| mysql.infoschema | caching_sha2_password |
| mysql.session    | caching_sha2_password |
| mysql.sys        | caching_sha2_password |
| test             | mysql_native_password |
| root             | caching_sha2_password |
+------------------+-----------------------+

マイグレーションも無事成功。

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