django默认使用的是sqlite,如果想使用mysql的话需要进行一些配置。
首先是安装mysql,安装过程就不说了。
其次是设置settings.py文件
settings默认是
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
修改
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'edu',
'USER':'root',
'PASSWORD':'root',
'HOST':'127.0.0.1'
}
}
name是数据库的表名,user和password是连接的用户名和密码
报错:django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb
module: No module named ‘MySQLdb’
这是因为还没有安装链接mysql的驱动
如果是python2.7可以输入pip install mysql-python
报错:Failed building wheel for mysql-python
_mysql.c(42): fatal error C1083: 无法打开包括文件: “config-win.h”: No such file or directory
error: command ‘D:\visual studio\VC\BIN\x86_amd64\cl.exe’ failed with exit status 2 到
网址https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python
下载对应的驱动
如果是python3输入pip install mysqlclient
报错Failed building wheel for mysqlclient
还是去上边的网站下载对应的驱动
然后直接pip install 下载的这个文件
比如说
pip install mysqlclient-1.3.13-cp36-cp36m-win_amd64.whl
完成后就安装成功了,可以连接到数据库了。
执行
`
`
python manage.py makemigrations
python manage.py migrate
可以看到数据库中新增加了一些表,这些都是django默认要用到的表。