Django的连接MySQL的问题,NameError的:名称'_mysql'未定义

时间:2020-09-17 14:30:53

标签: python mysql python-3.x django

我发现MySQL数据库无法正常启动,因此我卸载了Mysql 5.7。 我安装了MySQL 8.0.21,发现我的Django项目有问题。 我想在Django项目上进行makemigrations,然后在Django项目上遇到错误。请帮助,我不知道如何解决此错误。

数据包

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

错误消息

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

2 个答案:

答案 0 :(得分:0)

要在Django上使用MySQL的recommended way是安装mysqlclient而不是mysql-connector-python

如果使用mysql-connector-python,则需要在数据库设置(docs)中将ENGINE更改为'mysql.connector.django'

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'",
    },
}

答案 1 :(得分:-1)

pip install PyMySQL

#setting.py  
import pymysql  
pymysql.install_as_MySQLdb()