将Django2中SQLite迁移到Mysql,折腾

用SQLiteStudio导出SQLite3中的数据为sql文件,不能直接导入mysql,各种语法问题,修改了30min还是失败,心累。

于是直接在mysql中重新建数据库,并修改Settings设置,连接到mysql中,直接执行数据库同步。

# python3 + Django 2.1
python manage.py migrate   

 报错,“No module named 'MySQLdb'”,查了下Django连接MySQL数据库时,默认使用MySQLdb驱动;

然而python3使用的是PyMySQL,需要修改与settings同目录下的__init__.py文件:

import pymysql
pymysql.install_as_MySQLdb()

(神奇Django使用python3写的,连接Mysql时,还需要自己适配python3数据库连接驱动,这难道不是BUG?)

再敲:python manage.py migrate   ,又报错了:

django.db.utils.OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: YES)")

网上查了,全都是说去看settings配置啊什么的,我看了不下5遍,配置绝对没有问题,但就是1045,啊西巴 !

不信邪,我用workbench按照配置连接了下数据库,发现也“(using password: YES)”,哇,哦噶西,昨天还好好的,

灵光一闪,疯狂操作一波:

1、cmd用管理员身份进入,然后输入:

mysqladmin -u root -p password root

2、启动mysql并登入:
mysql -u root -p

3、登入成功后mysql > 样式中输入 
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';

4、成功后最后记得输入 

FLUSH PRIVILEGES;

确定workbench可以连接后,再敲:python manage.py migrate,啊咧,居然好了,问题解决了!

workbench查看数据中的表都新建好了,高兴。

然后创建管理员账号:

python manage.py createsuperuser

在Settings 中配置好后,Django2.1迁移Mysql,并新建了所有表,运行:python manage.py runserver

没报错,但出现如下警告:

Warning: (3135, "'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.")
  result = self._query(query)

强迫症,受不了这个警告,查了下,做如下修改:(最后的配置)

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'my_test',
        'USER': 'root',
        'PASSWORD': '****',
        'HOST': 'localhost',
        'PORT': '3306',
        'OPTIONS': {
            'init_command': "SET sql_mode='traditional'",
        },
    }
}

猜你喜欢

转载自blog.csdn.net/xingzishuai/article/details/81778319