//系统版本
]# lsb_release -a
LSB Version: :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
Distributor ID: RedHatEnterpriseServer
Description: Red Hat Enterprise Linux Server release 6.4 (Santiago)
Release: 6.4
Codename: Santiago
//mysql的rpm安装情况
]# rpm -qa | grep -i mysql
MySQL-server-advanced-5.6.22-1.rhel5.x86_64
MySQL-devel-advanced-5.6.22-1.rhel5.x86_64
MySQL-client-advanced-5.6.22-1.rhel5.x86_64
1. //下载源码包
https://pypi.python.org/pypi/MySQL-python/
我下载的是1.2.3
2. 解压:tar zxvf MySQL-python*
]# pwd
/u01/app/mysql-python/MySQL-python-1.2.3
[root@clone2_lsb_oracle MySQL-python-1.2.3]# ls
build ez_setup.py metadata.cfg _mysql_exceptions.py PKG-INFO setup.cfg setup_posix.py setup_windows.py
dist HISTORY _mysql.c _mysql_exceptions.pyc pymemcompat.h setup_common.py setup_posix.pyc site.cfg
doc MANIFEST.in MySQLdb MySQL_python.egg-info README setup_common.pyc setup.py tests
[root@clone2_lsb_oracle MySQL-python-1.2.3]# ?
3. 安装
3.1 之前遇到的问题
安装时
/usr/include/python2.6/pyconfig-64.h:808:1: warning: this is the location of the previous definition
gcc -pthread -shared build/temp.linux-x86_64-2.6/_mysql.o -L/usr/lib64 -L/usr/lib64 -lmysqlclient -lpthread -lm -lrt -ldl -lstdc++ -lpython2.6 -o build/lib.linux-x86_64-2.6/_mysql.so
/usr/bin/ld: cannot find -lmysqlclient
collect2: ld returned 1 exit status
error: command 'gcc' failed with exit status 1
使用时
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> import MySQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "MySQLdb/__init__.py", line 19, in <module>
import _mysql
ImportError: /usr/lib64/python2.6/site-packages/MySQL_python-1.2.5-py2.6-linux-x86_64.egg/_mysql.so: undefined symbol: __gxx_personality_v0
>>>
问题1是:
在编译时找不到连接文件mysqlclient.so,为什么没有?因为编译时没生成。 根因是编译MySQLdb时,指向的依赖的mysql路径不对!!
方案a 建立软连接文件:ln -s /usr/lib64/mysql/libmysqlclient.a /usr/lib64/libmysqlclient.a
(推荐)方案b 修改依赖的mysql路径:
找到find / -name 'mysql_config'
我的是/usr/bin/mysql_config
修改]# vi setup_posix.py ,在第26行,指定依赖的mysql_config配置文件
25 return data
26 mysql_config.path = "/usr/bin/mysql_config"
再修改]# vi /usr/bin/mysql_config ,在所有 -lmysqlclient前面加上实际依赖的mysql路径,我的是/user/lib64/mysql
114 libs=" $ldflags -L$pkglibdir -L/usr/lib64/mysql -lmysqlclient -lpthread -lm -lrt -ldl "
115 libs="$libs "
116 libs_r=" $ldflags -L$pkglibdir -L/usr/lib64/mysql -lmysqlclient -lpthread -lm -lrt -ldl "
问题2是:
没链接libstdc++.so,用gcc编译和链接c++文件,没有自动链接上c++的标准库。
]# vi setup_posix.py ,修改 setup_posix.py 为编译时加上stdc++,
86 create_release_file(metadata)
87 del metadata['version_info']
88 libraries.append('stdc++')
89 ext_options = dict(
注意如果之前编译过,请删除setup_posix.pyc字节文件(重新编译会再生成)
3.2 编译安装
MySQL-python-1.2.3]# python setup.py build
MySQL-python-1.2.3]# python setup.py install
如果安装过程出错,要重新安装/安装另外的版本,请查看 egg和easy_install卸载安装的egg
//测试dbDump.py
import sys
import MySQLdb
COLSIZ=10
def dbDump(cur):
cur.execute('SELECT * FROM t1')
print '\n%s%s' % ('NAME'.ljust(COLSIZ),'ID'.ljust(COLSIZ))
for data in cur.fetchall():
print '%s%s' % tuple([str(s).title().ljust(COLSIZ) \
for s in data])
cxn = MySQLdb.connect(host="192.168.xxx.xxx",port=3306,db="hivemeta",user="userxxx",passwd="pdxxx")
cur = cxn.cursor()
dbDump(cur)
cur.close()
cxn.close()
MySQL-python-1.2.3]# python /u01/app/pythonstudy/dbDump.py
NAME ID
1 Qq
2 Ss
3 Ff