常见问题FAQ
- 问题一:连接Mysql出现 `django.db.utils.OperationalError: (2059, )`?
- 问题二:使用pip install mysqlclient命令安装mysqlclient失败?
- 问题三:启动Django出现`django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb. Did you install mysqlclient or MySQL-python`?
- 问题四:提示 `'mysql.connector.django' isn't an available database backend.` ?
问题一:连接Mysql出现 django.db.utils.OperationalError: (2059, <NULL>)
?
出现 [OperationalError: (2059, “Authentication plugin 'caching_sha2_password' cannot be loaded: The specified module could not be found.\r\n”)])
问题原因是 Mysql-8.x 版本,默认身份验证是 caching_sha2_password
。
网上有很多解决方式,其中是很多都是把身份验证改为其他方式,这是不安全的也是不建议的。
正确的是在 settings.py 中,把 'ENGINE': 'django.db.backends.mysql'
改为 'ENGINE': 'mysql.connector.django'
。
参考资料
问题二:使用pip install mysqlclient命令安装mysqlclient失败?
mysqlclient
模块特殊,无法通过pip install mysqlclient
直接安装。
安装方法
-
打开 https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient,下载对应的版本。对应的版本,如图所示。
cp27
表示Python2.7
cp35
表示Python3.5
cp36
表示python3.6
cp37
表示python3.7
cp38
表示python3.8
…以此类推win32
表示 windows 32位程序
win_amd64
表示 `windows 64位程序
我是Python3.7 32位程序,下载
mysqlclient-1.4.4-cp27-cp27m-win32.whl
-
执行命令
pip install mysqlclient-1.4.4-cp37-cp37m-win32.whl
,如图所示,就表示安装成功了。
参考资料
https://blog.csdn.net/cn_1937/article/details/81533544
问题三:启动Django出现django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb. Did you install mysqlclient or MySQL-python
?
解决方法
很多教程都推荐使用这个,但是官方是不推荐使用pymysql
的。本人用它也出现了另外一个错误 ImproperlyConfigured: mysqlclient 1.3.13 or newer is required
,所以还是果断放弃了。使用了官方推荐的 mysqlclient
,安装方法如上所示。
参考资料
问题四:提示 'mysql.connector.django' isn't an available database backend.
?
错误详情
django.core.exceptions.ImproperlyConfigured: 'mysql.connector.django' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
'mysql', 'oracle', 'postgresql', 'sqlite3'
解决方法
执行命令 pip install mysql-connector-python
安装完后之后,即可解决。