Mac OS 安装mysqlcliet 失败问题解决


简单记录 Mac OS pip 安装mysqlclient报错处理过程
系统:

python: Python 3.8.2 虚拟环境

直接安装 mysqlclient 会报错

Using cached https://files.pythonhosted.org/packages/f4/f1/3bb6f64ca7a429729413e6556b7ba5976df06019a5245a43d36032f1061e/mysqlclient-1.4.2.post1.tar.gz
    ERROR: Complete output from command python setup.py egg_info:
    ERROR: /bin/sh: mysql_config: command not found
    Traceback (most recent call last):
      File "", line 1, in 
      File "/private/var/folders/6c/jdnqnjsx48d8qj0kwn751c580000gn/T/pip-install-bz44wlso/mysqlclient/setup.py", line 16, in 
        metadata, options = get_config()
      File "/private/var/folders/6c/jdnqnjsx48d8qj0kwn751c580000gn/T/pip-install-bz44wlso/mysqlclient/setup_posix.py", line 51, in get_config
        libs = mysql_config("libs")
      File "/private/var/folders/6c/jdnqnjsx48d8qj0kwn751c580000gn/T/pip-install-bz44wlso/mysqlclient/setup_posix.py", line 29, in mysql_config
        raise EnvironmentError("%s not found" % (_mysql_config_path,))
    OSError: mysql_config not found

报错的原因是:mysql_config not found

在网上找了一圈,解决办法:

brew install mysql

然后分步走:

  1. 查找mysql_config文件夹位置sudo find / -name mysql_config,一般在/usr/local/Cellar/mysql/8.0.27/bin/mysql_config这个路径下
  2. 将mysql_config链接到/usr/local/bin目录下 ln -s /usr/local/Cellar/mysql/8.0.27/bin/mysql_config /usr/local/bin/mysql_config

做完这两步,就可以在终端里打印出mysql_config。输入mysql_config --version,能够输出

mysql_config -version
Usage: /usr/local/bin/mysql_config [OPTIONS]
Compiler: AppleClang 12.0.0.12000032
Options:
        --cflags         [-I/usr/local/Cellar/mysql/8.0.27/include/mysql ]
        --cxxflags       [-I/usr/local/Cellar/mysql/8.0.27/include/mysql ]
        --include        [-I/usr/local/Cellar/mysql/8.0.27/include/mysql]
        --libs           [-L/usr/local/Cellar/mysql/8.0.27/lib -lmysqlclient -lz -lzstd -lssl -lcrypto -lresolv]
        --libs_r         [-L/usr/local/Cellar/mysql/8.0.27/lib -lmysqlclient -lz -lzstd -lssl -lcrypto -lresolv]
        --plugindir      [/usr/local/Cellar/mysql/8.0.27/lib/plugin]
        --socket         [/tmp/mysql.sock]
        --port           [0]
        --version        [8.0.27]
        --variable=VAR   VAR is one of:
                pkgincludedir [/usr/local/Cellar/mysql/8.0.27/include/mysql]
                pkglibdir     [/usr/local/Cellar/mysql/8.0.27/lib]
                plugindir     [/usr/local/Cellar/mysql/8.0.27/lib/plugin]

这时安装还会报错,如下:

In file included from MySQLdb/_mysql.c:29:
    In file included from /usr/local/Cellar/mysql/8.0.27/include/mysql/mysql.h:45:
    In file included from /Library/Developer/CommandLineTools/usr/lib/clang/12.0.0/include/stdint.h:52:
    In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/stdint.h:52:
    In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/sys/_types.h:32:
    /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/sys/cdefs.h:807:2: error: Unsupported architecture
    #error Unsupported architecture

原因是 架构不支持。痛苦的尝试了一段时间后,找个一个解决办法,声明架构

export ARCHFLAGS="-arch x86_64"

安装成功:

(dev) ?  bin pip install mysqlclient
Collecting mysqlclient
  Using cached mysqlclient-2.1.0.tar.gz (87 kB)
  Preparing metadata (setup.py) ... done
Building wheels for collected packages: mysqlclient
  Building wheel for mysqlclient (setup.py) ... done
  Created wheel for mysqlclient: filename=mysqlclient-2.1.0-cp38-cp38-macosx_10_14_x86_64.whl size=56114 sha256=4a7b05f15f6983798e57dddcee8f07a8e28e96f15f19a2800b2e9a3f657dc07e
  Stored in directory: /Users/lijinkui/Library/Caches/pip/wheels/61/e7/42/9d56347e42d7ce19397c0ca050c6bef56640e18be7021ac189
Successfully built mysqlclient
Installing collected packages: mysqlclient
Successfully installed mysqlclient-2.1.0

参考链接:
https://www.jianshu.com/p/6411c14ce3f1