macos11.2.3安装MySQL-python
前言:安装命令没有几个,但是安装过程踩坑无数,各种依赖库或命令报错,废话不多说 记录如下。
安装环境
系统:macos11.2.3 BigSur
python版本:Python 2.7.16(使用系统自带)
安装前说明
# 如果之前已安装多次MySQL-python,但是都失败了;或者说安装成功了,但是import MySQLdb失败,尽量先清理下已安装的,按下面操作下。
# brew list / pip list 查看是否已安装如下模块
brew uninstall MySQL-python
brew uninstall mysql-connector-c
brew uninstall mysql-client
pip uninstall mysql-client
安装mysql5.7
brew install mysql@5.7
brew list
安装openssl
brew install openssl@1.1
brew list
# 找到openssl@1.1的安装目录:
cd /opt/homebrew/Cellar/openssl@1.1/1.1.1k
pwd
安装MySQL-python
# 执行安装命令时,一定指定openssl库的位置
sudo env LDFLAGS="-I/opt/homebrew/Cellar/openssl@1.1/1.1.1k/include -L/opt/homebrew/Cellar/openssl@1.1/1.1.1k/lib" pip --no-cache install MySQL-python
pip list
验证
D-C02FA3TTQ05D:bin kevin$ python
WARNING: Python 2.7 is not recommended.
This version is included in macOS for compatibility with legacy software.
Future versions of macOS will not include Python 2.7.
Instead, it is recommended that you transition to using 'python3' from within Terminal.
Python 2.7.16 (default, Dec 21 2020, 23:00:36)
[GCC Apple LLVM 12.0.0 (clang-1200.0.30.4) [+internal-os, ptrauth-isa=sign+stri on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>>
>>> db = MySQLdb.connect("localhost", "root", "", "traffic_platform", charset='utf8')
>>>
>>> cursor = db.cursor()
>>>
>>> cursor.execute("SELECT VERSION()")
1L
>>> data = cursor.fetchone()
>>>
>>> print "Database version : %s " % data
Database version : 5.7.34
>>>
>>> db.close()