0

I am trying to get Django running with MySql, I am following this guide: https://docs.djangoproject.com/en/4.2/intro/tutorial02/

Everything works up until this command: python manage.py migrate

I will include the entire error, the punchline is:

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?

The full error:

    Traceback (most recent call last):
  File "/Users/timo/opt/anaconda3/lib/python3.9/site-packages/django/db/backends/mysql/base.py", line 15, in <module>
    import MySQLdb as Database
  File "/Users/timo/opt/anaconda3/lib/python3.9/site-packages/MySQLdb/__init__.py", line 17, in <module>
    from . import _mysql
ImportError: dlopen(/Users/timo/opt/anaconda3/lib/python3.9/site-packages/MySQLdb/_mysql.cpython-39-darwin.so, 0x0002): symbol not found in flat namespace '_mysql_affected_rows'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/timo/Documents/coding/plegeus/musicians/backend/manage.py", line 22, in <module>
    main()
  File "/Users/timo/Documents/coding/plegeus/musicians/backend/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/Users/timo/opt/anaconda3/lib/python3.9/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/Users/timo/opt/anaconda3/lib/python3.9/site-packages/django/core/management/__init__.py", line 416, in execute
    django.setup()
  File "/Users/timo/opt/anaconda3/lib/python3.9/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/timo/opt/anaconda3/lib/python3.9/site-packages/django/apps/registry.py", line 116, in populate
    app_config.import_models()
  File "/Users/timo/opt/anaconda3/lib/python3.9/site-packages/django/apps/config.py", line 269, in import_models
    self.models_module = import_module(models_module_name)
  File "/Users/timo/opt/anaconda3/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/Users/timo/opt/anaconda3/lib/python3.9/site-packages/django/contrib/auth/models.py", line 3, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/Users/timo/opt/anaconda3/lib/python3.9/site-packages/django/contrib/auth/base_user.py", line 57, in <module>
    class AbstractBaseUser(models.Model):
  File "/Users/timo/opt/anaconda3/lib/python3.9/site-packages/django/db/models/base.py", line 143, in __new__
    new_class.add_to_class("_meta", Options(meta, app_label))
  File "/Users/timo/opt/anaconda3/lib/python3.9/site-packages/django/db/models/base.py", line 371, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/timo/opt/anaconda3/lib/python3.9/site-packages/django/db/models/options.py", line 243, in contribute_to_class
    self.db_table, connection.ops.max_name_length()
  File "/Users/timo/opt/anaconda3/lib/python3.9/site-packages/django/utils/connection.py", line 15, in __getattr__
    return getattr(self._connections[self._alias], item)
  File "/Users/timo/opt/anaconda3/lib/python3.9/site-packages/django/utils/connection.py", line 62, in __getitem__
    conn = self.create_connection(alias)
  File "/Users/timo/opt/anaconda3/lib/python3.9/site-packages/django/db/utils.py", line 193, in create_connection
    backend = load_backend(db["ENGINE"])
  File "/Users/timo/opt/anaconda3/lib/python3.9/site-packages/django/db/utils.py", line 113, in load_backend
    return import_module("%s.base" % backend_name)
  File "/Users/timo/opt/anaconda3/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/Users/timo/opt/anaconda3/lib/python3.9/site-packages/django/db/backends/mysql/base.py", line 17, in <module>
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?

The settings.py file has the following database setting:

DATABASES = {
"default": {
    "ENGINE": "django.db.backends.mysql",
    "NAME": "musicians_users",
    "USER": "root",
    "PASSWORD": "",
    "HOST": "localhost",
    "PORT": "3306",
  }
}

Using MySql cli I can verify that the database does in fact exist.

edit: The strange thing is that mysqlclient was already installed using the following command: pip install mysqlclient

Requirement already satisfied: mysqlclient in /Users/timo/opt/anaconda3/lib/python3.9/site-packages (2.2.0)

Thanks.

Plegeus
  • 139
  • 11

2 Answers2

0

The issue was resolved by installing and running Django in a conda environment as well as mysqlclient.

I had another issue where some caching_sha2_password.so file was not found, this was resolved by creating a user as follows:

CREATE USER 'username'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

where username and password is the same you should use in settings.py. (Don't forget to grant privileges on the user.)

My DATABASES entry looks as follows:

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.mysql",
        "NAME": "musicians_users",
        "USER": "username",
        "PASSWORD": "password",
        "HOST": "127.0.0.1",
        "PORT": "3306",
    }
}

running python manage.py migrate now works when executed within the same conda environment.

Plegeus
  • 139
  • 11
-1

The error you're encountering indicates that the MySQLdb module, which is required for Django to interact with MySQL databases, is not properly loaded. This is usually caused by missing or incorrect installation of the mysqlclient package, which is the Python interface to MySQL.

Install mysqlclient:

Make sure you have mysqlclient installed in your Python environment. You can install it using pip. Run the following command in your terminal:

pip install mysqlclient

This will install the necessary package to enable Django to communicate with MySQL.

Mufasil
  • 303
  • 1
  • 4
  • 16
  • Thanks for your reply, I have forgotten to include this detail, but I did in fact already install mysqlclient using the exact command you propose. I will update the post to include this detail. – Plegeus Aug 30 '23 at 20:06
  • to be clear, the problem persists. – Plegeus Aug 30 '23 at 20:07