Setting up Django with MySql – with and without MAMP

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.

  1. 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.
  2. 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 -
      export PATH=$PATH:/usr/local/mysql/bin

    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
      cd MySQL-python-1.2.3
    • 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:
      export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/
  3. Lets test our setup to make sure everything is working as expected-
    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-

  1. 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-
    alias mysql=/Applications/MAMP/Library/bin/mysql
    alias mysql_config=/Applications/MAMP/Library/bin/mysql_config
    Other thing to note, when you will setup your own django project, use this in settings.py -’HOST’: ‘/Applications/MAMP/tmp/mysql/mysql.sock’
  2. 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.

  

5 thoughts on “Setting up Django with MySql – with and without MAMP

  1. MAMP and MAMP Pro users, can try Python4MAMP, simple and easy to install add-on to get Python, Django, PIL… working on MAMP

  2. I realized my explanation lacked detail. I have found this problem trying to use mamp. This is the same issue I always run into with mamp. I have never figured out why.

    Connection to Django default failed
    java.sql.SQLException: Access denied for user ‘root’@’localhost’ (using password: YES)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4190)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4122)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:927)
    at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4689)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1304)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2486)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2519)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2304)
    at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:834)
    at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:346)
    in RemoteDriverImpl.connect(RemoteDriverImpl.java:27)
    at com.sun.proxy.$Proxy84.connect(Unknown Source)
    in LocalDataSource.getConnection(LocalDataSource.java:158)

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>