sqlite-mysql migrate

一、我的做法

设置2个参数,根据操作对象做切换。

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'lxg',
        'USER': 'myname',
        'PASSWORD': 'mypass',
        'HOST': '127.0.0.1',
        'PORT': '3306',
    }
}
DATABASES_bk = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

分别对2种数据库做 python manage.py migrate 

中间,会遇到问题,单比较容易解决。

sqlite下:导出sqlite

python manage.py dumpdata --natural-foreign --natural-primary -e contenttypes -e auth.Permission --indent 4 > datadump.json

 the --natural-foreign argument serializes foreign keys, since you are transitioning to a new database. 

The -e contenttypes -e auth.Permission arguments exclude tables that would cause Django to throw an IntegrityError.

切换到mysql下:执行 》

先创建mysql的库

mysql> DROP DATABASE lxg;
mysql> CREATE DATABASE lxg CHARACTER SET UTF8;
mysql> GRANT ALL PRIVILEGES ON lxg.* TO root@localhost;
mysql> FLUSH PRIVILEGES;
mysql> QUIT
python manage.py makemigrations   #根据情况,如果是custom migration时候可能不做这一步。
python manage.py migrate --run-syncdb  #--run-syncdb可以补充migrations缺失的内容
python manage.py loaddata datadump.json
1.创建user  :create user test identified by '123456';

2.给user授权,许可远程访问所有内容:grant all privileges on *.* to 'test'@'%'identified by '123456' with grant option;
3.flush privileges ;
4.update mysql.user set password=password('新密码') where User="test"

创建用户更简单的方式:
1.create user '用户名'@'localhost' identified by '密码';  # 本地登录 2.create user '用户名'@'%' identified by '密码';            # 远程登录

  

 

 
比较有用的资源

no1:https://justinmi.me/blog/2017/04/28/migrating-sql-databases

no2:https://stackoverflow.com/questions/3034910/whats-the-best-way-to-migrate-a-django-db-from-sqlite-to-mysql

no3:https://stackoverflow.com/questions/11546151/how-to-make-db-dumpfile-in-django

no4:Django如何把SQLite数据库转换为Mysql数据库,(中文讲的也挺好的)  https://www.django.cn/article/show-17.html

原文地址:https://www.cnblogs.com/lxgbky/p/12584376.html