如何使用pip安装Python的MySQLdb模块?


当前回答

实际上,遵循@Nick T的答案对我不起作用,我尝试apt-get install python-mysqldb对我有效

root@2fb0da64a933:/home/test_scrapy# apt-get install python-mysqldb
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libmariadbclient18 mysql-common
Suggested packages:
  default-mysql-server | virtual-mysql-server python-egenix-mxdatetime python-mysqldb-dbg
The following NEW packages will be installed:
  libmariadbclient18 mysql-common python-mysqldb
0 upgraded, 3 newly installed, 0 to remove and 29 not upgraded.
Need to get 843 kB of archives.
After this operation, 4611 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://deb.debian.org/debian stretch/main amd64 mysql-common all 5.8+1.0.2 [5608 B]
Get:2 http://deb.debian.org/debian stretch/main amd64 libmariadbclient18 amd64 10.1.38-0+deb9u1 [785 kB]
Get:3 http://deb.debian.org/debian stretch/main amd64 python-mysqldb amd64 1.3.7-1.1 [52.1 kB]                    
Fetched 843 kB in 23s (35.8 kB/s)                                                                                 
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package mysql-common.
(Reading database ... 13223 files and directories currently installed.)
Preparing to unpack .../mysql-common_5.8+1.0.2_all.deb ...
Unpacking mysql-common (5.8+1.0.2) ...
Selecting previously unselected package libmariadbclient18:amd64.
Preparing to unpack .../libmariadbclient18_10.1.38-0+deb9u1_amd64.deb ...
Unpacking libmariadbclient18:amd64 (10.1.38-0+deb9u1) ...
Selecting previously unselected package python-mysqldb.
Preparing to unpack .../python-mysqldb_1.3.7-1.1_amd64.deb ...
Unpacking python-mysqldb (1.3.7-1.1) ...
Setting up mysql-common (5.8+1.0.2) ...
update-alternatives: using /etc/mysql/my.cnf.fallback to provide /etc/mysql/my.cnf (my.cnf) in auto mode
Setting up libmariadbclient18:amd64 (10.1.38-0+deb9u1) ...
Processing triggers for libc-bin (2.24-11+deb9u3) ...
Setting up python-mysqldb (1.3.7-1.1) ...
root@2fb0da64a933:/home/test_scrapy# python 
Python 2.7.13 (default, Nov 24 2017, 17:33:09) 
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>> 

其他回答

对于Python3,我需要这样做:

python3 -m pip install MySQL

我在通过Pip(问题编译源)在Windows上安装64位版本的MySQLdb时遇到了问题[32位版本安装ok]。设法从http://www.lfd.uci.edu/~gohlke/pythonlibs/上的.whl文件安装编译后的MySQLdb

然后,.whl文件可以通过pip作为https://pip.pypa.io/en/latest/user_guide/#installing-from-wheels中的文档安装

例如,如果你保存在C:/ the,你可以安装通过

pip install c:/MySQL_python-1.2.5-cp27-none-win_amd64.whl

后续:如果你安装了64位版本的Python,那么你想从上面的链接安装64位AMD版本的MySQLdb[即,即使你有英特尔处理器]。如果你尝试安装32位版本,我认为你会在下面的评论中得到不支持的轮子错误。

我的环境是:

Windows 10 Pro, Python 3.7 (Python -3.7.1-amd64.exe) MySQL 8.0 (MySQL -install -web-community-8.0.13.0 msi)

PIP安装mysqlclient-1.3.13-cp37-cp37m-win_amd64.whl

对我有用。

import MySQLdb, sys


# --------------------------------------------------
# Connect to MySQL
# --------------------------------------------------
try:
    db = MySQLdb.connect(host="localhost", user="user", passwd="pass", db="database", charset='cp1251')
except MySQLdb.Error as e:
    print ("Error %d: %s" % (e.args[0], e.args[1]))
    sys.exit()

# Creating cursor 
cursor = db.cursor()

这招对我很管用:

pip install mysqlclient

这是针对python 3.x的

在RHEL 7:

Sudo yum安装yum-utils mariadb-devel python-pip python-devel GCC

sudo /bin/pip2 install MySQL-python