Python3.x + Django2.2.5 + Mysql8.x 常見問題 FAQ

問題一:連接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'

參考資料

https://stackoverflow.com/questions/54633968/2059-authentication-plugin-caching-sha2-password-when-running-server-conne

問題二:使用pip install mysqlclient命令安裝mysqlclient失敗?

mysqlclient模塊特殊,無法通過pip install mysqlclient直接安裝。

安裝方法

  1. 打開 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

  2. 執行命令pip install mysqlclient-1.4.4-cp37-cp37m-win32.whl,如圖所示,就表示安裝成功了。

    1

參考資料

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,安裝方法如上所示。

參考資料

https://stackoverflow.com/questions/55657752/django-installing-mysqlclient-error-mysqlclient-1-3-13-or-newer-is-required

問題四:提示 '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 安裝完後之後,即可解決。

參考資料
https://code.djangoproject.com/ticket/24355

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章