Preparation: mounting on the cloud server mysql => the Ubuntu install and configure a remote connection MySQL
Local virtual environment installation mysqlclient ( Download ): python -m pip install mysqlclient- 1.4.4-cp36-cp36m-win_amd64.whl
mysql data set
# root账号登录
mysql -u root -p
# 创建数据库 — mysite_db
create database mysite_db default charset=utf8mb4 default collate utf8mb4_unicode_ci;
# 创建账号设置密码并赋予任意主机访问权限
create user 'user'@'%' identified by 'password';
# 添加权限到数据库(mysite_db)
GRANT ALL PRIVILEGES ON mysite_db.* TO 'user'@'%'IDENTIFIED BY 'password' WITH GRANT OPTION;
# 刷新权限(授权之后必须刷新权限才生效)
FLUSH PRIVILEGES;
Modify the blog project under \ mysite \ settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '数据库名',
'USER': '用户名',
'PASSWORD': '密码',
'HOST': 'IP',
'PORT': '端口号',
}
}
Database migration
Migrating database: python manage.py migrate
Generate cache: python manage.py createcachetable
Run the project and see the start situation: python manage.py runserver
Migrating data
1. Restore the settings.py DATABASES is sqlite, export the data into the data.json: python manage.py dumpdata> data.json
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
2. delete MySQL table django_content_type and all data auth_permission prevent duplication of data import data error
输入: delete from auth_permission;
delete from django_content_type;
3. Then the settings.py DATABASES changes back mysql, and import data input python manage.py loaddata data.json
4. mysql load the time zone tables, command line execution: mysql_tzinfo_to_sql / usr / share / zoneinfo | mysql -u root mysql -p