如何使用Python 3.5和Django 2.2结合pymysql驱动连接MySQL数据库?
- 内容介绍
- 文章标签
- 相关推荐
本文共计286个文字,预计阅读时间需要2分钟。
1. 在项目同名文件夹下的`__init__.py`文件中添加以下代码即可: python import pymysql pymysql.install_as_MySQLdb()
2. 修改`settings.py`文件中的`DATABASES`配置: python DATABASES={ 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'your_database_name', } }
1,在project同名文件夹下的__init__文件中添加如下代码即可
import pymysql pymysql.install_as_MySQLdb(),
2,修改setting文件
DATABASES = { ‘default‘: { ‘ENGINE‘: ‘django.db.backends.mysql‘, ‘NAME‘: ‘test2‘, ‘USER‘:‘root‘, ‘PASSWORD‘:‘Gauss_234‘, ‘HOST‘:‘139.155.131.188‘, ‘POST‘:‘3306‘, } }
3,mysql授权
grant all privileges on test2.* to ‘root‘@‘139.155.131.188‘ identified "password" with grant option;
flush privileges;
4,如果这时候启动django会报错
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
修改\Python35\Lib\site-packages\django\db\backends\mysql\base.py
打开后将如下代码注释:
注释好了之后重新启动django服务器:
会出现以下报错信息、
AttributeError: ‘str‘ object has no attribute ‘decode‘
找到python文件下的django文件>db文件>backends>mysql>operations.py
将decode修改为encode
最后重启服务即可
参考链接:www.pianshen.com/article/2285376009/
本文共计286个文字,预计阅读时间需要2分钟。
1. 在项目同名文件夹下的`__init__.py`文件中添加以下代码即可: python import pymysql pymysql.install_as_MySQLdb()
2. 修改`settings.py`文件中的`DATABASES`配置: python DATABASES={ 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'your_database_name', } }
1,在project同名文件夹下的__init__文件中添加如下代码即可
import pymysql pymysql.install_as_MySQLdb(),
2,修改setting文件
DATABASES = { ‘default‘: { ‘ENGINE‘: ‘django.db.backends.mysql‘, ‘NAME‘: ‘test2‘, ‘USER‘:‘root‘, ‘PASSWORD‘:‘Gauss_234‘, ‘HOST‘:‘139.155.131.188‘, ‘POST‘:‘3306‘, } }
3,mysql授权
grant all privileges on test2.* to ‘root‘@‘139.155.131.188‘ identified "password" with grant option;
flush privileges;
4,如果这时候启动django会报错
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
修改\Python35\Lib\site-packages\django\db\backends\mysql\base.py
打开后将如下代码注释:
注释好了之后重新启动django服务器:
会出现以下报错信息、
AttributeError: ‘str‘ object has no attribute ‘decode‘
找到python文件下的django文件>db文件>backends>mysql>operations.py
将decode修改为encode
最后重启服务即可
参考链接:www.pianshen.com/article/2285376009/

