MySQL API Drivers安装小记

前言

最近需要使用Django写点东西,由于自己的macbook上没有也不打算安装MySQL而是以Docker的MySQL镜像替代,Django文档提供了三种MySQL驱动供选择,官方推荐的是mysqlclient,由于我本地没有安装MySQL,所以是没有Native Driver的以至于在安装MySQL驱动的时候遇到了点小问题,在此记录下。


过程

安装mysqlclient

1
pip install mysqlclient

然而得到错误信息如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Collecting mysqlclient
Using cached mysqlclient-1.3.10.tar.gz
Complete output from command python setup.py egg_info:
/bin/sh: mysql_config: command not found
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/private/var/folders/ll/d2yyxp555ss1gm613y33hfy80000gn/T/pip-build-mcd2q8ly/mysqlclient/setup.py", line 17, in <module>
metadata, options = get_config()
File "/private/var/folders/ll/d2yyxp555ss1gm613y33hfy80000gn/T/pip-build-mcd2q8ly/mysqlclient/setup_posix.py", line 44, in get_config
libs = mysql_config("libs_r")
File "/private/var/folders/ll/d2yyxp555ss1gm613y33hfy80000gn/T/pip-build-mcd2q8ly/mysqlclient/setup_posix.py", line 26, in mysql_config
raise EnvironmentError("%s not found" % (mysql_config.path,))
OSError: mysql_config not found

----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/ll/d2yyxp555ss1gm613y33hfy80000gn/T/pip-build-mcd2q8ly/mysqlclient/

因为没有安装MySQL,所以在安装mysqlclient之前还需要安装Connector,如下:

1
brew install mysql-connector-c

之后安装再安装mysqlclient

1
pip install mysqlclient

然后又就报错了,错误信息如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Collecting mysqlclient
Using cached mysqlclient-1.3.10.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/ll/d2yyxp555ss1gm613y33hfy80000gn/T/pip-build-smwmu1qn/mysqlclient/setup.py", line 17, in <module>
metadata, options = get_config()
File "/private/var/folders/ll/d2yyxp555ss1gm613y33hfy80000gn/T/pip-build-smwmu1qn/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/ll/d2yyxp555ss1gm613y33hfy80000gn/T/pip-build-smwmu1qn/mysqlclient/setup_posix.py", line 54, in <listcomp>
libraries = [dequote(i[2:]) for i in libs if i.startswith('-l')]
File "/private/var/folders/ll/d2yyxp555ss1gm613y33hfy80000gn/T/pip-build-smwmu1qn/mysqlclient/setup_posix.py", line 12, in dequote
if s[0] in "\"'" and s[0] == s[-1]:
IndexError: string index out of range

----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/ll/d2yyxp555ss1gm613y33hfy80000gn/T/pip-build-smwmu1qn/mysqlclient/

解决

通过查找资料得出可能的结论是通过brew安装的mysql-connector-c配置可能不正确,打开/usr/local/bin/mysql_config脚本修改其中的部分内容:

1
2
3
# Create options
libs="-L$pkglibdir"
libs="$libs -l"

修改为:

1
2
3
# Create options
libs="-L$pkglibdir"
libs="$libs -lmysqlclient -lssl -lcrypto"

保存,再次安装mysqlclient应该就会正常安装了。接着就可以使用Django和运行在Docker中的MySQL愉快的Coding了~

0%