django2.2连接mysql遇到的坑(亲测)

1、mysql数据库配置

django2.2连接mysql遇到的坑(亲测)

 

2、首先需要建一个myweb数据库

 

3、执行数据库迁移命令makemigrations

python manage.py makemigrations MySite

报错:

django2.2连接mysql遇到的坑(亲测)

主要是说没有安装mysql客户端;但是明明安装了,因为没有mysql那个包,只有pymysql

需要在项目的目录下的__init__.py 添加如下代码;此目录C:\Users\28277\Desktop\MyWebs\Lib\site-packages\django\db\backends\mysql 下的__init__.py

import pymysql
pymysql.install_as_MySQLdb()

django2.2连接mysql遇到的坑(亲测)

 

4、继续执行makemigrations迁移命令,又报错

django2.2连接mysql遇到的坑(亲测)

mysql版本太低了,进入C:\Users\28277\Desktop\MyWebs\Lib\site-packages\django\db\backends\mysql\base.py 文件

django2.2连接mysql遇到的坑(亲测)

35,36行需要注释掉,然后就不会因为版本而报错,具体啥版本问题是否更新mysql不懂

注释掉后:

django2.2连接mysql遇到的坑(亲测)

 

 5、继续执行迁移命令,还是报错了;

 

进入C:\Users\28277\Desktop\MyWebs\lib\site-packages\django\db\backends\mysql\operations.py

django2.2连接mysql遇到的坑(亲测)

decode 改为encode

修改后如下:

django2.2连接mysql遇到的坑(亲测)

 

6、继续执行迁移命令,正确

django2.2连接mysql遇到的坑(亲测)

来源:https://www.cnblogs.com/hanwenlin/p/10677026.html