常見問題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
安裝完後之後,即可解決。