# django mysql 版本报错
## 解决方法:
Django 连接 MySQL 时默认使用 MySQLdb 驱动,但 MySQLdb 不支持 Python3,因此这里将 MySQL 驱动设置为 pymysql,使用 pip install pymysql 进行安装,然后在工程文件\_\_init\_\_.py 添加以下代码即可。
```shell
# 安装pymysql
pip3 install pymysql
```
```python
import pymysql
pymysql.install_as_MySQLdb()
```
### 第一种:
django 降到 2.1.4 版本就 OK 了
### 第二种(仍使用 django 2.2 版本):
```shell
#找到Python环境下 django包,并进入到backends下的mysql文件夹
cd ~/anaconda3/lib/python3.7/site-packages/django/db/backends/mysql
# 查看
ls
#文件列表如下
base.py creation.py introspection.py schema.py
client.py features.py operations.py validation.py
compiler.py __init__.py __pycache__
# 找到base.py文件,注释掉 base.py 中如下部分(35/36行)
vim base.py
```
修改以下内容:
```python
# if version < (1, 3, 3):
# raise ImproperlyConfigured("mysqlclient 1.3.3 or newer is required; you have %s" % # Database.__version__)
```
#找到`operations.py`文件(46行,版本不同行数不同哈~自个儿find一下),将`decode`改为`encode`, 然后 `vim operations.py`
```python
# 原始
if query is not None:
query = query.decode(errors='replace')
return query
#改为
if query is not None:
query = query.encode(errors='replace')
return query
```
一键复制
编辑
Web IDE
原始数据
按行查看
历史