-
Django開發項目時使用MySQL數據庫,然而部署到Ubuntu服務器的時候會經常出錯。
-
Django連接MySQL數據庫需要依賴第三方庫
mysqlclient
,然而服務器通過pip3 install mysqlclient
命令安裝第三方庫mysqlclient
時會經常報錯:
ERROR: Command errored out with exit status 1:
command: /home/bright/web/venv/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-ffv9cnlj/mysqlclient/setup.py'"'"'; __file__='"'"'/tmp/pip-install-ffv9cnlj/mysqlclient/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-install-ffv9cnlj/mysqlclient/pip-egg-info
cwd: /tmp/pip-install-ffv9cnlj/mysqlclient/
Complete output (12 lines):
/bin/sh: 1: mysql_config: not found
/bin/sh: 1: mariadb_config: not found
/bin/sh: 1: mysql_config: not found
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-install-ffv9cnlj/mysqlclient/setup.py", line 16, in <module>
metadata, options = get_config()
File "/tmp/pip-install-ffv9cnlj/mysqlclient/setup_posix.py", line 61, in get_config
libs = mysql_config("libs")
File "/tmp/pip-install-ffv9cnlj/mysqlclient/setup_posix.py", line 29, in mysql_config
raise EnvironmentError("%s not found" % (_mysql_config_path,))
OSError: mysql_config not found
----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
-
用
apt
安裝的 mysql 的 mysql_config 文件缺失了,或者說系統裏沒有mysql_config
文件,,,, -
解決方案:
sudo apt install libmysqlclient-dev
然後再:
pip3 install mysqlclient
發現:
Building wheels for collected packages: mysqlclient
Building wheel for mysqlclient (setup.py) ... done
Created wheel for mysqlclient: filename=mysqlclient-1.4.6-cp37-cp37m-linux_x86_64.whl size=100352 sha256=07ec9df2d857581eee041a6ae7740149919a265d19776c7eb8dbce47b4108324
Stored in directory: /home/bright/.cache/pip/wheels/42/b3/81/881eb2e7c762ba99ec0349eeb2b8b03760871295b5800d7a51
Successfully built mysqlclient
Installing collected packages: mysqlclient
Successfully installed mysqlclient-1.4.6
mysqlclient
安裝成功。
這樣即使服務器裏沒有安裝MySQL
數據庫,也不會報錯。