1. 程式人生 > >Django2.1更改資料庫型別為mysql報的錯

Django2.1更改資料庫型別為mysql報的錯

Django2.1更改資料庫型別為mysql報錯解決

Django預設是使用python自帶的splite3資料庫(小型輕量級關係資料庫),現在改為mysql,settings.py檔案如下:
在這裡插入圖片描述

執行報錯:

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?

找不到mysql程式,但我明明已經安裝了。參考這個:https://blog.csdn.net/qq_42776455/article/details/82959857


不管是java還是python,還是什麼語言連線資料庫都需要先載入資料庫驅動程式,所以就明白了這裡要安裝一個python-mysql的驅動pip install pymysql
注意要在虛擬環境裡安裝。

然後在__init__.py檔案中新增:

import pymysql
# 當成mysqldb一樣使用,當然也可以不寫這句,就按照pymysql的方式
pymysql.install_as_MySQLdb()

python 3.X中:PyMYSQL取代了MySQLdb。
個人覺得這兩者的用法就是名字變了,看個人選擇。

寫好之後就正常使用了。


如果還是失敗,重啟下pycharm,右下角會有一個提示,點選之後安裝driver

在這裡插入圖片描述
點選 Test Connection:
在這裡插入圖片描述
連線成功。

如果接著報錯資訊如下:

django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table ((1064, "You have an error in your SQL syntax; 
check the manual that corresponds to your MySQL server version for the right syntax to use near '(6) NOT NULL)' at line 1"))

哈哈哈,那麼可以告訴你了,Django2.1版本後不再支援mysql5.5了。