在Python连接和使用mysql时,需要调用MySQLdb 模块,是用于Python链接Mysql数据库的接口,它实现了 Python 数据库 API 规范 V2.0,基于 MySQL C API 上建立的。

 MySQLdb模块在不同的平台下安装方式不一样,这里主要介绍Linux下的安装方式,https://pypi.python.org/pypi/MySQL-python  可以在这里选择合适的版本,解压安装。

安装步骤:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
1、选择合适的版本以及检查相关路径
这里选择版本为MySQL-python-1.2.5.zip,下面是下载路径与过程。
 wget https://pypi.python.org/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip
--2016-10-08 21:34:56--  https://pypi.python.org/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip
Resolving pypi.python.org... 151.101.76.223, 2a04:4e42:12::223
Connecting to pypi.python.org|151.101.76.223|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 108935 (106K) [application/octet-stream]
Saving to: “MySQL-python-1.2.5.zip”
 
100%[======================================================>] 108,935     --.-K/s   in 0.1s    
 
2016-10-08 21:34:57 (1.00 MB/s) - “MySQL-python-1.2.5.zip” saved [108935/108935]
 
[root@localhost python]# ls 
mysql.py  MySQL-python-1.2.5.zip
解压:
[root@localhost python]# unzip  MySQL-python-1.2.5.zip 
Archive:  MySQL-python-1.2.5.zip
  inflating: MySQL-python-1.2.5/GPL-2.0  
  inflating: MySQL-python-1.2.5/HISTORY  
  inflating: MySQL-python-1.2.5/INSTALL  
  inflating: MySQL-python-1.2.5/MANIFEST.in  
  inflating: MySQL-python-1.2.5/metadata.cfg  
  inflating: MySQL-python-1.2.5/PKG-INFO  
  inflating: MySQL-python-1.2.5/pymemcompat.h  
  inflating: MySQL-python-1.2.5/README.md  
  inflating: MySQL-python-1.2.5/setup.cfg  
  inflating: MySQL-python-1.2.5/setup.py  
  inflating: MySQL-python-1.2.5/setup_common.py  
  inflating: MySQL-python-1.2.5/
切换到解压目录安装:
使用命令 python  setup.py  build && python  setup.py install
   
  安装报错:
   
  [root@localhost MySQL-python-1.2.5]# python  setup.py  build
Traceback (most recent call last):
  File "setup.py", line 7, in <module>
    import setuptools
ImportError: No module named setuptools
提示缺少setuptools 可以在 
  
  
 安装命令和上面一样,等最后执行完毕没有报错,安装成功,我们继续安装上面的。
 python  setup.py   build
running build
running build_py
creating build
creating build/lib
copying pkg_resources.py -> build/lib
copying easy_install.py -> build/lib
copying site.py -> build/lib
creating build/lib/setuptools
copying setuptools/package_index.py -> build/lib/setuptools
copying setuptools/depends.py -> build/lib/setuptools
copying setuptools/extension.py -> build/lib/setuptools
copying setuptools/__init__.py -> build/lib/set
。。。。。。
 
Installed /usr/local/python2.7.11/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
Processing dependencies for setuptools==0.6c11
Finished processing dependencies for setuptools==0.6c11
[root@localhost setuptools-0.6c11]# cd  ..
 
继续安装MySQL-python 出现error: command 'gcc' failed with exit status 1可以用以下命令解决
 
yum install gcc python-devel mysql-devel libxml2 libxml2-dev libxslt* zlib gcc openssl

安装完成后的检验:

在shell窗口调用Python命令,然后执行,不报错即可

import MySQLdb