MacOS 下安装mysqlclient 的问题及解决办法

【操作环境】
操作系统:MacOS X 10.13.1
mysql运行环境:Docker
Docker版本:17.09-ce


在开发Django时,刚开始使用的sqlite进行开发,想部署到生产环境需要连接到mysql上再跑一边测试。
为了不破坏整机的文件环境,我使用了Docker运行了Mysql。并没有通过源码的方式或brew命令来安装mysql。

在刚运行Django 的 migrate命令的时,便提示我不存在连接mysql的库,叫我安装mysqlclient
顺带一提,mysqlclient 是Python3中的MySQLdb, 它实现了与MySQLdb相兼容的接口,可以完美的替代MySQLdb,并且不用对代码做任何修改。

此时,我的电脑上没有任何关于mysql的文件(除了Docker镜像外)

运行 pip install mysqlclient 出现了下面的问题

Collecting mysqlclient
  Using cached mysqlclient-1.3.12.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/w2/xwm2lzmn55sfs0_s8gpxv5g00000gn/T/pip-build-y1_wya1q/mysqlclient/setup.py", line 17, in <module>
        metadata, options = get_config()
      File "/private/var/folders/w2/xwm2lzmn55sfs0_s8gpxv5g00000gn/T/pip-build-y1_wya1q/mysqlclient/setup_posix.py", line 44, in get_config
        libs = mysql_config("libs_r")
      File "/private/var/folders/w2/xwm2lzmn55sfs0_s8gpxv5g00000gn/T/pip-build-y1_wya1q/mysqlclient/setup_posix.py", line 26, in mysql_config
        raise EnvironmentError("%s not found" % (mysql_config.path,))
    OSError: mysql_config not found

大意便是说,我的系统中缺少了mysql_config文件。

于是我是用brew 安装了mysql-connector-c ,安装再次运行安装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/w2/xwm2lzmn55sfs0_s8gpxv5g00000gn/T/pip-build-59wqoy85/mysqlclient/setup.py", line 17, in <module>
        metadata, options = get_config()
      File "/private/var/folders/w2/xwm2lzmn55sfs0_s8gpxv5g00000gn/T/pip-build-59wqoy85/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/w2/xwm2lzmn55sfs0_s8gpxv5g00000gn/T/pip-build-59wqoy85/mysqlclient/setup_posix.py", line 54, in <listcomp>
        libraries = [dequote(i[2:]) for i in libs if i.startswith('-l')]
      File "/private/var/folders/w2/xwm2lzmn55sfs0_s8gpxv5g00000gn/T/pip-build-59wqoy85/mysqlclient/setup_posix.py", line 12, in dequote
        if s[0] in "\"'" and s[0] == s[-1]:
    IndexError: string index out of range

这个问题果然不是我一个人遇到的。在mysqlclient的issue上我找到了相应的解决办法
Issue #169
即修改mysql_config中的下列内容

# Create options 
libs="-L$pkglibdir"
libs="$libs -l "

修改为

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

修改完后就能够安装mysqlclient了呢。


修改mysql_config文件可能会遇到的问题:
mysql_config文件不可写。
在我的机器上,文件/usr/local/bin/mysql_config 为符号文件,通过ls命令,找到原来的文件,修改为可写文件,进行修改后,为防止被不小修改,再设为只读文件就好了。

posted @ 2017-11-19 19:21  pluviophile  阅读(3826)  评论(0编辑  收藏  举报