0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Mac上の Django migrate で 'mysql_native_password' cannot be loaded エラー

Last updated at Posted at 2025-05-29

Django の migrateが通らない…

Django のプロジェクトをMacで作成して

$ django-admin startapp myapp

myapp/myapp/settings.py をこんな感じで編集しました。

settings.py
...
DATABASES = {
    'default': {
        #'ENGINE': 'django.db.backends.sqlite3',
        #'NAME': BASE_DIR / 'db.sqlite3',
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'my_db_name',
        'USER': 'my_db_user',
        'PASSWORD': 'my_db_password',
        'HOST': 'my_db_host',
        'PORT': '3306',
    }
}
...

データベース(リモートホスト上のMariaDB 10.5.23)に接続したら...

$ python manage.py migrate
...
django.db.utils.OperationalError: (2059, "Authentication plugin 'mysql_native_password' cannot be loaded: ...
...

プラグイン 'mysql_native_password' が無いようだ…なぜ?
Macのmysql-clientをいつの間にかバージョンアップしてしまっていたのでしょうか…
仕方がないのでバージョン5.7を入れてみるとしました。

$ brew install mysql-client@5.7
==> Downloading https://formulae.brew.sh/api/formula.jws.json
==> Downloading https://formulae.brew.sh/api/cask.jws.json
Error: mysql-client@5.7 has been disabled because it is not supported upstream! It was disabled on 2024-08-01.

去年の8月1日からインストールできなくなっていたようです。

いろいろとググっていると8.0をインストールしたら良いらしい…やってみます。

$ brew install mysql-client@8.0

で再度 migrate …

$ python manage.py migrate
...
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?
...

mysqlclient は pip で入れましたが…

さらにググると pymysql をインストールして、

$ pip install pymysql

そして myapp/myapp/__init__.py を以下のようにすると良いようです。

__init__.py
import pymysql
pymysql.install_as_MySQLdb()

再度実行

$ python manage.py migrate 
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying sessions.0001_initial... OK```

今度はうまく行きました。
めでたしめでたし。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?