Please read the official document: https: //docs.djangoproject.com/en/1.11/ref/databases/#mysql-db-api-drivers
Configure MySQL database
1. Create a new MySQL database
1. Create a new MySQL database:
xxx_project
$ create database xxx_project charset=utf8;
2. New MySQL user
$ create user username identified by '123456';
3. authorized
itcast
users access xxx_project database
$ grant all on xxx_project.* to 'username'@'%';
4. After the authorization privileges refresh
$ flush privileges;
2. Configure MySQL database
= {DATABASES 'default': { 'ENGINE': 'django.db.backends.mysql', Database Engine # 'HOST': '127.0.0.1', # database host 'PORT': 3306, # database port 'USER' : 'username', # database user name 'pASSWORD': '123456', # database user password 'nAME': 'xxx_project' # database name } }
Possible errors
- Error loading MySQLdb module: No module named 'MySQLdb'.
The error occurs:
- Django MySQL database operation requires a driver MySQLdb
- At present the project in a virtual environment is no driver MySQLdb
Solution:
- Installation PyMySQL Expansion Pack
- Because MySQLdb applies only to Python2.x version, Python3.x versions use PyMySQL alternative MySQLdb
3. Install the extension package PyMySQL
1. Install Driver
$ pip install PyMySQL
2. In the subdirectory of the project of the same name
__init__.py
file, add the following code:
import pymysql pymysql.install_as_MySQLdb()
After the configuration: run the program, the test results.