Installing MySQL on Snow Leopard

These are instructions for compiling and installing a 64-bit version of MySQL, the world’s most popular open source database, on Mac OS X 10.6 (Snow Leopard).

The benefits of manually building MySQL yourself in /usr/local are detailed here. I also wrote a in-depth explanation of why you might want to build MySQL yourself in my Compiling MySQL on Leopard article.

No Support

These instructions will probably work just fine and you won’t run into any trouble, but if you do, please don’t email me about it. I wish I could help everybody who might have an issue, but I just don’t have the time to help you troubleshoot hundreds of potential variables with your specific configuration, and I probably won’t even reply to the email, and I’m sorry in advance for that.


Before following these instructions, you will need:

  1. Mac OS X 10.6 Snow Leopard
  2. The latest Xcode Tools (from the Snow Leopard DVD or downloaded from Apple — the 10.5 version won’t work)
  3. Confidence running UNIX commands using the Terminal

If you want to learn more about UNIX and the command line, check out my PeepCode screencast on this topic.

Step 1: Set the PATH

Launch from the /Applications/Utilities folder.

We need to set your shell’s PATH variable first. The PATH variable determines where your system searches for command-line programs. Using the editor of your choice, create and edit a file in your home directory named .profile (note the “.” preceding the filename).

If you’re using TextMate like you should be and have installed the UNIX mate command, then you can create and start editing the file like this:

mate ~/.profile

To the end of this file, add the following line (or verify that it’s already there):

export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH"

Close and save the file and run this command to load the new setting into your current shell:

source ~/.profile

To verify that you’ve updated your path, enter the following command:

echo $PATH

You should see /usr/local/bin at the beginning of the line returned by the system.

Step 2: Download

We’re going to create a folder to contain the files we’re about to download and compile. If you want, you can delete this folder when you’re done, but keeping it around makes it easier to re-install (or uninstall) these apps later.

Make the new folder:

mkdir ~/src
cd ~/src

I used to provide a link to download the latest version of MySQL, but they update frequently and sometimes the links die, so instead, please go to the MySQL site, and download the latest version from this page, picking a mirror near you. Then move or copy it to your src folder.

For those of you who expect the world, you can try this command, but please don’t email me if it’s broken:

curl -O

Step 3: Compile and Install

Build and install MySQL like this:

tar xzvf mysql-5.1.37.tar.gz
cd mysql-5.1.37
./configure --prefix=/usr/local/mysql --with-extra-charsets=complex \
--enable-thread-safe-client --enable-local-infile --enable-shared \
sudo make install
cd /usr/local/mysql
sudo ./bin/mysql_install_db --user=mysql
sudo chown -R mysql ./var
cd ..

Starting (and Auto-Starting) MySQL

In most cases, you’ll want MySQL to auto-start every time you boot (or reboot) your Mac. The easiest way to do this is using launchd, Mac OS X’s infrastructure for managing system processes.

I’ve prepared a launchd plist file that will allow you to manage MySQL, starting it at boot and stopping it cleanly at shutdown. Save the plist file to your ~/src directory and then move it to the proper place with the following commands:

cd ~/src
curl -O
sudo mv ~/src/com.mysql.mysqld.plist /Library/LaunchDaemons
sudo chown root /Library/LaunchDaemons/com.mysql.mysqld.plist

Finally, tell launchd to load and startup MySQL:

sudo launchctl load -w /Library/LaunchDaemons/com.mysql.mysqld.plist

If you see no response, it probably means that everything worked correctly, and that MySQL is running. You can verify this by launching MySQL’s command-line monitor:

mysql -uroot

You should see something like this:

Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 1
Server version: 5.1.37 Source distribution
Type 'help;' or 'h' for help. Type 'c' to clear the buffer.

If this prompt appears, MySQL has been installed correctly. Type exit and press return to quit the MySQL monitor.

If you didn’t see that message, it’s possible that something “bad” happened, or that you may have missed a step above. You can always try running through the instructions again.

You now have a custom-built 64-bit version of MySQL.

Starting and Stopping MySQL Manually

If you ever want to stop MySQL manually, use this command:

sudo launchctl unload -w /Library/LaunchDaemons/com.mysql.mysqld.plist

To (re)start MySQL manually, use this command:

sudo launchctl load -w /Library/LaunchDaemons/com.mysql.mysqld.plist

Extra Credit: The Rails Gem

You can install the Rails MySQL Gem by pointing it at your new MySQL installation direcory:

sudo gem install mysql -- --with-mysql-dir=/usr/local/mysql

That should do it.

More articles in the Archive →