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