安装很简单,步骤如下:
前期:yum -y install python-setuptools,或者自己网上找源码包安装
1. 下载安装包:
#wget https://pypi.python.org/packages/source/M/MySQL-python/MySQL-python-1.2.5.zip --no-check-certificate
#unzip MySQL-python-1.2.5.zip
#cd MySQL-python-1.2.5
#python setup.py build
[root@ MySQL-python-1.2.5]# python setup.py build running build running build_py creating build creating build/lib.linux-x86_64-2.6 copying _mysql_exceptions.py -> build/lib.linux-x86_64-2.6 creating build/lib.linux-x86_64-2.6/MySQLdb copying MySQLdb/__init__.py -> build/lib.linux-x86_64-2.6/MySQLdb copying MySQLdb/converters.py -> build/lib.linux-x86_64-2.6/MySQLdb copying MySQLdb/connections.py -> build/lib.linux-x86_64-2.6/MySQLdb copying MySQLdb/cursors.py -> build/lib.linux-x86_64-2.6/MySQLdb copying MySQLdb/release.py -> build/lib.linux-x86_64-2.6/MySQLdb copying MySQLdb/times.py -> build/lib.linux-x86_64-2.6/MySQLdb creating build/lib.linux-x86_64-2.6/MySQLdb/constants copying MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-2.6/MySQLdb/constants copying MySQLdb/constants/CR.py -> build/lib.linux-x86_64-2.6/MySQLdb/constants copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-2.6/MySQLdb/constants copying MySQLdb/constants/ER.py -> build/lib.linux-x86_64-2.6/MySQLdb/constants copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-2.6/MySQLdb/constants copying MySQLdb/constants/REFRESH.py -> build/lib.linux-x86_64-2.6/MySQLdb/constants copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-2.6/MySQLdb/constants running build_ext building '_mysql' extension creating build/temp.linux-x86_64-2.6 gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 -I/usr/include/mysql -I/usr/include/python2.6 -c _mysql.c -o build/temp.linux-x86_64-2.6/_mysql.o -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fno-strict-aliasing -fwrapv -fPIC -DUNIV_LINUX -DUNIV_LINUX In file included from /usr/include/mysql/my_config.h:14, from _mysql.c:44: /usr/include/mysql/my_config_x86_64.h:1082:1: warning: "HAVE_WCSCOLL" redefined In file included from /usr/include/python2.6/pyconfig.h:6, from /usr/include/python2.6/Python.h:8, from _mysql.c:29: /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/mysql -L/usr/lib64 -lmysqlclient_r -lz -lpthread -lcrypt -lnsl -lm -lpthread -lssl -lcrypto -lpython2.6 -o build/lib.linux-x86_64-2.6/_mysql.so
#python setup.py install
[[email protected]]# python setup.py install running install running bdist_egg running egg_info writing MySQL_python.egg-info/PKG-INFO writing top-level names to MySQL_python.egg-info/top_level.txt writing dependency_links to MySQL_python.egg-info/dependency_links.txt reading manifest file 'MySQL_python.egg-info/SOURCES.txt' reading manifest template 'MANIFEST.in' writing manifest file 'MySQL_python.egg-info/SOURCES.txt' installing library code to build/bdist.linux-x86_64/egg running install_lib running build_py copying MySQLdb/release.py -> build/lib.linux-x86_64-2.6/MySQLdb running build_ext creating build/bdist.linux-x86_64 creating build/bdist.linux-x86_64/egg creating build/bdist.linux-x86_64/egg/MySQLdb copying build/lib.linux-x86_64-2.6/MySQLdb/times.py -> build/bdist.linux-x86_64/egg/MySQLdb creating build/bdist.linux-x86_64/egg/MySQLdb/constants copying build/lib.linux-x86_64-2.6/MySQLdb/constants/__init__.py -> build/bdist.linux-x86_64/egg/MySQLdb/constants copying build/lib.linux-x86_64-2.6/MySQLdb/constants/REFRESH.py -> build/bdist.linux-x86_64/egg/MySQLdb/constants copying build/lib.linux-x86_64-2.6/MySQLdb/constants/CR.py -> build/bdist.linux-x86_64/egg/MySQLdb/constants copying build/lib.linux-x86_64-2.6/MySQLdb/constants/CLIENT.py -> build/bdist.linux-x86_64/egg/MySQLdb/constants copying build/lib.linux-x86_64-2.6/MySQLdb/constants/FLAG.py -> build/bdist.linux-x86_64/egg/MySQLdb/constants copying build/lib.linux-x86_64-2.6/MySQLdb/constants/ER.py -> build/bdist.linux-x86_64/egg/MySQLdb/constants copying build/lib.linux-x86_64-2.6/MySQLdb/constants/FIELD_TYPE.py -> build/bdist.linux-x86_64/egg/MySQLdb/constants copying build/lib.linux-x86_64-2.6/MySQLdb/cursors.py -> build/bdist.linux-x86_64/egg/MySQLdb copying build/lib.linux-x86_64-2.6/MySQLdb/converters.py -> build/bdist.linux-x86_64/egg/MySQLdb copying build/lib.linux-x86_64-2.6/MySQLdb/__init__.py -> build/bdist.linux-x86_64/egg/MySQLdb copying build/lib.linux-x86_64-2.6/MySQLdb/connections.py -> build/bdist.linux-x86_64/egg/MySQLdb copying build/lib.linux-x86_64-2.6/MySQLdb/release.py -> build/bdist.linux-x86_64/egg/MySQLdb copying build/lib.linux-x86_64-2.6/_mysql.so -> build/bdist.linux-x86_64/egg copying build/lib.linux-x86_64-2.6/_mysql_exceptions.py -> build/bdist.linux-x86_64/egg byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/times.py to times.pyc byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/__init__.py to __init__.pyc byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/REFRESH.py to REFRESH.pyc byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/CR.py to CR.pyc byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/CLIENT.py to CLIENT.pyc byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/FLAG.py to FLAG.pyc byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/ER.py to ER.pyc byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/constants/FIELD_TYPE.py to FIELD_TYPE.pyc byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/cursors.py to cursors.pyc byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/converters.py to converters.pyc byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/__init__.py to __init__.pyc byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/connections.py to connections.pyc byte-compiling build/bdist.linux-x86_64/egg/MySQLdb/release.py to release.pyc byte-compiling build/bdist.linux-x86_64/egg/_mysql_exceptions.py to _mysql_exceptions.pyc creating stub loader for _mysql.so byte-compiling build/bdist.linux-x86_64/egg/_mysql.py to _mysql.pyc creating build/bdist.linux-x86_64/egg/EGG-INFO copying MySQL_python.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO copying MySQL_python.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO copying MySQL_python.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO copying MySQL_python.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO writing build/bdist.linux-x86_64/egg/EGG-INFO/native_libs.txt zip_safe flag not set; analyzing archive contents... creating dist creating 'dist/MySQL_python-1.2.5-py2.6-linux-x86_64.egg' and adding 'build/bdist.linux-x86_64/egg' to it removing 'build/bdist.linux-x86_64/egg' (and everything under it) Processing MySQL_python-1.2.5-py2.6-linux-x86_64.egg creating /usr/lib64/python2.6/site-packages/MySQL_python-1.2.5-py2.6-linux-x86_64.egg Extracting MySQL_python-1.2.5-py2.6-linux-x86_64.egg to /usr/lib64/python2.6/site-packages Adding MySQL-python 1.2.5 to easy-install.pth file Installed /usr/lib64/python2.6/site-packages/MySQL_python-1.2.5-py2.6-linux-x86_64.egg Processing dependencies for MySQL-python==1.2.5 Finished processing dependencies for MySQL-python==1.2.5
测试:
[root@ MySQL-python-1.2.5]# python Python 2.6.6 (r266:84292, Jan 22 2014, 09:42:36) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import MySQLdb >>> quit() 未成功安装测试时会提示: >>> import mysqldb Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named mysqldb
安装时遇到的错误:
若在执行python setup.py build时报错报EnvironmentError: mysql_config not found,需要进行如下操作:
1. 使用find / -name mysql_config查找mysql_config的位置,如/usr/bin/mysql/mysql_config
2. 修改MySQL-python-1.2.5 /site.cfg文件,去掉mysql_config=XXX注释,并改成mysql_config=/usr/bin/mysql/mysql_config
3. 如果没有mysql_config命令,则需要安装如下包:
yum -y install mysql-server mysql-devel mysql-shared