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:
cd MySQL-python-1.2.3c1 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!