安装pymysql
pip install pymysql
安装mysqlclient
  1. 安装libmysqld-dev
sudo apt install libmysqlclient
  1. 安装mysqlclient
pip install mysqlclient
修改django的MySQL驱动文件

/home/tqc/anaconda3/lib/python3.7/site-packages/django/db/backends/mysql__init__.py中, 把内容改为:

import pymysql
pymysql.install_as_MySQLdb()
sqlite向MySQL迁移

MySQL 从库

setting.py中, 修改:

DATABASES = {
    'default': {#slave
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },
    "slave": {#default
        "ENGINE": "django.db.backends.mysql",
        "NAME": "mysite",
        "USER": "root",
        "PASSWORD": "password",
        "HOST": "127.0.0.1",
        "PORT": "3306",
    },
}

加上MySQL的从库

建立MySQL数据库mysite

CREATE DATABASE mysite;

使用django的命令进行迁移

python manage.py migrate --database slave

setting.py中切换主库从库

python manage.py dumpdata > mysite_all_data.json
python manage.py loaddata mysite_all_data.json