How to download and install MySQLdb module for Python on Linux?

Please use md5 checksum to make sure the downloaded files are not corrupt. Also, $ before commands mean you can run the command as ordinary user and # means you have to run the command as root (or use $ sudo <command>).

$ wget http://downloads.sourceforge.net/project/mysql-python/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz
$ tar xzf MySQL-python-1.2.3.tar.gz
$ cd MySQL-python-1.2.3
# /opt/python/bin/python setup.py install

That should install MySQLdb module, but in case you get an error complaining about missing mysql_config, do the following inside MySQLdb unzipped folder (use your favorite text editor instead of nano, if you will):

$ nano site.cfg

and find the line that starts with something like the following:

# mysql_config = ...

You have to uncomment it and replace the path with path of mysql_config in your local machine. On my computer, I did the following:

mysql_config = /opt/mysql/bin/mysql_config

Ctrl-O to save and Ctrl-X to exit out of nano and run the following on command line:

# /opt/python-2.7/bin/python setup.py install

That should install the MySQLdb module for your python installation. To verify:

$ ls /opt/python/lib/python2.7/site-packages/ -la | grep MySQL

The output looked like below on my machine:

-rw-r--r--  1 root root 106932 Nov  4 11:49 MySQL_python-1.2.3-py2.7-linux-x86_64.egg

Did this tutorial help a little? How about buy me a cup of coffee?

Buy me a coffee at ko-fi.com

Please feel free to use the comments form below if you have any questions or need more explanation on anything. I do not guarantee a response.

IMPORTANT: You must thoroughy test any instructions on a production-like test environment first before trying anything on production systems. And, make sure it is tested for security, privacy, and safety. See our terms here.

Comments (write a comment):

Thanks for the help. I confirmed this on Fedora 17, though I needed to install python-devel and mysql-devel before it would work. Also, if you have a 64-bit machine, the mysql package might end up in /usr/lib64/... Check the output of setup.py, it will tell you. Posted by: Brian on Sep 20, 2012

Where exactly should I put the unzipped MySQL-python-1.2.3 folder? Right now it's in my downloads but is it okay to leave it there? Posted by: Stephanie on Feb 08, 2015

leave a comment on tutorial leave a comment