前言
django里mysql backend用的mysqlclient,所以使用的话需要安装,但安装比较坑比
错误过程-如果自己遇到了建议看下
以为就是一条命令的事
pip install mysqlclient
但接二连三的错误坑了有近2个小时
1. 首先遇到的是
EnvironmentError: mysql_config not found
网上查了下,大致意思是说需要安装mysql,不能把,就连一下数据库,还需要下一个数据库?又继续看了下,原来是要装一个mysql的开发包就可以了(当然你安装mysql也是可以的)
2. 于是
brew install mysql-connector-c
3. 再次 pip install mysqlclient
$ pip install mysqlclient
Collecting mysqlclient
Using cached mysqlclient-1.3.12.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/private/var/folders/y5/rz5w2dqx00s24rgcgdzyf9z80000gn/T/pip-build-jckrqk4x/mysqlclient/setup.py", line 17, in <module>
metadata, options = get_config()
File "/private/var/folders/y5/rz5w2dqx00s24rgcgdzyf9z80000gn/T/pip-build-jckrqk4x/mysqlclient/setup_posix.py", line 54, in get_config
libraries = [dequote(i[2:]) for i in libs if i.startswith('-l')]
File "/private/var/folders/y5/rz5w2dqx00s24rgcgdzyf9z80000gn/T/pip-build-jckrqk4x/mysqlclient/setup_posix.py", line 54, in <listcomp>
libraries = [dequote(i[2:]) for i in libs if i.startswith('-l')]
File "/private/var/folders/y5/rz5w2dqx00s24rgcgdzyf9z80000gn/T/pip-build-jckrqk4x/mysqlclient/setup_posix.py", line 12, in dequote
if s[0] in "\"'" and s[0] == s[-1]:
IndexError: string index out of range
还是报错,不至于这么复杂吧
正确过程
于是到mysqlclient源码github目录看了下,原来人家早就注明了?(所以出现问题第一找官网)
如下:
Install
Prerequisites
You may need to install the Python and MySQL development headers and libraries like so:
sudo apt-get install python-dev libmysqlclient-dev
# Debian / Ubuntusudo yum install python-devel mysql-devel
# Red Hat / CentOSbrew install mysql-connector-c
# macOS (Homebrew) (Currently, it has bug. See below)
On Windows, there are binary wheels you can install without MySQLConnector/C or MSVC.
Note on Python 3 : if you are using python3 then you need to install python3-dev using the following command :
sudo apt-get install python3-dev
# debian / Ubuntu
sudo yum install python3-devel
# Red Hat / CentOS
Note about bug of MySQL Connector/C on macOS
See also: https://bugs.mysql.com/bug.php?id=86971
Versions of MySQL Connector/C may have incorrect default configuration options that cause compilation errors when mysqlclient-python
is installed. (As of November 2017, this is known to be true for homebrew's mysql-connector-c
and official package)
Modification of mysql_config
resolves these issues as follows.
Change
# on macOS, on or about line 112:
# Create options
libs="-L$pkglibdir"
libs="$libs -l "
to
# Create options
libs="-L$pkglibdir"
libs="$libs -lmysqlclient -lssl -lcrypto"
修改mysql_config时因为是只读的,需要chmod 777 设置下,改完后再改回来
修改完后 再次安装ok了