3

I found MySQL database cannot start properly, so I uninstall Mysql 5.7. I installed MySQL 8.0.21, I found that my Django project has a problem. I would like makemigrations on my Django project and then I got an error on my Django project. Please help, I don't know how to solve this error.

Packet

Django==2.1.5
mod-wsgi==4.7.1
mysql-connector-python==8.0.21
mysqlclient==2.0.1
Python 3.7.9
MySQL Ver 8.0.21

Setting.py

DATABASES = {
    'default': {
                'ENGINE': 'django.db.backends.mysql',
                'HOST': 'xxxxxx.compute.amazonaws.com',
                'PORT': '3306',
                'NAME': 'xxxxx',
                'USER': 'xxxxx',
                'PASSWORD': 'xxxxxx',
                'init_command': "SET sql_modes = 'STRICT_TRANS_TABLES'",
    },
}
python3 manage.py makemigrations

Error message

Traceback (most recent call last):
  File "/var/www/html/Django_website/django/lib64/python3.7/site-packages/MySQLdb/__init__.py", line 18, in <module>
    from . import _mysql
ImportError: libmysqlclient.so.18: cannot open shared object file: No such file or directory

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/var/www/html/Django_website/django/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/var/www/html/Django_website/django/lib/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute
    django.setup()
  File "/var/www/html/Django_website/django/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/var/www/html/Django_website/django/lib/python3.7/site-packages/django/apps/registry.py", line 112, in populate
    app_config.import_models()
  File "/var/www/html/Django_website/django/lib/python3.7/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib64/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/var/www/html/Django_website/web/users/models.py", line 3, in <module>
    from django.contrib.auth.models import User
  File "/var/www/html/Django_website/django/lib/python3.7/site-packages/django/contrib/auth/models.py", line 2, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/var/www/html/Django_website/django/lib/python3.7/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
    class AbstractBaseUser(models.Model):
  File "/var/www/html/Django_website/django/lib/python3.7/site-packages/django/db/models/base.py", line 101, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/var/www/html/Django_website/django/lib/python3.7/site-packages/django/db/models/base.py", line 305, in add_to_class
    value.contribute_to_class(cls, name)
  File "/var/www/html/Django_website/django/lib/python3.7/site-packages/django/db/models/options.py", line 203, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/var/www/html/Django_website/django/lib/python3.7/site-packages/django/db/__init__.py", line 33, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/var/www/html/Django_website/django/lib/python3.7/site-packages/django/db/utils.py", line 202, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/var/www/html/Django_website/django/lib/python3.7/site-packages/django/db/utils.py", line 110, in load_backend
    return import_module('%s.base' % backend_name)
  File "/usr/lib64/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/var/www/html/Django_website/django/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 15, in <module>
    import MySQLdb as Database
  File "/var/www/html/Django_website/django/lib64/python3.7/site-packages/MySQLdb/__init__.py", line 24, in <module>
    version_info, _mysql.version_info, _mysql.__file__
NameError: name '_mysql' is not defined
0

4 Answers 4

7

The recommended way to use MySQL with Django is to install mysqlclient instead of mysql-connector-python.

If you use mysql-connector-python, then you need to change the ENGINE to 'mysql.connector.django' in your DATABASES setting (docs).

DATABASES = {
    'default': {
                'ENGINE': 'mysql.connector.django',
                'HOST': 'xxxxxx.compute.amazonaws.com',
                'PORT': '3306',
                'NAME': 'xxxxx',
                'USER': 'xxxxx',
                'PASSWORD': 'xxxxxx',
                'init_command': "SET sql_modes = 'STRICT_TRANS_TABLES'",
    },
}
Sign up to request clarification or add additional context in comments.

Comments

6
pip install PyMySQL

Django: settings.py

import pymysql  
pymysql.install_as_MySQLdb()

Source code pymysql.__init__.py

def install_as_MySQLdb():
    """
    After this function is called, any application that imports MySQLdb or
    _mysql will unwittingly actually use pymysql.
    """
    sys.modules["MySQLdb"] = sys.modules["_mysql"] = sys.modules["pymysql"]

1 Comment

Please explain what the code does and how it does it.
1

you need to instsall missing pkg by running this on ubuntu and then restart mysql

sudo apt-get install libmysqlclient-dev python-dev
sudo /etc/init.d/mysql restart

1 Comment

It says: python-dev has no installation candidate
0

I just had this problem . you should use

sudo apt-get install libmysqlclient-dev

if it said the package is missing then try :

sudo apt-get install libmariadb-dev

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.