django 使用mysql数据库

 

 

一 修改settings里面的配置文件

import pymysql         # 一定要添加这两行!通过pip install pymysql! 或者pycharm 里面安装
pymysql.install_as_MySQLdb()
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mysite2',
        'USER':'root',
        'PASSWORD':'2GXXXXl',
        'HOST':'39.108.113.213',
        'PORT':'3306',
    }
}

二 报错

django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3

百度之后发现 找到对应项目的 djangodbackendsmysqlase.py文件

注释

if version < (1, 3, 3):
    raise ImproperlyConfigured("mysqlclient 1.3.3 or newer is required; you have %s" % Database.__version__)

例如

 

启动后又报错

django.db.utils.InternalError: (1049, "Unknown database 'mysite2'")   使用mysql数据库需要手动创建数据库

 重启继续报错

AttributeError: 'str' object has no attribute 'decode'

顺着报错文件点进去,找到query = query.decode(errors=‘replace’)
将decode修改为encode即可

 正常

继续报错

django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table ((1064, "You have an error in your SQL
syntax; check the manual that corresponds to your MySQL server ve

原因:django2.1 版本不再支持mysql5.5


原文地址:https://www.cnblogs.com/ZFBG/p/11277766.html