Setting up MySQL for Python (MySQLdb) on Mac OS X

MySQL is an excellent, very popular and open source database management system. A binary installation is available for Mac OS, and the process of installing MySQL on Mac OS is also very well documented. So just head over to MySQL.com, where you will find the binary installation, together with extensive installation, and usage documentation.

To connect to MySQL from Python use MySQL for Python (MySQLdb). Get MySQLdb from http://sourceforge.net/projects/mysql-python/.

    1. Download MySQL from mysql.com and install the DBMS as instructed in the documentation.
    2. Make sure “Library/Python/2.3” does not have:
  • Directory “MySQL”
  • File “_mysql.pyd”
  • File “_mysql_exceptions.py”
  • File “_mysql_exceptions.pyc”

Either uninstall MySQL for Python if a setup program was used to install it, otherwise manually delete them.

  1. Download and unpack MySQL for Python. At the time of writing this is MySQL-python-1.2.1_p2.tar.gz .
  2. Open Terminal and change to the directory where MySQLdb was unpacked to.
  3. Get rid of any previous builds that might interfere, by deleting the “build” directory if it exists.
  4. Edit the setup.py file, and change:

    return popen(“mysql_config –%s” % what)
    to
    return popen(“/usr/local/mysql/bin/mysql_config –%s” % what)

  5. Cleanup any previous install attempts:

    python setup.py clean

  6. Build MySQLdb:

    python setup.py build

Viola! MySQL for Python is all setup, and ready for your Python-MySQL data access code. Enjoy!