Getting Django & Python to work with MySQL on OS X 10.6.8

When setting up a new mac or fresh install of Mac OS X there is a issue that many developers may come across. The issue is with getting MySQL-python to work properly. This guide was written with 10.6.8 in mind, but it will probably work for earlier versions of the OS as well as (hopefully) 10.7 a.k.a.Lion.

EDIT: This will get you up and working, but for some reason it needs you to type the following into terminal before you start each time:

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/

After you have typed this it will work until you next quit/restart. I will update this when I have figured out the best way of adding this into your paths so that you don't have to do it each time. If you know then feel free to comment below. Thanks.

The way to get this to work is a good clean install. You can google for how to remove older versions of MySQL , and I wouldn't remove Python since some OS utilities apparently use it. We will also need to get Xcode so that some of the files can be compiled.

Download Xcode (It's about 4Gb so find sonething else to do for a bit): http://developer.apple.com/tools/Xcode/

Download MySQL(Latest 64bit): http://dev.mysql.com/downloads/mysql/

Download Python (2.7.2 64bit): http://www.python.org/getit/

Install both. Install the MySQL pref pane too and start the MySQL service/server.

Open up terminal and type:

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/

Keep terminal open after doing this until we are done. Use the same window for the following. /usr/local/mysql/lib/ is where MySQL is where the MySQL installer puts MySQL. The command is so that the next bit we do has the correct path for MySQL when it is built. You will also need PIP for this bit so get that and install it. You may have to download and install easy_install. Easy install can install MySQL-Python, but PIP is a better installer so it's more likely to work.

PIP: http://guide.python-distribute.org/installation.html

Now we have PIP we can get on with getting the rest fixed. We need the Python MySQL bindings so they can talk to each other. PIP will do this for us.

sudo pip install MySQL-python

That should all work now.

if you want Django too, then just use easy_install or PIP

sudo PIP install Django

To see your databases you can use the command line, or use Sequel Pro

Choose connect to socket, which will connect to the default MySQL port and use the username 'root' & password is left blank. If everything worked correctly you will now be connected.

Create a user with all privileges and an empty Database.

In your Django settings fill out the DATABASE with your details… Like this:

DATABASES = {
    'default': {
           'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. 
           'NAME': 'testdb',                      # Or path to database file if using sqlite3. 
           'USER': 'testuser',                      # Not used with sqlite3. 
           'PASSWORD': 'testpass',                  # Not used with sqlite3. 
           'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3. 
           'PORT': '',                      # Set to empty string for default. Not used with sqlite3. 
    } 
}

Of course you would want more security on a live/web facing server!

Hopefully this will work for you or get you going in the right direction (and save you hours of pain).

I read a lot of other blogs but between these few I managed to piece it all together. Big thanks to the people who put me on the right path:

http://davidmichaelthompson.com/2008/02/18/notes-installing-django-python-mysql-on-os-x/

http://devnet.jetbrains.net/message/5292163

http://qor72.blogspot.com/2009/04/mysql-os-x-and-easyinstall-mysql-python.html

Published on

Filled under

Discuss!

comments powered by Disqus