One of the biggest pains I have when developing on OSX is always trying to get MySQLdb working. Its one of those things that just doesn’t seem to be easy enough. I prefer not having MySQL running right off my desktop if I can so I will describe how I get MySQLdb running without installing the whole server, on Snow Leopard, using a VirtualEnv, and running 64bit.

 

Step 1

Download the necessary packages


wget http://www.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.13-osx10.6-x86_64.tar.gz/from/http://mysql.mirror.rafal.ca/

wget http://sourceforge.net/projects/mysql-python/files/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz/download

 

Step 2

Uncompress the archives


tar -zxvf mysql-5.5.13-osx10.6-x86_64.tar.gz

tar -zxvf MySQL-python-1.2.3.tar.gz

 

Step 3

The first thing thats needed to be done is located the exact path of mysql_config, which should be located directly in the bin/ folder of the extracted files from MySQL, for my files they are located in ~/temp/mysql-5.5.13-osx10.6-x86_64/bin/ with this in mind change your working path to the root of your extracted MySQL-python-1.2.3 and modify the site.cfg file so that it now contains a mysql_config


vim site.cfg

mysql_config = ~/temp/mysql-5.5.13-osx10.6-x86_64/bin/mysql_config

 

Step 4

Now its time to build the library while still in the root of your extracted MySQL-python-1.2.3 be sure to run the build


export ARCHFLAGS="-arch i386 -arch x86_64"

python setup.py build

note: I added the export to allow for 64bit compiling under the newer versions of xcode

 

Step 5

Now that the build is complete, you don’t have any use for the extracted version of MySQL server, but we still need the libraries. Enter back into the root of the mysql-5.5.13-osx10.6-x86_64 and lets put them on our systems. Afterwards you can delete the files associated with MySQL Server (mysql-5.5.13-osx10.6-x86_64)


cd ./lib/

cp ./*.dylib /usr/lib/

 

Step 6

From here its up to you, you can install the libraries system wide or you can install them on a per environment bases


source ./bin/activate

cd ~/temp/MySQL-python-1.2.3/

python setup.py install

 

Once you have completed all these simple tasks you will have a system or environment with a mysql connectable python. Also located in the MySQL-python-1.2.3 folder there will now be a dist/ folder which will contain a nicely packaged egg that you can use for future installs on your system. Which makes the whole process easier to install assuming you picked the route of installing on a virtual environment. Save the egg for later and use it again when you need a new install in a new environment.


easy_install MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg