tags:

views:

960

answers:

3

I'm a Python newbie, but I've just spent a day working out how to get MySQLdb working properly, and the universe according to google includes numerous references to what a PITA it is, and an inordinate number of guides that seem to be outdated. Given that this site is intended to address these sorts of problems, and I know that I'm going to need a reference to the solution in future, I'm going to ask the question, provide my answer and see what else floats to the surface.

So the question is how to get MySQLdb working on Mac OS X?

+4  A: 

Install mysql and python via link text The ortters have done all the difficult work.

sudo port install py26-mysql 
sudo port install mysql5-server

should install what you need. (see [Stack overflow for comments re mysql server])[2]

In general macports (or fink) help when there are complex libraries etc that need to be installed.

Python only code and if simple C dependencies can be set up via setuptools etc, but it begins to get complex if you mix the two.

Mark
Note that when starting use MacPorts, make sure that your path prefers /opt/local/bin over /usr/bin as MacPorts installs things under /opt/local.
tequilatango
Yes! And skip the second line if you just need to connect to an existing MySQL server. Unfortunately, with the number of dependencies involved, trying to use python with mysql on OS X is one case where it is easier to let MacPorts install an additional python instance rather than trying to play package manager yourself.
Ned Deily
This worked great, thanks!
pyrony
As a sidenote, if you are getting a deprecation warning, the following page has the fix: https://bugs.launchpad.net/python-mysqldb/+bug/341943
pyrony
+5  A: 

Here is the tale of my rambling experience with this problem. Would love to see it edited or generalised if you have better experience of the issue... apply a bit of that SO magic.

First off, the author (still?) of MySQLdb says here that one of the most pernicious problems is that OS X comes installed with a 32 bit version of Python, but most average joes (myself included) probably jump to install the 64 bit version of MySQL. Bad move... remove the 64 bit version if you have installed it (instructions on this fiddly task are available on SO here), then download and install the 32 bit version (package here)

There are numerous step-by-steps on how to build and install the MySQLdb libraries. They often have subtle differences. This seemed the most popular to me, and provided the working solution. I've reproduced it with a couple of edits below

Step 0: Before I start, I assumed that you have MySQL and Python installed on the mac.

Step 1: Download the latest MySQL for Python adapter from SourceForge.

Step 2: Extract your downloaded package:

tar xzvf MySQL-python-1.2.2.tar.gz

Step 3: Inside the folder, clean the package:

sudo python setup.py clean

COUPLE OF EXTRA STEPS, (from this comment)

Step 3b: Remove everything under your MySQL-python-1.2.2/build/* directory -- don't trust the "python setup.py clean" to do it for you

Step 3c: Remove the egg under Users/$USER/.python-eggs

Step 4: Originally required editing _mysql.c, but is now NO LONGER NECESSARY. MySQLdb community seem to have fixed this bug now.

Step 5: Create a symbolic link under lib to point to a sub-directory called mysql. This is where it looks for during compilation.

sudo ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql

Step 6: Edit the setup_posix.py and change the following

mysql_config.path = "mysql_config"

to

mysql_config.path = "/usr/local/mysql/bin/mysql_config"

Step 7: In the same directory, rebuild your package (ignore the warnings that comes with it)

sudo python setup.py build

Step 8: Install the package and you are done.

sudo python setup.py install

Step 9: Test if it's working. It works if you can import MySQLdb.

python

>>> import MySQLdb

One final hiccup though is that if you start Python from the build directory you will get this error:

/Library/Python/2.5/site-packages/MySQL_python-1.2.3c1-py2.5-macosx-10.5-i386.egg/_mysql.py:3: UserWarning: Module _mysql was already imported from /Library/Python/2.5/site-packages/MySQL_python-1.2.3c1-py2.5-macosx-10.5-i386.egg/_mysql.pyc, but XXXX/MySQL-python-1.2.3c1 is being added to sys.path

This is pretty easy to Google, but to save you the trouble you will end up here (or maybe not... not a particularly future-proof URL) and figure out that you need to cd .. out of build directory and the error should disappear.

As I wrote at the top, I'd love to see this answer generalised, as there are numerous other specific experiences of this horrible problem out there. Edit away, or provide your own, better answer.

mblackwell8
Note that the Apple-supplied Python on 10.6 (Snow Leopard) prefers to run 64-bit (it's 32-bit/64-bit universal). There are other pitfalls, too. For a while in the mysql 5.1.x series, the OS X tarballs packaged by mysql were faulty (claimed to be universal but weren't). The data base adapter for Python, MySQLdb, has had its own issues. This is one case where you are well advised to just use MacPorts to build everything you need automatically. It's just too easy for something to go wrong.
Ned Deily
A: 

I had this same issue once. I documented it for myself at

http://arcoleo.org/dsawiki/Wiki.jsp?page=MySQLdb%20Installation

Dave