python连接mysql数据库

 

mysql-python的下载地址和安装过程:http://sourceforge.net/projects/mysql-python/files/mysql-python/1.2.3/

 

需要提前安装好的环境:

(1) Python 2.3.4 or higher

  * http://www.python.org/

(2) setuptools

  * http://pypi.python.org/pypi/setuptools

(3)MySQL 3.23.32 or higher

  * http://www.mysql.com/downloads/

 

 

 

常见错误:

 

(1)安装mysql-python时出现,EnvironmentError: mysql_config not found  错误
代码:root@vpser:~# cd MySQL-python-1.2.3
root@vpser:~/MySQL-python-1.2.3# python setup.py install
sh: mysql_config: not found
Traceback (most recent call last):
  File "setup.py", line 15, in <module>
    metadata, options = get_config()
  File "/root/MySQL-python-1.2.3/setup_posix.py", line 43, in get_config
    libs = mysql_config("libs_r")
  File "/root/MySQL-python-1.2.3/setup_posix.py", line 24, in mysql_config
    raise EnvironmentError("%s not found" % (mysql_config.path,))
EnvironmentError: mysql_config not found

 

首先查找mysql_config的位置,使用find / -name mysql_config ,比如我的在/usr/local/mysql/bin/mysql_config


修改setup_posix.py文件,在26行:
mysql_config.path = "mysql_config" 修改为:
mysql_config.path = "/usr/local/mysql/bin/mysql_config"


(2)安装完mysql-python后import加载模块 ImportError: libmysqlclient_r.so.16


安装完mysql-python后import加载模块提示以下错误,
ImportError: libmysqlclient_r.so.16: cannot open shared object file: No such file or directory


于是google之,总结一下解决方法:

将mysql/lib下所有关于libmysqlclient的so文件软链接到/usr/lib下。

ln -s /usr/local/mysql/lib/mysql/libmysqlclient* /usr/lib
ldconfig

这样 import MySQLdb 的时候就不会出错了

你可能感兴趣的:(python)