Installing MySQLdb on Lion

Captured at: 2013-05-02 15:24:02 UTC

I’ve been meaning to install and use MySQLdb (a Python library that allows one to access MySQL databases) on OS-X Lion, and had to run around for nearly an hour to finally get it working. So heres a small HowTo for those who might also be looking at doing it without wasting all that time.

For starters, you need to have Python and setuptools.

1. Install the latest version of MySQL from here. Make sure you download the “.dmg” version. Look here for more instructions on how to get this done.

2. Ideally, if the world were perfect, we would have been able to download MySQLdb from here, gone through the regular Python setup process and be done. Unfortunately, life isn’t meant to be that easy, so we will have to do a little work here. First things first, let us add MySQL to our path. To do this append the following line to your ~/.bash_profile file.

Appending MySQL to the Path

export PATH=$PATH:/usr/local/mysql/bin
# Aliases for MySQL
alias mysql=/usr/local/mysql/bin/mysql
alias mysqladmin=/usr/local/mysql/bin/mysqladmin
alias mysql_config=/usr/local/mysql/bin/mysql_config

The aliases are not really needed after we’ve added MySQL to the path, but let us just go with this for now.

3. Try this (Replace .xx. which is .18. in the example below, with the version that you have).

Otool on libmysqlclient.dylib

otool -D `/usr/local/mysql/lib/libmysqlclient.18.dylib`
/usr/local/mysql/lib/libmysqlclient.18.dylib:
libmysqlclient.18.dylib

If you don’t see the path at the end like me (in the example above), this will likely be a problem for you during installation later. Just to be more explicit, you will end up seeing the following error:

Error due to libmysqlclient not containing path

In [1]: import _mysql
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
/Users/arvind/Downloads/mysql-5.5.24-osx10.6-x86_64/<ipython-input-1-524926236c88> in <module>()
----> 1 import _mysql

/Users/arvind/Downloads/mysql-5.5.24-osx10.6-x86_64/build/bdist.macosx-10.7-intel/egg/_mysql.py in <module>()

/Users/arvind/Downloads/mysql-5.5.24-osx10.6-x86_64/build/bdist.macosx-10.7-intel/egg/_mysql.py in __bootstrap__()

ImportError: dlopen(/Users/arvind/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
  Referenced from: /Users/arvind/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp/_mysql.so
  Reason: image not found

Fixing the above error requires the following:

Adding path to library

sudo install_name_tool -id /usr/local/mysql/lib/libmysqlclient.18.dylib
/usr/local/mysql/lib/libmysqlclient.18.dylib

Given this, if we had to repeat the step with otool, we will find that the path to the library is now included in it. Now we should be all set to install MySQLdb. (Credit)

4. Download MySQLdb from here. Now, we go through the typical steps for a setuptools install:

Basic Installation Steps

tar -czf MySQL-python-1.2.3.tar.gz
cd MySQL-python-1.2.3
python setup.py build
python setup.py install

The last step might need a ‘sudo’ access.

Finally, we should be ready to test if MySQLdb is installed. Try:

Import MySQLdb

import _mysql
import MySQLdb

If it goes past this quietly without throwing any exceptions, congratulations – you’ve just finished installing MySQLdb. Time to start playing around with those databases from Python!