应用MySQL数据库,用pymysql的设置方法
修改 settings.py中数据库的设置:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', #数据库引擎 必填
'NAME': ' ', #数据库名 必填
'USER': 'root', #用户名 必填
'PASSWORD': '', #密码 必填
'HOST': '', #数据库主机,默认为localhost
'PORT': '', #数据库端口,MySQL默认为3306
'OPTIONS': {
'autocommit': True,
},
}
}
在__init.py__中增加:
import pymysql
pymysql.install_as_MySQLdb()
在mysite目录下运行命令行:
python manage.py migrate
报错:
AttributeError: 'module' object has noattribute 'install_as_MySQLdb'
通过检查,发现pymysql模块正确安装,setting.py中设置得数据库名称、密码无误;最后通过查阅github中(https://github.com/PyMySQL/PyMySQL/issues/349)提到重装pymysql:
pip install -U --force-reinstall pymysql
问题解决,重新运行
python manage.py migrate
。