首页 > 代码库 > python2.7之MySQLdb模块 for linux安装

python2.7之MySQLdb模块 for linux安装

1.下载:MySQL-python
http://sourceforge.net/projects/mysql-python/files/mysql-python-test/1.2.3b1/MySQL-python-1.2.3b1.tar.gz/download
tar -zxf MySQL-python-1.2.3b1.tar.gz
cd MySQL-python-1.2.3b1
python setup.py build
==>ImportError: No module named setuptools

2.下载setuptools
http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c8.tar.gz
tar -zxf setuptools-0.6c8.tar.gz
cd setuptools-0.6c8
python setup.py build
python setup.py install

3.回到MySQL-python-1.2.3b目录
cd MySQL-python-1.2.3b1
在运行时python setup.py build && python setup.py install没有如下错误:

有类似错误,处理方式如下:
python setup.py build #若,报错:mysql_config not found
找出mysql 安装目录:/opt/mysql/
vi MySQL-python-1.2.3b1/setup_posix.py
行     26 #mysql_config.path = "mysql_config"
改成如下: 
行     27 mysql_config.path = "/opt/mysql/bin/mysql_config"

再次运行 
python setup.py build #报错:/usr/bin/ld:cannot find -lmysqlclient_r
root下:
echo "/opt/mysql/lib/mysql" >> /etc/ld.so.conf #请主意这个/etc/ld.so.conf,
这个是ld的默认配置文件,视系统不同而不同。 /opt/mysql/lib/mysql与mysql安装目录相关。

刷新ld配置文件让其生效,使用:ldconfig

接着运行 python setup.py install

检验是否已经安装成功MySQLdb时,提示如下错误:
>>> import MySQLdb
/usr/lib/python2.4/site-packages/MySQL_python-1.2.3b1-py2.4-linux-i686.egg/_mysql.py:3: UserWarning: Module _mysql was already imported from /usr/lib/python2.4/site-packages/MySQL_python-1.2.3b1-py2.4-linux-i686.egg/_mysql.pyc, but /data/MySQL-python-1.2.3b1 is being added to sys.path
Traceback (most recent call last):
  File "", line 1, in ?
  File "MySQLdb/__init__.py", line 19, in ?
    import _mysql
  File "build/bdist.linux-i686/egg/_mysql.py", line 7, in ?
  File "build/bdist.linux-i686/egg/_mysql.py", line 6, in __bootstrap__
ImportError: libmysqlclient_r.so.16: cannot open shared object file: No such file or directory
解决方法:
vi /etc/profile下加:
export LD_LIBRARY_PATH=/opt/mysql/lib/mysql:$LD_LIBRARY_PATH

python2.7之MySQLdb模块 for linux安装