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()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值