You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

安装mysqlclient时出现mysql_config未找到错误,求解决方法

问题描述

执行pip install mysqlclient时出现如下错误:

Collecting mysqlclient
  Using cached mysqlclient-2.1.1.tar.gz (88 kB)
  Preparing metadata (setup.py) ... error
  error: subprocess-exited-with-error
  
  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ╰─> [16 lines of output]
      /bin/sh: 1: mysql_config: not found
      /bin/sh: 1: mariadb_config: not found
      /bin/sh: 1: mysql_config: not found
      Traceback (most recent call last):
        File "<string>", line 2, in <module>
        File "<pip-setuptools-caller>", line 34, in <module>
        File "/tmp/pip-install-k_urbzi_/mysqlclient_beba1330b6804b2990fa74f56f11c3f5/setup.py", line 15, in <module>
          metadata, options = get_config()
        File "/tmp/pip-install-k_urbzi_/mysqlclient_beba1330b6804b2990fa74f56f11c3f5/setup_posix.py", line 70, in get_config
          libs = mysql_config("libs")
        File "/tmp/pip-install-k_urbzi_/mysqlclient_beba1330b6804b2990fa74f56f11c3f5/setup_posix.py", line 31, in mysql_config
          raise OSError("{} not found".format(_mysql_config_path))
      OSError: mysql_config not found
      mysql_config --version
      mariadb_config --version
      mysql_config --libs
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.

环境信息:

  • Python版本:Python 3.9.2
  • pip相关版本:
    Package       Version
    ---------------------
    pip           22.3
    pkg_resources 0.0.0
    setuptools    65.5.0
    
解决方案

错误根源是系统缺少MySQL/MariaDB的开发依赖包,mysql_config是这类依赖提供的工具,需先安装对应依赖:

Debian/Ubuntu 系列系统

执行以下命令安装依赖:

sudo apt-get install python3-dev default-libmysqlclient-dev build-essential

安装完成后重新执行:

pip install mysqlclient

RedHat/CentOS 系列系统

执行以下命令安装依赖:

sudo yum install python3-devel mysql-devel gcc

若为CentOS 8+版本,使用dnf命令:

sudo dnf install python3-devel mysql-devel gcc

之后重新运行pip install mysqlclient

macOS 系统

使用Homebrew安装依赖:

brew install mysql-client

临时将mysql_config加入环境变量:

export PATH="/usr/local/opt/mysql-client/bin:$PATH"

再执行pip install mysqlclient;也可直接指定路径安装:

pip install mysqlclient --global-option=build_ext --global-option="-I/usr/local/opt/mysql-client/include" --global-option="-L/usr/local/opt/mysql-client/lib"

内容的提问来源于stack exchange,提问作者CODE 232

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 19:35:36