Surely there were going to be some issues getting Django working on my Mac (I mean aren’t there are always some problems?), so I decided to crack open a text document and keep track of the things that happened so that in the future when I do this, I don’t run into any problems.
Just as a bit of context, I’ve been playing with Google App Engine recently and it’s been my first attempt at writing Python code. Python and Django? Love ‘em. App Engine? Eh, good at what it does, but I don’t really like working in Google’s sandbox. It just feels so restrictive. So for my next project I decided to skip the App Engine bit and try to build a site still using Django, but with a MySQL backend. Because of my work with App Engine, I already had installed Django and obviously Python 2.6 and 2.6 come with a standard Snow Leopard install, so I had those as well.
So from there I set off. The first thing I knew I would need was MySQL. I’ve used MySQL before, but since doing a clean install of Snow Leopard a few months ago, I hadn’t put it back on, so first to handle that.
Finding the binary installer for Snow Leopard was easy, but here I made a mistake.Â I figured I’d install the 32-bit version which ended up causing me some headaches.Â What I didn’t realize is that Snow Leopard installs most programs as 64-bit if your machine is 64-bit.Â How do you find out if your Mac is 64-bit or not?Â See this short post on how to tell from simply looking at your processor.Â So I mistakenly installed the 32-bit version and was on my way.
The next thing you need to do is install a MySQL Python adapter.Â The source for that is here.Â I chose to sudo python setup.py build/install, but you could use easy_install if you want.Â Once I installed, I tried to import the MySQLdb package and got the following error:
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/backends/mysql/base.py", line 13, in
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
This post and others indicated it was related to the build architecture of Python and MySQL. Bah, so this is when I realized I needed to go back and install the 64-bit version of MySQL.Â Okay, so I downloaded the 64-bit installer and ran that.Â (See further below how that worked, but not completely and I eventually did a clean install.)
Then I ran the following to try to re-install the MySQLdb adapter stuff:
ARCHFLAGS='-arch x86_64' python setup.py build
ARCHFLAGS='-arch x86_64' python setup.py install
No dice, still not working. Poking through the comments of the post above, I came across this indicating that re-building doesn’t actually do it from scratch and you need to go into the build directory and manually rm *.Â Once I did that, then I was in business — the import MySQLdb statement now worked.
Following the Django tutorial, I created by app, code-named “sanddollar.” So far so good until I got to:
python mange.py syncdb
and crossed my fingers. Forgot to create the database, but after that I still have a problem. I’m getting an error related to MySQL being able to create/write to a file:
File "build/bdist.macosx-10.6-universal/egg/MySQLdb/connections.py", line 36, in defaulterrorhandler
_mysql_exceptions.InternalError: (1, "Can't create/write to file '/usr/local/mysql/data/sanddollar/auth_permission.MYI' (Errcode: 2)")
This was a tough one.Â It spawned this Stack Overflow question, but after some poking around I noticed that despite the fact I was able to create databases through the MySQL Workbench, they weren’t creating new database directories under the mysql/data folder.Â My suspicion was that multiple installs on MySQL had gotten MySQL in a messed up state.Â Solution: remove all traces of MySQL and then do a clean install.
This sounds pretty straightforward, but it wasn’t.Â In the end, it took two sources (here and here) to get all the instructions for wiping MySQL where the second set of instructions is specific to Snow Leopard.Â Alright, once I had done that, I used the same .pkg installer to install the 64-bit version of MySQL one more time.
Then I tried to connect to MySQL, and I get the following error:
ERROR 2002 (HY000): Canâ€™t connect to local MySQL server through socket â€˜/tmp/mysql.sockâ€™ (2)
Fairly straightforward fix.Â No idea where the actual mysql.sock file is, but because I could connect via MySQL Workbench over TCP, someone in the IRC channel tipped me off to run:
which outputted the location of the mysql.sock file.Â In my case, it was located at /var/lib/mysql/mysql.sock.Â So then I ran the following from a bash shell:
$sudo ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
and voila, now I can connect from Terminal.Â Almost there.Â I created a new user for the “sanddollar” database named “sanddollar” and granted the user privileges for basic CRUD operations at host 127.0.0.1 (for some reason localhost wouldn’t work).Â Tried to connect through Terminal, worked great. Then tried the syncdb command again and this time paydirt.
Final thoughts: 32-bit/64-bit is complicated, more complicated than it feels like it should be.Â This makes working with Snow Leopard as a development environment a pain.Â However, I learned about the “file [filename]” unix command to determine the architecture of programs.Â No idea what it means but opened it up on Stack Overflow with this question. In the end, not too bad for getting things started.Â Feels like it could be simpler, but less than a day isn’t that bad, so onwards!