【python】Linux服务器下安装python 的 MySQLdb 以及常见报错解决

Linux服务器 下安装python 之MySQLdb


一般将涉及到数据库mysql操作的python项目跑到服务器端,首次会提示


mysqlLdb的安装


譬如
[root@iZ94s4ycnl7Z spider]# python spider.py 
Traceback (most recent call last):
  File "spider.py", line 3, in 
    import MySQLdb as mdb
ImportError: No module named MySQLdb
[root@iZ94s4ycnl7Z spider]# python --version
Python 2.7.5

如何安装呢?

首先安装  (CentOS下)mysql-devel    (Ubuntu下不叫mysql-devel,而是叫libmysqld-dev sudo apt-get install libmysqld-dev) 
yum -y install mysql-devel
接着 python-devel
 
   
yum install python-devel

wget http://downloads.sourceforge.net/project/mysql-python/mysql-python-test/1.2.4b4/MySQL-python-1.2.4b4.tar.gz
tar zxvf  MySQL-python-1.2.4b4.tar.gz
cd MySQL-python-1.2.4b4
python setup.py build
python setup.py install

 
   

常见的报错(一)


[root@iZ94s4ycnl7Z spider]# yum -y install mysql-dev
Loaded plugins: langpacks
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
base                                                             | 3.6 kB  00:00:00     
epel                                                             | 4.3 kB  00:00:00     
extras                                                           | 3.4 kB  00:00:00     
hhvm                                                             | 2.9 kB  00:00:00     
updates                                                          | 3.4 kB  00:00:00     
(1/6): hhvm/x86_64/primary_db                                    | 5.9 kB  00:00:00     
(2/6): epel/x86_64/group_gz                                      | 170 kB  00:00:00     
(3/6): extras/7/x86_64/primary_db                                | 166 kB  00:00:00     
(4/6): epel/x86_64/updateinfo                                    | 671 kB  00:00:00     
(5/6): epel/x86_64/primary_db                                    | 4.3 MB  00:00:04     
(6/6): updates/7/x86_64/primary_db                               | 9.1 MB  00:00:09     
No package mysql-dev available.
Error: Nothing to do

或者

[root@iZ94s4ycnl7Z MySQL-python-1.2.4b4]# yum -y install mysql-dev
Loaded plugins: langpacks
No package mysql-dev available.
Error: Nothing to do

这种情况的原因很简单
 
   

centos安装 python-dev包提示No package python-dev available:

出现此问题的原因是python-dev的包在centos的yum中不叫python-dev,而是python-devel.

所以使用下面的命令即可安装python-dev:


常见报错(二)


[root@iZ94s4ycnl7Z mysqlpython]# cd MySQL-python-1.2.4b4
[root@iZ94s4ycnl7Z MySQL-python-1.2.4b4]# python setup.py build
Downloading http://pypi.python.org/packages/source/d/distribute/distribute-0.6.28.tar.gz
Extracting in /tmp/tmpezR63R
Now working in /tmp/tmpezR63R/distribute-0.6.28
Building a Distribute egg in /usr/local/mysqlpython/MySQL-python-1.2.4b4
/usr/local/mysqlpython/MySQL-python-1.2.4b4/distribute-0.6.28-py2.7.egg
running build
running build_py
creating build
creating build/lib.linux-x86_64-2.7
copying _mysql_exceptions.py -> build/lib.linux-x86_64-2.7
creating build/lib.linux-x86_64-2.7/MySQLdb
copying MySQLdb/__init__.py -> build/lib.linux-x86_64-2.7/MySQLdb
copying MySQLdb/converters.py -> build/lib.linux-x86_64-2.7/MySQLdb
copying MySQLdb/connections.py -> build/lib.linux-x86_64-2.7/MySQLdb
copying MySQLdb/cursors.py -> build/lib.linux-x86_64-2.7/MySQLdb
copying MySQLdb/release.py -> build/lib.linux-x86_64-2.7/MySQLdb
copying MySQLdb/times.py -> build/lib.linux-x86_64-2.7/MySQLdb
creating build/lib.linux-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/CR.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/ER.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/REFRESH.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
running build_ext
building '_mysql' extension
creating build/temp.linux-x86_64-2.7
gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Dversion_info=(1,2,4,'beta',4) -D__version__=1.2.4b4 -I/usr/local/mysql/include -I/usr/include/python2.7 -c _mysql.c -o build/temp.linux-x86_64-2.7/_mysql.o -fPIC -g -static-libgcc -fno-omit-frame-pointer -fno-strict-aliasing -DMY_PTHREAD_FASTMUTEX=1
_mysql.c:29:20: fatal error: Python.h: No such file or directory
 #include "Python.h"
                    ^
compilation terminated.
error: command 'gcc' failed with exit status 1

这种情况的原因为没有安装 python-devel(CentOS)  其他环境叫做 python-dev

参考博文
    http://www.cnblogs.com/jackluo/p/3559978.html

你可能感兴趣的:(python,linux)