Installing MySQL and MySQLdb on OSX
I've just set up my dev environment for Django. Again. This is the fourth time I've had to do it. As always, installing the Python MySQL drivers was the biggest PITA. Google this issue and you'll find literally hundreds of people trying and struggling with this. Read the comments threads and you'll find a load more pople who tried, failed and finally gave up.
Some of the stuff I've googled and found has been useful for some installs, most have been interesting, but not fixed my problem(s). This blogpost is a collection of the techniques, explanations, error messages (and how to react) and things-I-needed-to-know but didn't (and how to find them out. I just know I'll need these notes the next time I need to step through this process, so putting them here semed like a good idea.
So, just to clarify.... THIS ISN'T A STEP-BY-STEP GUIDE TO HELP YOU INSTALL MYSQL AND MYSQLDB. There's already a ton of those and most seem to be out-of-date and fail at a crucial point without any pointer as to how to handle the error message you're seeing.
If you're reading this it's more than likely you found this post via google. Well done you. If that's the case it's likely that something on this page is relevant to you, but most stuff isn't. Feel free to skim-read. I'm not precious. This isn't my magnum opus. Also, I hope that explains the fragmented, non-sensical, non-linear, train-of-thought nature of this post.
Have fun! Stay calm! Good luck!
Stuff you're gonna need
- XCode
- Python
- MySQL
- MySQL-Python
Stuff you might need to know
What version of Python are you running?
Open a terminal window and type 'python' (without the quotes to run the python interpreter
$ python Python 2.5.1 (r251:54863, Nov 11 2008, 17:46:48)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Aha! I'm running 2.5.1. Hit Ctrl + C to quit python
What version of OSX are you running?
In the Apple Menu, click 'About This Mac' and you should get the Version Number under the OSX Apple Logo. I'm running 10.5.6, 'cos I'm old-fashioned.
What version of XCode do you have installed?
Make sure you've got the latest version that's compatible with your OS. Older versions of XCode can be downloaded from connect.apple.com (you'll need a Mac Developer Center account to log-in):
The last version that's available for Leopard (OSX 10.5.x - which is what I'm running) is XCode 3.1.3
Does your Mac have a 32-bit or 64-bit processor?
A support note on apple.com details how you can discern whether you hvae a 32-bit or 64-bit processor.
- Choose About This Mac from the Apple menu in the upper-left menu bar, then click More Info.
- Open the Hardware section.
- Locate the 'Processor Name'.
Mine's "Intel Core 2 Duo"
| Processor Name | 32-bit or 64-bit? |
|---|---|
| Intel Core Solo | 32 bit |
| Intel Core Duo | 32 bit |
| Intel Core 2 Duo | 64 bit |
| Intel Quad-Core Xeon | 64 bit |
| Dual-Core Intel Xeon | 64 bit |
| Quad-Core Intel Xeon | 64 bit |
Do you have MySQL installed already?
Open Terminal and type the command 'mysql' (without the quotes).
$ mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5089
Server version: 5.1.45 MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
I have Version 5.1.x running. To find out if you have a 32-bit or 64-bit version of MySQL, try:
SHOW GLOBAL VARIABLES LIKE 'version_compile_machine';
You'll either get the following feedback...
+-------------------------+-------+
| Variable_name | Value |
+-------------------------+-------+
| version_compile_machine | i386 |
+-------------------------+-------+
1 row in set (0.00 sec)
OR, you'll see
+-------------------------+--------+
| Variable_name | Value |
+-------------------------+--------+
| version_compile_machine | x86_64 |
+-------------------------+--------+
(You can quit this command line environment by hitting Ctrl + D.) The 'x86_64' value refers to the 64-bit version. i386 refers to a 32-bit version. If you have something different, Google your value to find out which version you have.
Uninstalling MySQL to start again
Instructions covering uninstalling MySQL courtesy of Rob Allen over at akrabat.com.
Caution! These steps will uninstall completely remove MySQL it (including all databases) from OSX Leopard - if you have databases consider backing them up with a mysqldump command at this point:
First, stop the database server, then....
sudo rm /usr/local/mysql
sudo rm -rf /usr/local/mysql*
sudo rm -rf /Library/StartupItems/MySQLCOM
sudo rm -rf /Library/PreferencePanes/My*
edit /etc/hostconfig and remove the line MYSQLCOM=-YES-
rm -rf ~/Library/PreferencePanes/My*
sudo rm -rf /Library/Receipts/mysql*
sudo rm -rf /Library/Receipts/MySQL*
The last two lines are particularly important if you're needing to install an older version of MySQL.
Four common errors, and how to tackle them
Problem #1: command 'gcc' failed with exit status 1
Symptom: When you run the install script for MySQL-python, you get an error traceback ending with the following line:
error: command 'gcc' failed with exit status 1
Problem: XCode isn't compiling.
Solution: Upgrading my XCode fixed this - I was running XCode 2.2.1 and upgraded to 3.1.3. XCode's installer should upgrade your Developer Tools without a problem, but if you have problems you can uninstall your exisintg XCode and install from scratch, uninstall like so:
sudo <Xcode>/Library/uninstall-devtools --mode=all
<Xcode> is the directory where the tools are installed. For typical installations the full path is /Developer/Library/uninstall-devtools
Problem #2: dynamic module does not define init function
Symptom: When you fire up the python interpreter and try to import MySQldb, you get the following error...
ImportError: dynamic module does not define init function (init_mysql)
Problem: The comments thread on Andy Dustman's blogpost provides me with the answer to this one. You have a 64bit mysql package and a 32bit python package. If you previously tried to build and install against one version of mysql, then installed a new, different version of MySQL this may happen.
Solution: Uninstall MySQL and install the right package - I've already covered this, scroll up a bit...
Problem #3: ERROR 1006 (HY000): Can't create database
Symptom: You've installed MySQL, but now you can create a database :( You get an error like this...
mysql> create database temp;
ERROR 1006 (HY000): Can't create database 'temp' (errno: 2)
A restart fixes this. Like it's dumb ol' Windoze or something. I have no idea why.
Problem #4: UserWarning: Module _mysql was already imported
Symptom:
You fire up a python interpreter, but get an import error when trying to import MySQLdb...
>>>import MySQLdb
UserWarning: Module _mysql was already imported from /Library/Python/2.5/site-packages/MySQL_python-1.2.2-py2.5-macosx-10.5-i386.egg/_mysql.pyc, but /Users/gareth/MySQL-python-1.2.2 is being added to sys.path
Soilution: You need to delete the directory you built from....
rm -rf MySQL-python-1.2.x/
And finally.... a note about commenting out ifndef uint in _mysql.c
Several posts detail a bug in MySQL-Python which is fixed by commenting out a few lines and doing a quick find-and-replace before running the install. Something like this:
#ifndef uint
#define uint unsigned int
#endif
This bug was fixed in the 1.2.3 release.
Related Links
Latest Posts
Muppets Birthday Card
5:47p.m., 28 Nov
Emma loves The Muppets. She even has her own Muppet who we call Emma Too and who was born at ...Detecting Online Status In The Browser
11:55a.m., 28 Nov
I was just heading into a meeting when I was asked how our (mostly web-based) iOS application was going to ...Dropping Support for Internet Explorer 6
2:37p.m., 11 Oct
Microsoft's Internet Explorer 6 has long been the bane of every front-end developer's life. It's a 10-year old browser - ...Xfm Buzz - A Radio Hack
1:15p.m., 31 May
At Global Towers we developers have 10% time to go away and hack at something that might, ultimately, bring value ...