linux和Windows上安装MySQLdb

1. Windows上安装MySQLdb

1.1 基于python2.7的安装

下载MySQL_python‑1.2.5‑cp27‑none‑win_amd64.whl

python install MySQL_python‑1.2.5‑cp27‑none‑win_amd64.whl

或者

pip install MySQL-python

1.2 基于python3.5的安装

MySQLdb的安装很曲折,还是按照正常方法安装,下载MySQL-python-1.2.5版本,解压后python setup.py install,发现怎么装都会报错“ConfigParser.NoSectionError: No section:'metadata'”,
是不是python3.5不支持MySQLdb了? 谷歌一下,果不其然。python3不再支持mysqldb。其替代模块是PyMySQL(下载地址:https://github.com/PyMySQL/PyMySQL),赶紧下载安装,修改好程序后,终于欢快的跑了起来。

python3 -m pip install PyMySQL

搞定。

2.linux上安装MySQLdb

2.1 基于python2.7的安装
首先知道一下内容:

my.cnf 在/etc/下,mysql_config在/usr/lib/mysql和/usr/bin下。
my.cnf是配置文件,存储数据库的位置,参数等信息。
mysql_config是命令,用于编译mysql客户端程序。

  • 1.下载MySQL-python-1.2.5版本
    https://pypi.python.org/pypi/MySQL-python

  • 2.安装依赖包
    yum -y install python-devel mysql-devel

  • 3.解压文件并修改site.cfg
    unzip MySQL-python-1.2.5.zip
    cd MySQL-python-1.2.5

找到本地MySQL安装目录下的mysql_config,将site.cfg文件中mysql_config修改为该地址(mysql_config = /usr/bin/mysql_config),并将threadsafe修改为False.

[root@iZuf6chjatdc17mvsvmdqtZ MySQL-python-1.2.5]# more site.cfg 
[options]
# embedded: link against the embedded server library
# threadsafe: use the threadsafe client
# static: link against a static library (probably required for embedded)

embedded = False
threadsafe = False
static = False

# The path to mysql_config.
# Only use this if mysql_config is not on your PATH, or you have some weird
# setup that requires it.
mysql_config = /usr/bin/mysql_config

# http://stackoverflow.com/questions/1972259/mysql-python-install-problem-using-virtualenv-wind
ows-pip
# Windows connector libs for MySQL. You need a 32-bit connector for your 32-bit Python build.
connector = C:\Program Files (x86)\MySQL\MySQL Connector C 6.0.2

  • 4.安装
    python setup.py build
    python setup.py install

2.2 基于python3.5的安装
python3 -m pip install PyMySQL

参考链接:# python的MySQLdb模块在linux环境下的安装

你可能感兴趣的:(linux和Windows上安装MySQLdb)