mac安装mysqlclient出现的问题

最近学Python,需要用到mysqlclient,周边的人都使用的是Windows,大家安装的都挺轻松的,结果轮到我的时候,无论如何也下不下来,不停报错,最终在结合了多个相关文档,最终找到了解决方法:

首先是安装mysqlclient的方法:

pip install mysqlclient

但是在运行后报错,首先遇到的是

EnvironmentError: mysql_config not found

网上查了下,大致意思是说需要安装mysql,不能把,就连一下数据库,还需要下一个数据库?又继续看了下,原来是要装一个mysql的开发包就可以了(当然你安装mysql也是可以的)
通过brew安装 mysql-connector-c

brew install mysql-connector-c

其中还有可能会出现的问题,但中间出现的问题是各种各样的,根据报错进行安装即可

于是到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 / Ubuntu
sudo yum install python-devel mysql-devel # Red Hat / CentOS
brew 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-cand 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里面的mysql_config,路径如下:

/usr/local/Cellar/mysql-connector-c/6.1.11/bin/

打开里面的mysql_config

vim mysql_config

找到第114,115行:将里面的内容进行更换:

libs="-L$pkglibdir"
libs="$libs -l "

更改为

libs="-L$pkglibdir"
libs="$libs -lmysqlclient -lssl -lcrypto"

保存后退出
重新安装mysqlclient即可:

pip install mysqlclient

你可能感兴趣的:(疑难解答)