Installing mysql-python on Centos
I'm trying to get the MySQL-python lib installed on centos 5.5. I ran
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?
Step 1 - Install package
# 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
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
I have Python 2.7.5, MySQL 5.6 and CentOS 7.1.1503.
For me it worked with the following command:
# pip install mysql-python
Note pre-requisites here:
Install 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:
# yum install python-devel
Install MySQL devel packages:
# yum install mysql-devel
For centos7 I required:
sudo yum install mysql-devel gcc python-pip python-devel
sudo pip install mysql-python
So, gcc
and mysql-devel
(rather than mysql
) were important
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.
Your choices are:
- Install the RPM version of MySQL.
- Compile MySQLDB to your version of MySQL.
mysql-python
NOT support Python3, you may need:
sudo pip3 install mysqlclient
Also, check this post for more alternatives.
精彩评论