In this post, we will discuss the steps needed to get your django work with mysql database on Mac OS Lion – there are 2 ways you can use mysql. It comes packaged with MAMP and if you are already using it, you can configure mysql within MAMP to get running with django. The advantage is MAMP’s front end controls and built in phpmyadmin. Otherwise, you can install both mysql and phpmyadmin fresh.
But before that, you should install python and django as covered nicely in Django Book. What it doesn’t cover in detail is getting the database setup, so refer to the following once you reach ‘Setting up the database’ section.
- First, we need MySQL server. If you already have MAMP installed, you dont need this step, skip over to next. If you dont, lets install MySQL community server from http://dev.mysql.com/downloads/mysql/ I downloaded the then latest mysql-5.5.20-osx10.6-x86_64.dmg and followed the instructions. If you set the preferences correctly in Startup Package and Preferences Pane (double click MySQLStartupItem.pkg and then MySQL.prefPane), you can configure mysql to be started/stopped from System Preferences. We did this to have easy controls to start and stop mysql.
- Setup mysql-python package- Although you have installed mysql, we need MySQLdb package to allow it to work with python. I have heard you can use pip or easy_install to install this package easilybut it can start giving errors sometime. So, here is the longer route but one which I have successfully tried and hopefully you can too. First, get mysql developer package to have all the files-
- From the same mysql download url, download the latest tar.gz package (version should be same as the dmg file you downloaded in step 1). Put it in your home dir and execute following command-cd ~; tar -xvzf mysql-5.5.20-osx10.6-x86_64.tar.gz
- This will create a dir called mysql-5.5.20-osx10.6-x86_64. Copy this dir over to /usr/local using the command sudo cp -r mysql-5.5.20-osx10.6-x86_64 /usr/local/
- Now, rename this dir in /usr/local to mysql using
sudo mv -r /usr/local/mysql-5.5.20-osx10.6-x86_64 /usr/local/mysql
With this, we have created a hard copy of our latest mysql. If you upgrade your mysql in future, follow the same steps. Alternatively, you can create a symlink too.
- Add this to your .bash_profile (created in previous post in your home dir) so that your system will know where to find mysql related files –
Next, install mysql-python package-
- Download the latest MySQL-python package from http://sourceforge.net/projects/mysql-python/files/mysql-python/ to your home dir
Execute following commands-
cd ~; tar -xvzf MySQL-python-1.2.3.tar.gz
- Edit site.cfg on vi and change the mysql path as follows
mysql_config = /usr/local/mysql/bin/mysql_config
- Lets build and install MySQL-python package now:
sudo python setup.py build
sudo python setup.py install
- IMPORTANT: Edit your ~/.bash_profile, add the following line:
On terminal, type python to start python shell. On python shell, type ‘import MySQLdb’. If you see no errors, you are good to go!
The steps 2-3 are required both with and without MAMP. Next, few helpful things-
- with MAMP – if you have MAMP, you already have phpMyAdmin installed and instead of starting MySql through system preferences, you can control it from MAMP interface. Further, I have these in my .bash_profile to use mysql from MAMP-
Other thing to note, when you will setup your own django project, use this in settings.py -‘HOST': ‘/Applications/MAMP/tmp/mysql/mysql.sock’
- without MAMP – lets install phpMyAdmin to manage our mysql server using instructions given here. I tried and it worked great, so I dont want to duplicate the efforts. Lastly, in your settings.py, you will leave ‘HOST’ empty when you setup your own django project.
So, are there any particular advantage of using mysql with or without MAMP? – I dont think so. I used both of them and did not feel any difference worth worrying over. So, take your pick – if you have MAMP, use it. If you dont, you definitely dont need it for django.
See previous article on Getting started to code with Python/Django – Learn Python/Django and get ready to launch your own idea on Internet
In next and last article, I will post how I learned the rest and got to build my own django app.