在linux下安装MySQLdb模块

来源:互联网 发布:网络教育要几年 编辑:程序博客网 时间:2024/05/08 10:03
在linux下安装MySQLdb模块

在linux下安装MySQLdb还是有一些问题的,稍不注意可能就会老报错。这里我做一些简单的介绍。
首先,去下载MySQLdb的源码包,这里有: https://sourceforge.net/project/showfiles.php?group_id=22307

这里下的是 MySQL-python-1.2.2.tar.gz
下载完了后,解压缩

tar zxf MySQL-python-1.2.2.tar.gz

之后会产生一个叫 MySQL-python-1.2.2 的目录,进去。
这里就要做一些配置了,往往问题就在这个地方。
要想编译这个模块,就必须要先安装开发版的mysql,这里不说mysql怎么安装,假设装好了,并且装到/opt/mysql下。
这个目录下有一个叫 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 = True
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/local/bin/mysql_config

# The Windows registry key for MySQL.
# This has to be set for Windows builds to work.
# Only change this if you have a different version.
registry_key = SOFTWARE/MySQL AB/MySQL Server 5.0

以#开头的是注释,这里可能需要修改的有两个,一个是mysql_config,一个是threadsafe。往往编译不过就是这两个造成的。
mysql_config是指定mysql_config这个文件的路径,一般在mysql安装路径下,这里mysql装到/opt/mysql,因此取消mysql_config这行的注释,修改为
mysql_config=/opt/mysql/bin/mysql_config
一般来说这样就可以编译了。但是,如果在编译到最后时,报告链接不上libmysqlclient_r.so的话,还需要把threadsafe设置为False。

需要注意的就这两个。