因为使用 Django 连接 mysql 需要 mysqlClient,安装过程可谓一波三折, 尝试使用 pip install mysqlclient, 提示未安装 mysql_config 报错如下:

1
2
3
4
5
6
7
8
9
10
/bin/sh: mysql_config: command not found
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/private/var/folders/bn/56s0mw415fq19vwqshrf6psr0000gn/T/pip-build-313xbn0o/mysqlclient/setup.py", line 17, in <module>
metadata, options = get_config()
File "/private/var/folders/bn/56s0mw415fq19vwqshrf6psr0000gn/T/pip-build-313xbn0o/mysqlclient/setup_posix.py", line 44, in get_config
libs = mysql_config("libs_r")
File "/private/var/folders/bn/56s0mw415fq19vwqshrf6psr0000gn/T/pip-build-313xbn0o/mysqlclient/setup_posix.py", line 26, in mysql_config
raise EnvironmentError("%s not found" % (mysql_config.path,))
OSError: mysql_config not found

使用 brew 安装 brew install mysql-connector-c
安装完成后输入 which mysql_config,输出路径如下,表示安装成功。

1
/usr/local/bin/mysql_config

然后继续用 pip install mysqlclient,又报错:

1
2
3
4
5
6
7
8
9
10
11
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/private/var/folders/bn/56s0mw415fq19vwqshrf6psr0000gn/T/pip-build-ec973xds/mysqlclient/setup.py", line 17, in <module>
metadata, options = get_config()
File "/private/var/folders/bn/56s0mw415fq19vwqshrf6psr0000gn/T/pip-build-ec973xds/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/bn/56s0mw415fq19vwqshrf6psr0000gn/T/pip-build-ec973xds/mysqlclient/setup_posix.py", line 54, in <listcomp>
libraries = [dequote(i[2:]) for i in libs if i.startswith('-l')]
File "/private/var/folders/bn/56s0mw415fq19vwqshrf6psr0000gn/T/pip-build-ec973xds/mysqlclient/setup_posix.py", line 12, in dequote
if s[0] in "\"'" and s[0] == s[-1]:
IndexError: string index out of range

打开 mysql_configvim /usr/local/bin/mysql_config, 找到如下位置:

1
2
3
4
# Create options
libs="-L$pkglibdir"
#libs="$libs -l " ##源代码
libs="$libs -lmysqlclient -lssl -lcrypto " ##修改成这样

完成后保存,继续执行 pip install mysqlclient, 又报错如下:

1
2
3
4
5
...
clang -bundle -undefined dynamic_lookup -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk build/temp.macosx-10.12-x86_64-2.7/_mysql.o -L/usr /local/Cellar/mysql/5.7.16/lib -lmysqlclient -lssl -lcrypto -o build/lib.macosx-10.12-x86_64-2.7/_mysql.so
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command 'clang' failed with exit status 1

呃 OpenSSL 肯定装了啊,执行 brew info openssl,注意提示

1
2
3
4
5
6
7
8
9
If you need to have this software first in your PATH run:
echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile

#这里提示需要设置环境变量
For compilers to find this software you may need to set:
LDFLAGS: -L/usr/local/opt/openssl/lib
CPPFLAGS: -I/usr/local/opt/openssl/include
For pkg-config to find this software you may need to set:
PKG_CONFIG_PATH: /usr/local/opt/openssl/lib/pkgconfig

再次执行 export LDFLAGS=-L/usr/local/opt/openssl/libexport CPPFLAGS=-I/usr/local/opt/openssl/include

在执行 pip install mysqlclient,终于可以开工了

1
2
3
4
5
6
7
8
Collecting mysqlclient
Using cached mysqlclient-1.3.12.tar.gz
Building wheels for collected packages: mysqlclient
Running setup.py bdist_wheel for mysqlclient ... done
Stored in directory: /Users/keith/Library/Caches/pip/wheels/df/bb/60/bf7c315cbe163515db1c846e4ffa5557dd785c82e82f3492e8
Successfully built mysqlclient
Installing collected packages: mysqlclient
Successfully installed mysqlclient-1.3.12