How can I install the MySQLdb module for Python using pip?
-
12If you're looking to use this with Python3 (e.g., with the newest version of Django) you'll need mysqlclient as MysqlDB doesn't support python3. See [This Question](https://stackoverflow.com/questions/14087598/python-3-importerror-no-module-named-configparser) – T.D. Smith Apr 25 '16 at 19:31
-
I'm using mysqlclient with Python 2.7 and MariaDB with no issues so far. – Dave Everitt May 01 '18 at 21:12
21 Answers
It's easy to do, but hard to remember the correct spelling:
pip install mysqlclient
If you need 1.2.x versions (legacy Python only), use pip install MySQL-python
Note: Some dependencies might have to be in place when running the above command. Some hints on how to install these on various platforms:
Ubuntu 14, Ubuntu 16, Debian 8.6 (jessie)
sudo apt-get install python-pip python-dev libmysqlclient-dev
Fedora 24:
sudo dnf install python python-devel mysql-devel redhat-rpm-config gcc
Mac OS
brew install mysql-connector-c
if that fails, try
brew install mysql
-
27You don't have to remember the whole name, you can use `pip search mysql` and find the one you need in the output. – WKPlus Sep 16 '14 at 10:14
-
12@WKPlus: right... but I did `pip search mysqldb` and didn't find it. Thus, I +1 this answer. – Dan H Dec 02 '14 at 19:59
-
7@WKPlus actually it's pretty much a real hassle to find out the right choice after a pip search of mysql – kommradHomer May 30 '15 at 07:31
-
13
-
On Ubuntu or any debian distribution, you could simply sudo apt-get install python-mysqldb . I tried using pip but it failed on my end. – David Okwii Aug 19 '15 at 09:07
-
3@DavidOkwii When you're not inside a virtualenv, sudo is required. That might have been your problem. – Marian Aug 19 '15 at 09:10
-
I wish I could pin this answer to the front page of my SO home. Can that be a feature? I come to this answer at least once a week... – Nostalg.io Oct 07 '15 at 23:30
-
The intuitive package name would be: `pip install mysqldb`. That doesn't work. – tread Jan 05 '16 at 18:11
-
1Actually, under Mac OS, pip install fails with mysql-connector-c installed. However, it works with "brew install mysql" instead. – osxdirk May 31 '17 at 09:08
-
There is a fix for the current problem with Mac OS, brew and mysql-connector-c [here](https://github.com/PyMySQL/mysqlclient-python/issues/169#issuecomment-299778504) – Marian Jul 20 '17 at 21:04
-
1This update of my mysql_config-file worked! https://stackoverflow.com/questions/43740481/python-setup-py-egg-info-mysqlclient – d_- May 17 '18 at 17:14
-
On ubuntu, `build-essential` package is also a required dependency – Prince Odame Sep 08 '19 at 07:14
-
Faced a lot of issues while installing the package for Python 3.6. Got a "Microsoft Visual C++ 14.0 or greater is required" where I have Visual C++ 2015-2022 installed. Copied MySQldb folder from site-package from another system where the MYSQLDB is installed perfectly for python 3.6 and pasting in my current system – Abhishek Roy Jan 03 '22 at 08:46
Starting from a fresh Ubuntu 14.04.2 system, these two commands were needed:
apt-get install python-dev libmysqlclient-dev
pip install MySQL-python
Just doing the "pip install" by itself did not work.
From http://codeinthehole.com/writing/how-to-set-up-mysql-for-python-on-ubuntu/

- 3,521
- 3
- 25
- 32
-
2this one worked, for some reason simple pip install doesn't work clean on ubuntu – Dap May 18 '15 at 19:07
-
In ubuntu 14.04, will get a error "raise EnvironmentError("%s not found" % (mysql_config.path,)", and the first cmd fixed the problem, thanks , save me some time – zhihong Jun 18 '15 at 10:06
-
To fix `error: command 'x86_64-linux-gnu-gcc' failed with exit status 1` use `sudo apt-get install build-essential` – FelikZ Jul 20 '15 at 14:42
-
I wonder why this is needed. One thing occurs to me. I tested this by installing only and not building. Did you build any of the things you installed rather than just install them? – Ray Kiddy Jul 21 '15 at 18:26
-
-
+1 This should be the answer for Ubuntu systems, it worked for me on Ubuntu 18.04.4. Solving my "ImportError: No module named MySQLdb" & ```Could not find a version that satisfies the requirement MySQLdb (from versions: ) No matching distribution found for MySQLdb``` error msgs – Roly Poly Mar 06 '20 at 23:10
First
pip install pymysql
Then put the code below into __init__.py
(projectname/__init__.py
)
import pymysql
pymysql.install_as_MySQLdb()
My environment is (python3.5, django1.10) and this solution works for me!
Hope this helps!!
-
3Woow, I have waisted over 5hrs trying to fix this without success. This is the only solution that worked on Ubuntu 16.04/python 3.5/Django 1.11. Thanks for sharing this. – Paullo Nov 16 '17 at 17:57
-
this not work for me, My environment is python 3.7 and django 2.2. Please help me out. – Rajen K Bhagat Apr 19 '19 at 18:40
-
This should be up there. I've been browsing for a solution for hours. I have a virtualenv with python 2.7 running django 1.6. I was not able to install MySQL-python nor mysqlclient. No matter whatever I tried I was getting a "fatal error: mysql.h". This one did the trick. – rolandsaven Nov 02 '19 at 18:04
I had problems installing the 64-bit version of MySQLdb on Windows via Pip (problem compiling sources) [32bit version installed ok]. Managed to install the compiled MySQLdb from the .whl file available from http://www.lfd.uci.edu/~gohlke/pythonlibs/
Alternative link: https://archive.linux.duke.edu/pypi/simple/mysqlclient/
The .whl file can then be installed via pip as document in https://pip.pypa.io/en/latest/user_guide/#installing-from-wheels
For example if you save in C:/
the you can install via
pip install c:/MySQL_python-1.2.5-cp27-none-win_amd64.whl
Follow-up: if you have a 64bit version of Python installed, then you want to install the 64-bit AMD version of MySQLdb from the link above [i.e. even if you have a Intel processor]. If you instead try and install the 32-bit version, I think you get the unsupported wheel error in comments below.
-
-
3I needed MySQL client. `pip install mysqlclient-1.3.7-cp27-none-win_amd64.whl` working great. – arsenik Jul 15 '16 at 13:14
-
2Went to [http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python](http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python) to download `whl` file (I dl'd 64 bit to match my 64 bit Python install). Saved file into `c:/` directory and ran `pip install c:/MySQL_python-1.2.5-cp27-none-win_amd64.whl` It installed in seconds and was good to go. Too easy! – curtisp Oct 11 '16 at 18:43
-
-
the link to www.lfd.uci.edu is dead.. this appears to be a good alternative... https://archive.linux.duke.edu/pypi/simple/mysqlclient/ – panofish May 31 '23 at 14:37
well this worked for me:
pip install mysqlclient
this is for python 3.x

- 1,840
- 1
- 21
- 26
-
3This is the first solution that worked for me for python 3.5 / django 1.11 – Braden Holt Oct 13 '17 at 21:07
-
1You could run into trouble with a mysql_config file missing. See https://stackoverflow.com/questions/7475223/mysql-config-not-found-when-installing-mysqldb-python-interface – zwep Dec 28 '18 at 12:53
Go to pycharm then go to default setting --> pip (double click) -- pymsqldb..-- > install --after installing use in a program like this
import pymysql as MySQLdb
# Open database connection
db = MySQLdb.connect("localhost","root","root","test" )
# prepare a cursor object using cursor() method
cursor = db.cursor()
# execute SQL query using execute() method.
cursor.execute("show tables")
# Fetch a single row using fetchone() method.
data = cursor.fetchall()
print (data)
# disconnect from server
db.close()

- 2,062
- 2
- 24
- 36

- 95
- 1
- 2
-
So for me MySQLdb was not working because virtual environment was referring to incorrect library. Using `import pymysql as MySQLdb` solved that. – darth vader Jan 27 '21 at 02:46
Many of the given answers here are quite confusing so I will try to put it simply. It helped me to install this
pip install pymysql
and then use the following command in the python file
import pymysql as MySQLdb
This way you can use MySQLdb without any problems.

- 958
- 11
- 16
-
1your answer does not make any sense since those are two diffrent models – urek mazino Sep 23 '22 at 22:20
I tried all the option but was not able to get it working on Redhat platform. I did the following to make it work:-
yum install MySQL-python -y
Once the package was installed was able to import module as follows in the interpreter:-
>>> import MySQLdb
>>>

- 2,875
- 1
- 13
- 21
-
The question was specificly mentioning `pip`. What distribution are you using? On Fedora 24, this works to install the module via pip: `sudo dnf install python python-devel mysql-devel redhat-rpm-config gcc`, `sudo pip install MySQL-python` – Marian Sep 26 '16 at 13:23
-
I understand the question is via pip but I personally wasn't able to solve it by using pip. Hence i just shared my workaround which fixed the issue for me. I am using centos 6.8. pip list does not show anything related to mysql and when i do "pip install MySQL-python" it threw an error. Now interestingly I figured out that once we install through yum i:e "yum install MySQL-python" I am able to import MySQLdb. Also once done with yum installation I was able to install via pip without any errors. – Ashwin Sep 26 '16 at 13:35
If you are unable to install mysqlclient you can also install pymysql:
pip install pymysql
This works same as MySqldb. After that use pymysql all over instead of MySQLdb

- 1,282
- 4
- 21
- 33

- 73
- 1
- 7
If you are use Raspberry Pi [Raspbian OS]
There are need to be install pip command at first
apt-get install python-pip
So that just install Sequently
apt-get install python-dev libmysqlclient-dev
apt-get install python-pip
pip install MySQL-python

- 1,089
- 1
- 14
- 34

- 53
- 1
- 9
I had the same problem too.Follow these steps if you are on Windows. Go to: 1.My Computer 2.System Properties 3.Advance System Settings 4. Under the "Advanced" tab click the button that says "Environment Variables" 5. Then under System Variables you have to add / change the following variables: PYTHONPATH and Path. Here is a paste of what my variables look like: python path:
C:\Python27;C:\Python27\Lib\site-packages;C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk;C:\Python27\Scripts
path:
C:\Program Files\MySQL\MySQL Utilities 1.3.5\;C:\Python27;C:\Python27\Lib\site-packages;C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk;C:\Python27\Scripts
See this link for reference
If you have Windows installed on your system then type the following command on cmd :
pip install mysql-connector
if the above command does not work try using:
pip install mysql-connector-python
Now,if the above commands do not get the work done, try using:
pip install mysql-connector-python-rf
That's it you are good to go now.

- 41
- 5
The above answer is great, but there may be some problems when we using pip to install MySQL-python in Windows
for example,It needs some files that are associated with Visual Stdio .One solution is installing VS2008 or 2010……Obviously,it cost too much.
Another way is the answer of @bob90937 . I am here to do something to add.
with http://www.lfd.uci.edu/~gohlke/pythonlibs, u can download many Windows binaries of many scientific open-source extension packages for the official CPython distribution of the Python programming language.
Back to topic,we can choose the MySQL-python(py2) or Mysqlclient(py3) and use pip install to install. it gives us Great convenience!

- 81
- 1
- 4
If pip3 isn't working, you can try:
sudo apt install python3-mysqldb

- 8,268
- 8
- 50
- 67

- 11
- 2
-
Not very good answer. You assume Python version is 3 and OS is Debian/Ubuntu, but you don't know that. Please wait until the author adds more details. – phd Jul 12 '17 at 16:30
pip install mysql-connector-python
as noted in the documentation:
https://dev.mysql.com/doc/connector-python/en/connector-python-installation-binary.html

- 1,214
- 1
- 14
- 16
on RHEL 7:
sudo yum install yum-utils mariadb-devel python-pip python-devel gcc
sudo /bin/pip2 install MySQL-python

- 13,930
- 7
- 70
- 73
actually, follow @Nick T's answer doesn't work for me, i try apt-get install python-mysqldb
work for me
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
>>>

- 11
- 2
You can follow the readme of mysqlclient.
For example on MAC OS: as per the readme for mysqlclient,
you will need to install pkg-config
and (mysql
or mysql-client
):
# Assume you are activating Python 3 venv $ brew install mysql pkg-config $ pip install mysqlclient
If you don't want to install MySQL server, you can use mysql-client instead:
# Assume you are activating Python 3 venv $ brew install mysql-client pkg-config $ export PKG_CONFIG_PATH="/opt/homebrew/opt/mysql-client/lib/pkgconfig" $ pip install mysqlclient

- 25
- 7
My environment are:
- Windows 10 Pro,
- Python 3.7 (python-3.7.1-amd64.exe),
- MySQL 8.0 (mysql-installer-web-community-8.0.13.0.msi)
pip install mysqlclient-1.3.13-cp37-cp37m-win_amd64.whl
works for me.
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()

- 109
- 3