mac下python安装MySQLdb

python 安装经常需要pip命令。

安装方法:

下载文件内容保存到本地文件 get-pip.py

python get-pip.py 

文档地址:https://pip.pypa.io/en/stable/installing/

安装python-mysql 链接模块

sudo pip install mysql 

一般情况会报如下错误 没有找到mysql_config

Collecting mysql
  Downloading mysql-0.0.1.tar.gz
Collecting MySQL-python (from mysql)
  Using cached MySQL-python-1.2.5.zip
    Complete output from command python setup.py egg_info:
    sh: mysql_config: command not found
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/private/var/folders/h3/7bmkqlps1gs8q2dtc4kst80m0000gp/T/pip-build-1Rz4iz/MySQL-python/setup.py", line 17, in <module>
        metadata, options = get_config()
      File "setup_posix.py", line 43, in get_config
        libs = mysql_config("libs_r")
      File "setup_posix.py", line 25, in mysql_config
        raise EnvironmentError("%s not found" % (mysql_config.path,))
    EnvironmentError: mysql_config not found
    
    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/h3/7bmkqlps1gs8q2dtc4kst80m0000gp/T/pip-build-1Rz4iz/MySQL-python/

我的mysql的 mysql_config 在

/usr/local/mysql/bin/mysql_config

这里安装的时候mysql_config读取的地址为

/usr/local/bin/mysql_config

所以最简单的方法就是做个软连接

sudo ln -s /usr/local/mysql/bin/mysql_config /usr/local/bin/mysql_config

在次运行

sudo pip install mysql

安装成功

Collecting mysql
  Downloading mysql-0.0.1.tar.gz
Collecting MySQL-python (from mysql)
  Downloading MySQL-python-1.2.5.zip (108kB)
    28% |�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ                       | 30kB 63kB/s eta 0:0    37% |�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ                    | 40kB 84kB/s e    47% |�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ                 | 51kB 62    56% |�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ              | 6    65% |�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ            75% |�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–    84% |�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–    94% |�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–    100% |�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ�–ˆ| 112kB 61kB/s 
Installing collected packages: MySQL-python, mysql
  Running setup.py install for MySQL-python ... done
  Running setup.py install for mysql ... done
Successfully installed MySQL-python-1.2.5 mysql-0.0.1

在python代码中测试:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import MySQLdb

# 打开数据库连接
db = MySQLdb.connect("127.0.0.1","root","123456","58")

# 使用cursor()方法获取操作游标
cursor = db.cursor()

# SQL 插入语句
sql = "INSERT INTO test(id,name) VALUES (11,'11')"
try:
   # 执行sql语句
   cursor.execute(sql)
   # 提交到数据库执行
   db.commit()
   print "success"
except:
   # Rollback in case there is any error
   print "error"
   db.rollback()

# 关闭数据库连接
db.close()

结果:

/usr/bin/python /Users/jhbai/www/python/mysql.py
success

Process finished with exit code 0