首先,在安装MySQLdb模块前可以做个测试,确认一下是否已经安装了MySQLdb模块
例如:

点击(此处)折叠或打开

  1. [root@localhost tmp]# python

  2. Python 2.6.(r266:84292, Dec 7 2011, 20:48:22) 

  3. [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2

  4. Type "help", "copyright", "credits" or "license" for more information.

  5. >>> import MySQLdb

  6. Traceback (most recent call last):

  7.   File "", line 1, in <module>

  8. ImportError: No module named MySQLdb

果然是没有安装啊。

1. 下载

安装MySQLdb模块,MySQLdb的下载页面下载

我下载的是MySQL-python-1.2.3.tar.gz
地址:http://jaist.dl.sourceforge.net/project/mysql-python/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz

2、安装依赖包:

yum -y install python-devel mysql-devel zlib-devel openssl-devel


3. 安装
理论安装步骤:
# tar xvf MySQL-python-1.2.3.tar.gz
# cd MySQL-python-1.2.3
# python setup.py build
# python setup.py install

不过实际遇到一些问题:

问题1:ImportError: No module named setuptools
好吧,需要安装setuptools
官网页面:https://pypi.python.org/pypi/setuptools#installation-instructions

> wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py
> python ez_setup.py --user


问题2:error: command 'gcc' failed with exit status 1
额。。。 新装的系统,啥都没装呢。。。
# yum -y install gcc


问题3:error: Python.h: No such file or directory
系统中没有Python.h的原因,是因为没有安装python的开发版,即Python-devel这个包,命令提示符下执行
yum install python-devel


问题4:/usr/bin/ld: cannot find -lz,造成编译错误
yum install zlib-devel

OK,这下终于build完成了。