1. Configure MySQL database
For the method of installing MySQL database in Django, please refer to the previous article
. Field types and constraints of changing Django database to MySQL and ORM
After installation, we only need to modify the DATABASES list in the configuration file (dev.py here)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # 数据库引擎
'HOST': '127.0.0.1', # 数据库主机
'PORT': 3306, # 数据库端口
'USER': 'root', # 数据库用户名
'PASSWORD': '123456', # 数据库用户密码
'NAME': 'XXXX' # 数据库名字
},
}
2. Configure Redis database
Install django-redis
pip install django-redis -i https://pypi.tuna.tsinghua.edu.cn/simple/
Add Redis related configuration in the configuration file
CACHES = {
# 默认存储信息: 存到 0 号库
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/0",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
},
# session 信息: 存到 1 号库
"session": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
},
}
SESSION_ENGINE = "django.contrib.sessions.backends.cache"
SESSION_CACHE_ALIAS = "session"