Linux下安装 MySQLdb模块 MySQL-python 坎坷之路


Linux下安装 MySQLdb模块 MySQL-python 坎坷之路

环境: 64位机器 + Python 2.7 +  MySQL-python-1.2.3 +  mysql-5.0.41-linux-x86_64-glibc23

(1) 首先需要安装 setuptools
tar zxf setuptools-0.6c11.tar.gz
cd setuptools-0.6c11
python setup.py build
python setup.py install

(2) 然后安装 MySQL-python
tar zxf MySQL-python-1.2.3.tar.gz
python setup.py install

出现如下错误:
sh: mysql_config: command not found
解决办法:将  /usr/local/mysql/bin 加入到 PATH

机器上的 /usr/local/mysql 是一个软链接,指向  /usr/local/mysql-5.0.41-linux-x86_64-glibc23

继续,出现如下错误:
gcc *** --march=i386 -mcpu=i686
_mysql.c:1: error: CPU you selected does not support x86-64 instruction set

我们的机器是 64 位的, 但这里gcc的选项 --march=i386, 所以出现上述错误,
解决办法是修改 setup.py 文件, options 修改 --march 选项

metadata, options = get_config()
print options
#options['extra_compile_args'] = ['-g', '-pipe', '-march=i386', '-mcpu=i686']
options['extra_compile_args'] = ['-g', '-pipe', '-mcpu=i686']

继续,出现如下错误:
gcc -pthread -shared build/temp.linux-x86_64-2.7/_mysql.o  -L/usr/lib/mysql -L/data/opt/Python-2.7.3/lib  -lmysqlclient_r -lz -lpthread -lcrypt -lnsl -lm -lpthread -lpython2.7 -o build/lib.linux-x86_64-2.7/_mysql.so
/usr/lib64/gcc/x86_64-suse-linux/4.1.2/../../../../x86_64-suse-linux/bin/ld: skipping incompatible /usr/lib/mysql/libmysqlclient_r.a when searching for -lmysqlclient_r
/usr/lib64/gcc/x86_64-suse-linux/4.1.2/../../../../x86_64-suse-linux/bin/ld: cannot find -lmysqlclient_r
collect2: ld returned 1 exit status

我们发现这里是  -L/usr/lib/mysql -lmysqlclient_r

而我们的mysql路径是 /usr/local/mysql

所以我们修改 setup.py, 修改 options 的 library_dirs, include_dirs.

options['library_dirs'] = ['/usr/local/mysql/lib']
options['include_dirs'] = ['/usr/local/mysql/include']

继续,安装,安装完毕
>>> import MySQLdb
>>>

成功.


你可能感兴趣的:(Python,MySQL)