准备: 云服务器上安装mysql
本地虚拟环境安装mysqlclient(下载地址): python -m pip install mysqlclient-1.4.4-cp36-cp36m-win_amd64.whl
mysql数据设置
# 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;
修改博客项目下mysitesettings.py
DATABASES = { ‘default‘: { ‘ENGINE‘: ‘django.db.backends.mysql‘, ‘NAME‘: ‘数据库名‘, ‘USER‘: ‘用户名‘, ‘PASSWORD‘: ‘密码‘, ‘HOST‘: ‘IP‘, ‘PORT‘: ‘端口号‘, } }
迁移数据库
迁移数据库: python manage.py migrate
生成缓存表:python manage.py createcachetable
运行项目并查看启动情况:python manage.py runserver
迁移数据
1. 还原settings.py的DATABASES为sqlite,把数据导出到data.json中: python manage.py dumpdata > data.json
DATABASES = { ‘default‘: { ‘ENGINE‘: ‘django.db.backends.sqlite3‘, ‘NAME‘: os.path.join(BASE_DIR, ‘db.sqlite3‘), } }
2. 先删除MySQL中表django_content_type和auth_permission的所有数据,防止导入数据报错数据重复
输入: delete from auth_permission;
delete from django_content_type;
3. 再把settings.py的DATABASES修改回mysql,并导入数据输入python manage.py loaddata data.json
4. mysql加载时区表,命令行执行: mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql -p