mac系统安装mysqlclient的一些坑(附解决方法)
最近在学习Django,做ORM模型实验时候,我看老师用到数据库了,然后windows电脑就 pip install mysqlclient 安装就完事了。
我的是mac,到我这就悲剧了,一个坑接着一个坑跳,我看网上有的人因为这个东西搞了一天的都有(手动“哭”)!
直接上方法,不错,就是mysqlclient源码github目录直接有解决方法,操作标红的部分即可。
--------------------------------------------------------
Install
Prerequisites
You may need to install the Python and MySQL development headers and libraries like so:
sudo apt-get install python-dev libmysqlclient-dev
# Debian / Ubuntusudo yum install python-devel mysql-devel
# Red Hat / CentOSbrew install mysql-connector-c
# macOS (Homebrew) (Currently, it has bug. See below)
On Windows, there are binary wheels you can install without MySQLConnector/C or MSVC.
Note on Python 3 : if you are using python3 then you need to install python3-dev using the following command :
sudo apt-get install python3-dev
# debian / Ubuntu
sudo yum install python3-devel
# Red Hat / CentOS
Note about bug of MySQL Connector/C on macOS
See also: https://bugs.mysql.com/bug.php?id=86971
Versions of MySQL Connector/C may have incorrect default configuration options that cause compilation errors when mysqlclient-python
is installed. (As of November 2017, this is known to be true for homebrew's mysql-connector-c
and official package)
Modification of mysql_config
resolves these issues as follows.
Change
# on macOS, on or about line 112:
# Create options
libs="-L$pkglibdir"
libs="$libs -l "
to
# Create options
libs="-L$pkglibdir"
libs="$libs -lmysqlclient -lssl -lcrypto"
--------------------------------------------------------
解决方案:
步骤一
安装mysql-connector-c
brew install mysql-connector-c
步骤二
修改mysql_config,这个文件在/usr/local/bin/mysql_config,实际在/usr/local/Cellar/mysql/8.0.23_1/bin/mysql_config
直接修改
# 记得sudo,不然没有权限,大概在文件120行左右(:set number 显示行号) sudo vi /usr/local/bin/mysql_config
# Create options
libs="-L$pkglibdir"
libs="$libs -l "
# 修改为
# Create options
libs="-L$pkglibdir"
libs="$libs -lmysqlclient -lssl -lcrypto"
步骤三
pip install mysqlclient
问题:
还遇到一个坑,客户端就是下载不下来。不用想了,换个源吧,我换了中科大的源解决了
解决方案:
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
参考链接:
https://my.oschina.net/u/914655/blog/1594267
https://zhuanlan.zhihu.com/p/111014448
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人