在 Centos 上安装 mysql-python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4380931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:30:30  来源:igfitidea点击:

Installing mysql-python on Centos

pythonmysql

提问by fred basset

I'm trying to get the MySQL-python lib installed on centos 5.5. I ran

我正在尝试在 centos 5.5 上安装 MySQL-python 库。我跑了

sudo yum install MySQL-python

but then when I tried:

但是当我尝试时:

import MySQLdb

I get this error:

我收到此错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "MySQLdb/__init__.py", line 22, in ?
    raise ImportError("this is MySQLdb version %s, but _mysql is version %r" %
ImportError: this is MySQLdb version (1, 2, 3, 'final', 0), \ # added linebreak
but _mysql is version (1, 2, 1, 'final', 1)

any clues on how to fix this?

关于如何解决这个问题的任何线索?

采纳答案by mluebke

You probably did not install MySQL via yum? The version of MySQLDB in the repository is tied to the version of MySQL in the repository. The versions need to match.

您可能没有通过 yum 安装 MySQL?存储库中 MySQLDB 的版本与存储库中的 MySQL 版本相关联。版本需要匹配。

Your choices are:

您的选择是:

  1. Install the RPM version of MySQL.
  2. Compile MySQLDB to your version of MySQL.
  1. 安装 MySQL 的 RPM 版本。
  2. 将 MySQLDB 编译为您的 MySQL 版本。

回答by mluebke

Step 1- Install package

第 1 步- 安装软件包

# yum install MySQL-python
Loaded plugins: auto-update-debuginfo, langpacks, presto, refresh-packagekit
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package MySQL-python.i686 0:1.2.3-3.fc15 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package              Arch         Version                 Repository      Size
================================================================================
Installing:
 MySQL-python         i686         1.2.3-3.fc15            fedora          78 k

Transaction Summary
================================================================================
Install       1 Package(s)

Total download size: 78 k
Installed size: 220 k
Is this ok [y/N]: y
Downloading Packages:
Setting up and reading Presto delta metadata
Processing delta metadata
Package(s) data still to download: 78 k
MySQL-python-1.2.3-3.fc15.i686.rpm                       |  78 kB     00:00     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : MySQL-python-1.2.3-3.fc15.i686                               1/1 

Installed:
  MySQL-python.i686 0:1.2.3-3.fc15                                              

Complete!

Step 2- Test working

第 2 步- 测试工作

import MySQLdb
db = MySQLdb.connect("localhost","myusername","mypassword","mydb" )
cursor = db.cursor()
cursor.execute("SELECT VERSION()")
data = cursor.fetchone()    
print "Database version : %s " % data    
db.close()

Ouput:

输出:

Database version : 5.5.20 

回答by Krunal Jain

I have Python 2.7.5, MySQL 5.6 and CentOS 7.1.1503.

我有Python 2.7.5、MySQL 5.6 和 CentOS 7.1.1503

For me it worked with the following command:

对我来说,它使用以下命令:

# pip install mysql-python

Note pre-requisites here:

请注意这里的先决条件:

Install Python pip:

安装 Python pip:

# rpm -iUvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm

# yum -y update
Reboot the machine (if kernel is also updated)

# yum -y install python-pip

Install Python devel packages:

安装 Python 开发包:

# yum install python-devel

Install MySQL devel packages:

安装 MySQL 开发包:

# yum install mysql-devel

回答by wired00

For centos7 I required: sudo yum install mysql-devel gcc python-pip python-devel sudo pip install mysql-python

对于centos7,我要求: sudo yum install mysql-devel gcc python-pip python-devel sudo pip install mysql-python

So, gccand mysql-devel(rather than mysql) were important

所以,gccmysql-devel(而不是mysql)很重要

回答by xtluo

mysql-pythonNOTsupport Python3, you may need:

mysql-python支持Python3,您可能需要:

sudo pip3 install mysqlclient

Also, check this postfor more alternatives.

另外,请查看此帖子以获取更多替代方案。