Python 之连接MySQL模块-MySQLdb

$ tar xfz MySQL-Python-1.2.1.tar.gz

  $ cd MySQL-python-1.2.1

  $ # edit site.cfg if necessary

  $ python setup.py build

  $ sudo python setup.py install # or su first

#上面这是文档上给的安装步骤

[root@zhu2 MySQL-python-1.2.3]# python setup.py build

Traceback (most recent call last):

  File "setup.py", line 5, in ?

    from setuptools import setup, Extension

ImportError: No module named setuptools

#这是由于python里缺少setuptools这个模块引起的。

#解决方法如下

https://pypi.python.org/pypi/setuptools#downloads

下载setuptools软件安装

wget https://pypi.python.org/packages/source/s/setuptools/setuptools-3.3.tar.gz#md5=87680a0eb0bb6f720d5e2d89ba67debc  --no-check-certificate


1.python连接mysql需要的模块MySQLdb默认是没有安装的,所以需要单独的安装后才能使用,安装的方法有编译安装和yum安装。虽是个小模块,但安装时也遇到了一些问题。

编译安装:

1.下载所需软件

http://sourceforge.net/projects/mysql-python/files/mysql-python/ #可在此地址下找到需要的版本。

wget http://downloads.sourceforge.net/project/mysql-python/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz

#解压

tar xf MySQL-python-1.2.3.tar.gz


2.安装

[root@jiang MySQL-python-1.2.3]# python setup.py build

Traceback (most recent call last):

  File "setup.py", line 5, in <module>

    from setuptools import setup, Extension

ImportError: No module named setuptools

#这个报错是由于缺少setuptools这个模块引起的,解决方法如下:

wget https://pypi.python.org/packages/source/s/setuptools/setuptools-3.3.tar.gz#md5=87680a0eb0bb6f720d5e2d89ba67debc

tar xf setuptools-3.3.tar.gz

python setup.py  build

python setup.py install


继续安装

unable to execute gcc: No such file or directory

error: command 'gcc' failed with exit status 1

#出现这个错误时,解决方法如下:

yum -y install python-devel mysql-devel gcc

解决该错误后继续安装

[root@jiang MySQL-python-1.2.3]# python setup.py build

[root@jiang MySQL-python-1.2.3]# python setup.py install

这样便可成功安装

Processing MySQL_python-1.2.3-py2.6-linux-i686.egg

Copying MySQL_python-1.2.3-py2.6-linux-i686.egg to /usr/lib/python2.6/site-packages

Adding MySQL-python 1.2.3 to easy-install.pth file

Installed /usr/lib/python2.6/site-packages/MySQL_python-1.2.3-py2.6-linux-i686.egg

Processing dependencies for MySQL-python==1.2.3

Finished processing dependencies for MySQL-python==1.2.3

检测该模块是否可成功使用

[root@jiang ~]# python

Python 2.6.6 (r266:84292, Jan 22 2014, 09:37:14)

[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2

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

>>> import MySQLdb


假如mysql使用的是编译安装,那么也可能碰到问题

mysql_config not found  #找不到mysql的安装路径

vim  setup_posix.py #找到第26行左右

mysql_config.path = "mysql_config"  #修改为对应的mysql安装路径即可,比如

/opt/mysql/bin/mysql_config

#######################

/usr/bin/ld: cannot find -lmysqlclient_r #这个错误是找不到mysql的库文件

解决方法:

echo "/usr/local/mysql/lib/mysql" >> /etc/ld.so.conf

ldconfig

如果没有libmysqlclient_r.so 就建立一个软连接 ln -s libmysqlclient.so libmysqlclient_r.so


###################################################################################################

yum安装更为便捷可选

yum -y install MySQL-python    #使用yum安装便不会出现那么多的包依赖问题了。

Python 的详细介绍:请点这里
Python 的下载地址:请点这里

推荐阅读:

相关推荐