MySQLdb不支持Python3,所以Django连接MySQL不能使用MySQLdb,需要mysqlclient。
使用pip安装:
$ pip install mysqlclient
结果报错如下:
Collecting mysqlclient
Using cached https://files.pythonhosted.org/packages/4d/38/c5f8bac9c50f3042c8f05615f84206f77f03db79781db841898fde1bb284/mysqlclient-1.4.4.tar.gz
ERROR: Command errored out with exit status 1:
command: /home/starx/pythonvirtualenv/bbs_py37/bin/python3 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-sjupbyog/mysqlclient/setup.py'"'"'; __fil
e__='"'"'/tmp/pip-install-sjupbyog/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 pip-egg-info
cwd: /tmp/pip-install-sjupbyog/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-sjupbyog/mysqlclient/setup.py", line 16, in <module>
metadata, options = get_config()
File "/tmp/pip-install-sjupbyog/mysqlclient/setup_posix.py", line 61, in get_config
libs = mysql_config("libs")
File "/tmp/pip-install-sjupbyog/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.
根据mysqlclient的官方文档,在安装前需要先安装一些依赖:
sudo apt-get install python-dev default-libmysqlclient-dev
Python3用户还需要安装python3-dev,关于python3-dev的解释
sudo apt-get install python3-dev
之后再安装mysqlclient:
$ pip install mysqlclient
成功按照并可以runserver
了。