Red Hat - python 2.7中的mysqldb模块

时间:2018-04-20 15:23:58

标签: python redhat mysql-python

我正在尝试使用以下命令在python 2.7中安装模块mysqldb:

pip install mysql-python

错误:

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,5,'final',1) -D__version__=1.2.5 -I/usr/include/mysql -I/usr/include/python2.7 -c _mysql.c -o build/temp.linux-x86_64-2.7/_mysql.o -m64
_mysql.c:44:23: fatal error: my_config.h: No such file or directory
 #include "my_config.h"
                       ^
compilation terminated.
error: command 'gcc' failed with exit status 1

我正在使用Linux RedHat,操作系统版本:

Linux 3.10.0-229.el7.x86_64 #1 SMP Thu Jan 29 18:37:38 EST 2015 x86_64 x86_64 x86_64 GNU/Linux

我安装了以下软件包:

$ yum install gcc
$ yum install python-devel
$ yum install mysql-devel

我错了什么?

提前致谢

1 个答案:

答案 0 :(得分:0)

鉴于您在RedHat,您可以使用以下命令安装此模块:

yum install MySQL-python

刚在docker实例上测试过:

[root@c72015444834 /]# yum install MySQL-python
Loaded plugins: fastestmirror
base                                                                                                                         | 3.6 kB  00:00:00
extras                                                                                                                       | 3.4 kB  00:00:00
updates                                                                                                                      | 3.4 kB  00:00:00
(1/4): base/7/x86_64/group_gz                                                                                                | 156 kB  00:00:02
(2/4): extras/7/x86_64/primary_db                                                                                            | 185 kB  00:00:15
(3/4): base/7/x86_64/primary_db                                                                                              | 5.7 MB  00:00:22
(4/4): updates/7/x86_64/primary_db                                                                                           | 6.9 MB  00:00:23
Determining fastest mirrors
 * base: mirror.tedra.es
 * extras: mirror.tedra.es
 * updates: mirror.tedra.es
Resolving Dependencies
--> Running transaction check
---> Package MySQL-python.x86_64 0:1.2.5-1.el7 will be installed
--> Processing Dependency: libmysqlclient.so.18(libmysqlclient_18)(64bit) for package: MySQL-python-1.2.5-1.el7.x86_64
--> Processing Dependency: libmysqlclient.so.18()(64bit) for package: MySQL-python-1.2.5-1.el7.x86_64
--> Running transaction check
---> Package mariadb-libs.x86_64 1:5.5.56-2.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================================================================================================
 Package                               Arch                            Version                                  Repository                     Size
====================================================================================================================================================
Installing:
 MySQL-python                          x86_64                          1.2.5-1.el7                              base                           90 k
Installing for dependencies:
 mariadb-libs                          x86_64                          1:5.5.56-2.el7                           base                          757 k

Transaction Summary
====================================================================================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 847 k
Installed size: 4.7 M
Is this ok [y/d/N]: y
Downloading packages:
warning: /var/cache/yum/x86_64/7/base/packages/MySQL-python-1.2.5-1.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY--:-- ETA
Public key for MySQL-python-1.2.5-1.el7.x86_64.rpm is not installed
(1/2): MySQL-python-1.2.5-1.el7.x86_64.rpm                                                                                   |  90 kB  00:00:00
(2/2): mariadb-libs-5.5.56-2.el7.x86_64.rpm                                                                                  | 757 kB  00:00:00
----------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                               885 kB/s | 847 kB  00:00:00
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
 Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
 Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 Package    : centos-release-7-1.1503.el7.centos.2.7.x86_64 (@CentOS/$releasever)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 1:mariadb-libs-5.5.56-2.el7.x86_64                                                                                               1/2
  Installing : MySQL-python-1.2.5-1.el7.x86_64                                                                                                  2/2
  Verifying  : 1:mariadb-libs-5.5.56-2.el7.x86_64                                                                                               1/2
  Verifying  : MySQL-python-1.2.5-1.el7.x86_64                                                                                                  2/2

Installed:
  MySQL-python.x86_64 0:1.2.5-1.el7

Dependency Installed:
  mariadb-libs.x86_64 1:5.5.56-2.el7

Complete!

要检查它是否已实际安装:

[root@c72015444834 /]# python
Python 2.7.5 (default, Jun 17 2014, 18:11:42)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb

但是,我的建议是你跳过mysqldb,因为你遇到的问题远比我的经验所希望的要多。 Intead跳转到Connector / Python,这是一个标准驱动程序,可以在没有pip问题的情况下安装,并且具有与mysqldb相同的接口。

安装它:

pip install mysql-connector

用法与mysqldb相同,因此切换应该很简单:

import mysql.connector

db = mysql.connector.connect(host=MYSQL_SERVER, user=MYSQL_USER, password=MYSQL_PASS, db=MYSQL_DB)
cursor = db.cursor()
try:
    strSQL = '''select field1, field2, field3 from yourTable where field4 = %s and field5 = %s'''
    cursor.execute(strSQL, ("asdf", "qwerty"))
    for row in cursor:
        field1, field2, field3 = row
        # do your stuff
finally:
    cursor.close()
    db.close()