Installing MySQLdb

When unresolved linker symbols attack.

Good news: you can get reliable and easy access to a proper, fully-featured database from within Python.

Bad news: you may have to tinker to get it to (a) install properly. And if not done properly, this may cause it to (b) not work properly.

Good news: Download a .dmg of MySQL for MacOSX from MySQL.org. Double-click and install the MySQL package, the MySQL tools package and the preference pane. This works seemlessly and is much better and quicker than compiling from source or using fink / darwinports etc. Also, the pref pane lets you quickly start up the MySQL server or have it startup automatically when the system boots.

Addendum: Matt Baker reports that he ended up with a malfunctioning MySQLdb egg that was apparently installed by the MySQL .dmg, which would explain other weird behaviour. The solution is to delete and install it another way.

Bad news: MySQL is installed off the main executable path in /usr/local/mysql. This isn't a problem in itself, as the prompt can be easily raised:

localhost$ /usr/local/mysql/bin/mysql mysql>

However it can cause problems in the later installation of MySQLdb, the Python package required for interaction, as it can't find the mysql_config executable.

Good news: There are several feasible ways of fixing this. First, you could edit your .bash_profile file in your home directory and add aliases to point at the useful binaries:

alias mysql='/usr/local/mysql/bin/mysql' alias
mysql_config='/usr/local/mysql/bin/mysql_config'

The MySQLdb instructions suggest an alternative way of telling the build process where the necessary files are, by editing the site.cfg file.

Bad news: MySQLdb may still not install. (Depends. I've had variable luck on different OSX systems that should had fairly similar configurations. What I give above is what worked for me.) The symptoms are that it complains about not being able to find mysql_config and then generates a bunch of linker errors. Or, it apparently installs but when you go into Python and type:

>>> import MySQLdb

you get a bunch of errors then as the library has been built and installed but is non-functional.

Good news: Instead of adding the aliases. adding the MySQL binaries directory to your $PATH, the build stage will work. In your .bash_profile file, add something like:

PATH="${PATH}:/usr/local/mysql/bin"

so that the build can find all the MySQL binaries. If compiling on a PPC, you may still get an error message about an "unrecognised architecture" but this can be ignored safely.

Great news: This works.

Later news: Each round of installing Plone shows me new ways that installation can go awry. This latest time was no different. A source install of MySQLdb as above failed, installing into Python without apparent problems but dropping at the import stage, with errors like:

>>> import MySQLdb Traceback (most recent call last): File "<stdin>",
line 1, in ? File
"/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages
/PIL/__init__.py", line 19, in ? File
"build/bdist.macosx-10.3-fat/egg/_mysql.py", line 7, in ? File
"build/bdist.macosx-10.3-fat/egg/_mysql.py", line 6, in
__bootstrap__ ImportError: Failure linking new module:
/Users/johnB/.python-eggs
/MySQL_python-1.2.2-py2.4-macosx-10.3-fat.egg-tmp/_mysql.so: Library
not loaded: /usr/local/mysql/lib/mysql/libmysqlclient_r.15.dylib
Referenced from:
/Users/johnB/.python-eggs/MySQL_python-1.2.2-py2.4-macosx-10.3-fat.egg-tmp/
_mysql.so Reason: image not found

Installing a pre-compiled fat Mac binary as suggested by these guys did the trick in the end. Note that for this to work, the dysfunctional library had to be removed by hand. Go to you site-packages directory, delete the mySQLdb egg and delete the line referring to it in the easy-install.pth file.