系统运行环境:

macOS sonoma 14.7

Python 3.11.10

pip install mysqlclient (2.2.4 version)

遇到的问题:

运行 Python 程序连接本地的 MySQL,总是出现:

‘/opt/homebrew/opt/mysql-client/lib/libmysqlclient.23.dylib’ (no such file)

自行检查了路径,确实没有这个文件。

但是我已经通过 brew 包管理器进行了安装:

brew install mysql-client pkg-config

经过研究发现,brew 安装的包版本有更新,mysql-client 已经到了9.x 版本,与 libmysqlclient.23.dylib 版本不兼容。

即便你图省事,做 ln 软连接也无法正常使用,运行会报告错误。

我通过 brew 安装了 mysql-client@8.0 / mysql-client@8.4 和最新的 mysql-client@9,都不行,一定要 libmysqlclient.23.dylib 版本。

检索了一下答案,这个帖子帮助最大:

https://i-am-j.ag/blog/posts/python-mysql-8.3.0-homebrew/

Fix: Python’s mysqlclient broken with MySQL 8.3.0 on Mac with Homebrew

Feb 12, 2024 · 209 words · 1 minute read

At Hungryroot we use AWS’ Aurora MySQL with some of our Django projects. This morning, a few of our developers, working on Mac OS with Homebrew, reported that their mysqlclient libraries were erroring out with:

‘/opt/homebrew/Cellar/mysql-client/8.3.0/lib/libmysqlclient.22.dylib’ (no such file)

Attempts to reinstall the mysqlclient Python library were failing with:

clang -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/opt/homebrew/opt/mysql-client/include “-Dversion_info=(2, 2, 1, ‘final’, 0)” -D__version__=2.2.1 -I/Users/avishekde/.pyenv/versions/3.11.6/include/python3.11 -c src/MySQLdb/_mysql.c -o build/temp.macosx-14.1-arm64-cpython-311/src/MySQLdb/_mysql.o -I/opt/homebrew/Cellar/mysql-client/8.3.0/include/mysql -std=c99 src/MySQLdb/_mysql.c:527:9: error: call to undeclared function ‘mysql_ssl_set’; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] mysql_ssl_set(&(self->connection), key, cert, ca, capath, cipher); ^

It turns out that the recently released MySQL 8.3.0 removed some APIs that the Python mysqlclient depended upon, as documented in this GitHub issue.

It has been fixed in mysqlclient version 2.2.4, however you shouldn’t just go upgrading a core dependency like that without some testing and examination of the changelog. So to get yourself out of this mess:

Download this file – the Homebrew formula for MySQL 8.2.0

brew uninstall mysql-client

brew install ~/Downloads/mysql-client.rb (or modify with whatever path you saved the downloaded file to)

brew pin mysql-client

That should get you back in business.

Once you upgrade to 2.2.4, you should brew unpin mysql-client and let Homebrew resume managing that dependency.

我决定使用 mysql-client 8.3 版本测试。

先下载这个地址的安装文件,这个就是 8.3 版本:

https://github.com/Homebrew/homebrew-core/blob/aff76a194b0d0935e9614c98ca7496aef3ac59fd/Formula/m/mysql-client.rb

然后根据上面的提示进行安装,最后也有一点错误信息,但是并没有影响。

最后提示要求修改环境变量,照做就是,比如:

export PATH=”/opt/homebrew/opt/mysql-client/bin:$PATH”

export LDFLAGS=”-L/opt/homebrew/opt/mysql-client/lib:$LDFLAGS” export CPPFLAGS=”-I/opt/homebrew/opt/mysql-client/include:$CPPFLAGS”

export PKG_CONFIG_PATH=”/opt/homebrew/opt/mysql-client/lib/pkgconfig:$PKG_CONFIG_PATH”

后面重新安装一下 mysqlclient 包,可这样使用命令:

pip install –force-reinstall –no-cache mysqlclient

经过测试,问题完全解决了。

此外,如果使用 ‘pip install mysqlclient’ 安装失败,也可以用这个办法解决,操作一致。

为什么说Word这个软件很糟糕?
如何消除 macOS sonoma系统更新小红点?