首先說(shuō)一下,mysqlclient現(xiàn)在支持python3琼牧,mysqlclient具體哪個(gè)版本支持的不太清楚。想的自己去看下文檔說(shuō)明
This project adds Python 3 support and bug fixes. I hope this fork is merged back to MySQLdb1 like distribute was merged back to setuptools.
原來(lái)Django一直使用的Mondb數(shù)據(jù)庫(kù),由于項(xiàng)目需要使用mysql喜爷,配置mysql時(shí)一直失敗,報(bào)錯(cuò)提示
django.core.exceptions.ImproperlyConfigured:
mysqlclient 1.3.13 or newer is required; you have 0.9.3
上網(wǎng)搜了半天萄唇,說(shuō)是將報(bào)錯(cuò)的代碼注釋掉檩帐,注釋掉后又會(huì)出現(xiàn)其他問(wèn)題。
在這里提供一個(gè)其他的解決方法
大多數(shù)人使用Django配置mysql數(shù)據(jù)庫(kù)使用的是pymysql庫(kù)另萤,django默認(rèn)使用的是mysqlclient湃密。那么為什么不用mysqlclient呢?
在cmd命令行里更新mysqlclient庫(kù)
pip install --upgrade mysqlclient
在setting.py里配置mysql
DATABASES = {
'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'ENGINE': 'django.db.backends.mysql',
'NAME': '數(shù)據(jù)庫(kù)名稱'
'USER': '用戶名',
'PASSWORD': '用戶密碼',
'HOST': 'localhost',
'PORT': '3306',
}
}
配置完成后執(zhí)行遷移命令
python manage.py makemigrations
python manage.py migrate
此時(shí)就可以正常執(zhí)行四敞,不會(huì)報(bào)錯(cuò)了
更新一個(gè)泛源,這段時(shí)間開(kāi)發(fā)環(huán)境從win10換到了mac,再配置的時(shí)候發(fā)現(xiàn)在mac下會(huì)出一些其他的問(wèn)題忿危。下面是mac的解決方案
我的python版本和pip版本
misslovedeMBP:lib misslove$ python3
Python 3.7.4 (v3.7.4:e09359112e, Jul 8 2019, 14:54:52)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
misslovedeMBP:lib misslove$ pip3 -V
pip 19.2.3 from /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pip (python 3.7)
使用pip命令安裝django和mysqlclient
pip3 install -i https://pypi.douban.com/simple/ django
pip3 install -i https://pypi.douban.com/simple/ mysqlclient
安裝完后使用python3 manage.py runserver會(huì)報(bào)錯(cuò)誤
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?
明明安裝了mysqlclient卻還是報(bào)錯(cuò)达箍,嘗試在python3里引用MySQLdb庫(kù)發(fā)現(xiàn)報(bào)錯(cuò)
>>> import MySQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/MySQLdb/__init__.py", line 18, in <module>
from . import _mysql
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so, 2): Library not loaded: @rpath/libmysqlclient.21.dylib
Referenced from: /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so
Reason: image not found
主要看下面這一個(gè)報(bào)錯(cuò),我這里因?yàn)橐呀?jīng)改了一些東西铺厨,可能報(bào)錯(cuò)不一樣缎玫,但都是lib***.dylib。意思是在/usr/lib目錄下找不到這個(gè)文件解滓。我們安裝mysql時(shí)肯定是有的赃磨,所以建立幾個(gè)軟鏈接指向它就可以了。
Library not loaded: @rpath/libmysqlclient.21.dylib
我的mysql安裝在 /usr/local/mysql-8.0.17-macos10.14-x86_64/洼裤,所以在這個(gè)目錄下找lib目錄煞躬,進(jìn)去后會(huì)發(fā)現(xiàn)有剛才報(bào)錯(cuò)缺失的文件
misslovedeMBP:lib misslove$ cd /usr/local/mysql-8.0.17-macos10.14-x86_64/
misslovedeMBP:mysql-8.0.17-macos10.14-x86_64 misslove$ ls
LICENSE README.router docs lib share
LICENSE.router bin include man support-files
README data keyring run var
misslovedeMBP:mysql-8.0.17-macos10.14-x86_64 misslove$ cd lib/
misslovedeMBP:lib misslove$ ls
libcrypto.1.0.0.dylib libmysqlrouter_http.1.dylib
libcrypto.dylib libmysqlrouter_http.dylib
libmysqlclient.21.dylib libmysqlservices.a
libmysqlclient.a libssl.1.0.0.dylib
libmysqlclient.dylib libssl.dylib
libmysqlharness.1.dylib mecab
libmysqlharness.a mysqlrouter
libmysqlharness.dylib pkgconfig
libmysqlrouter.1.dylib plugin
libmysqlrouter.dylib
找到具體位置后就可以在/usr/lib目錄下建立軟鏈接。建立完成后可以到/usr/lib目錄下使用ls -al命令查看建立的鏈接
sudo ln -s /usr/local/mysql-8.0.17-macos10.14-x86_64/lib/libmysqlclient.21.dylib /usr/lib/libmysqlclient.21.dylib
misslovedeMBP:lib misslove$ ls -al
lrwxr-xr-x 1 root wheel 69 9 8 15:26 libmysqlclient.21.dylib -> /usr/local/mysql-8.0.17-macos10.14-x86_64/lib/libmysqlclient.21.dylib
建立完成后再去python里import MySQLdb可能會(huì)報(bào)其他的文件問(wèn)題逸邦,復(fù)制文件名恩沛,建立軟鏈接就可以了。
除了這個(gè)問(wèn)題還會(huì)有一個(gè)新的問(wèn)題缕减,報(bào)找不到MySQLdb庫(kù)雷客,說(shuō)實(shí)話我看到了也一臉懵逼,發(fā)現(xiàn)退出重新再打開(kāi)就好桥狡,或者重啟終端搅裙≈遄浚可能需要讓系統(tǒng)底層代碼跑一會(huì)兒?
misslovedeMBP:lib misslove$ python3
Python 3.7.4 (v3.7.4:e09359112e, Jul 8 2019, 14:54:52)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import MqSQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'MqSQLdb'
>>> exit()
misslovedeMBP:platform_static misslove$ python3
Python 3.7.4 (v3.7.4:e09359112e, Jul 8 2019, 14:54:52)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>>
此時(shí)再python3 manage.py runserver就沒(méi)問(wèn)題了部逮。
下面這個(gè)問(wèn)題是因?yàn)槲野褀in10下的整個(gè)文件夾復(fù)制粘貼到mac下的娜汁,執(zhí)行一下python manage.py migrate命令就可以了,具體命令時(shí)做什么的自行百度兄朋。我就不說(shuō)了掐禁,因?yàn)槲乙仓恢雷饔茫v不清概念
misslovedeMBP:platform_static misslove$ python3 manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
September 08, 2019 - 07:44:10
Django version 2.2.5, using settings 'platform_static.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.