配置 Mysql
1. 安装 pymysql
pip3 install pymysql
2.设置项目中的 settings.py
import pymysql # 一定要添加这两行!通过 pip install pymysql!
pymysql.install_as_MySQLdb()
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mysite',
'HOST': '127.0.0.1',
'PORT': '3306',
'USER': 'root',
'PASSWORD': 'osk2014',
}
}

3.在数据库工具中新建 database

如果出现以下错误
错误 1
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
编辑以下文件
vim /home/rlysyy/Django/demo/lib/python3.6/site-packages/django/db/backends/mysql/base.py
注释以下图片中的两行代码

错误 2
AttributeError: 'str' object has no attribute 'decode'
编辑以下文件
vim /home/rlysyy/Django/demo/lib/python3.6/site-packages/django/db/backends/mysql/operations.py
将 decoe 修改为 encode

网友评论