macOS使用pip安装mysqlclient的问题与解

这片文章记录一下我在macOS上使用pip安装mysqlclient时遇到的问题与解决方式。

安装MySQL

MySQL下载需要的MySQL版本,这里我没有下载MySQL 8,而是下载的MySQL 5.7,因为当前mysqlclient的最新版本只支持MySQL的5.5-5.7版本。下载dmg格式的安装包,双击安装。完毕。

1
~ $ which mysql # 使用该命令查找不到mysql

配置MySQL的环境变量

1
~ $ nano ~/.bash_profile # 编辑bash_profile

.bash_profile的最后面添加,

1
2
# mysql
export PATH=$PATH:/usr/local/mysql/bin

ctr + x退出编辑并按y保存。

1
~ $ source ~/.bash_profile # 重载环境变量配置

安装mysqlclient

进入python的虚拟环境。

1
~ $ pip install mysqlclient # 使用pip安装mysqlclient

错误 mysql_config not found

错误提示

Google一下关键字 “Mac mysqlclient mysql_config not found”等,找到解决方式。

1
~ $ brew install mysql-connector-c # 也可以去mysql官网下载mysql-connector-c

安装 mysql-connector-c

1
2
~ $ which mysql_config  # 
/usr/local/bin/mysql_config

mysql连接器ok。

继续在python虚拟环境中安装mysqlclient。

1
~ $ pip install mysqlclient

错误 IndexError: string index out of range

错误提示

已经和第一次pip install mysqlclient的错误提示不一样了。

继续Google,找到解决方法。

解决方法

修改mysql_config

1
2
~ $ cd /usr/local/bin/mysql_config 
~ $ nano mysql_config

找到

1
2
3
# Create options 
libs="-L$pkglibdir"
libs="$libs -l "

改为

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

安装mysqlclient

1
~ $ pip install mysqlclient

终于成功。

成功

macOS使用pip安装mysqlclient的问题与解

https://oxo.red/install_mysqlclient_with_pip_on_macOS/

作者

Han

发布于

2018-05-08

更新于

2021-08-20

许可协议

评论