django2.2连接mysql5.7遇到这个报错,参考大佬的博客解决问题,下面记录下自己的操作供日后参考。本篇博客参考了大佬的博客https://blog.youkuaiyun.com/weixin_33127753/article/details/89100552
我是T型人小付,一位坚持终身学习的互联网从业者。喜欢我的博客欢迎在csdn上关注我,如果有问题欢迎在底下的评论区交流,谢谢。
操作环境
- python 3.7.1
- Django 2.2.10
- mysql 5.7
- pymysql 0.9.3
错误现象
Pycharm连接到了mysql,进行迁移操作
(django) [fuhx@testmachine HelloDjango]$ python manage.py migrate
出现下列报错
File "/home/fuhx/python_projects/django/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 36, in <module>
raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3
报错解决
打开报错中提到的base.py
文件,把35,36行注释掉
# if version < (1, 3, 13):
# raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
再次尝试迁移,又出现下面的报错
File "/home/fuhx/python_projects/django/lib/python3.7/site-packages/django/db/backends/mysql/operations.py", line 146, in last_executed_query
query = query.decode(errors='replace')
AttributeError: 'str' object has no attribute 'decode'
python3中的str类型通过encode方法变为bytes类型,bytes类型通过decode方法变为str类型
将decode
改为encode,如下
if query is not None:
query = query.encode(errors='replace')
问题得到解决
验证解决
再次进行迁移,没有任何报错。刷新Pycharm中的mysql数据库,发现表已经生成了,说明迁移成功。