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 | # mysql | 
按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 | ~ $ which mysql_config # | 
mysql连接器ok。
继续在python虚拟环境中安装mysqlclient。
| 1 | ~ $ pip install mysqlclient | 
错误 IndexError: string index out of range

已经和第一次pip install mysqlclient的错误提示不一样了。
继续Google,找到解决方法。

修改mysql_config
| 1 | ~ $ cd /usr/local/bin/mysql_config | 
找到
| 1 | # Create options | 
改为
| 1 | # Create options | 
安装mysqlclient
| 1 | ~ $ pip install mysqlclient | 
终于成功。

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