Technology & Life Hacking

Installing MySQL from source on Mac OS X Leopard

Published on 14/09/08
by Colin Loretz

I spent a few hours going through these steps with a friend of mine, Glenn, who is starting to learn Ruby on Rails. He wanted to install MySQL so we decided to do it from source. Glenn was trying to install MySQL using Dan Benjamin’s tutorial at Hivelogic. I found the same steps that Dan posted on a few other websites but the remote source code his example tries to download no longer exists. This process does not take a few hours but we ran into a few issues trying to find a working copy of the version we used: mysql-5.0.45.tar.gz.

This is a quick post, which I hope may help anyone else trying to install MySQL, but I have not gone in depth on any of the steps. If you have any issues or questions, please feel free to comment below.

Disable any existing installs of MySQL:
sudo rm /usr/local/mysql

Remove the startup item:
sudo rm -rf /Library/StartupItems/MySQLCOM/

Setup your path by editing your .bash_login file, I’ll be using Textmate:
mate ~/.bash_login

Add to the end of the file:
export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH"

Save the file.

Create a directory called src in your home folder.
mkdir src
cd src

To download the mysql source:
curl -O http://mirror.provenscaling.com/mysql/community/source/5.0/mysql-5.0.45.tar.gz

Extrace the code from the gzip and enter the directory:
tar xzvf mysql-5.0.45.tar.gz
cd mysql-5.0.45

You now need to configure MySQL, paste the following into Terminal and press enter:
CC=gcc CFLAGS="-O3 -fno-omit-frame-pointer" CXX=gcc \
CXXFLAGS="-O3 -fno-omit-frame-pointer -felide-constructors \
-fno-exceptions -fno-rtti" \
./configure --prefix=/usr/local/mysql \
--with-extra-charsets=complex --enable-thread-safe-client \
--enable-local-infile --enable-shared

Now we will compile the code:
make

And install the code:
sudo make install

The last two steps may take a while.

Once all of this is complete, it’s time to setup the initial database user and privileges.
cd /usr/local/mysql
sudo ./bin/mysql_install_db --user=mysql
sudo chown -R mysql ./var

Now MySQL should be installed. In order to start the initial MySQL and have it start every time you restart your machine, do the following:

Create a file in /Library/LaunchDaemons named com.mysql.mysqld.plist
Paste the following XML into the new file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>KeepAlive</key>
    <true/>
    <key>Label</key>
    <string>com.mysql.mysqld</string>
    <key>Program</key>
    <string>/usr/local/mysql/bin/mysqld_safe</string>
    <key>RunAtLoad</key>
    <true/>
    <key>UserName</key>
    <string>mysql</string>
    <key>WorkingDirectory</key>
    <string>/usr/local/mysql</string>
</dict>
</plist>

While still in the LaunchDaemons folder, enter:
sudo chown root /com.mysql.mysqld.plist
sudo chown root com.mysql.mysqld.plist 

Return to the top directory and enter:
sudo launchctl load -w /Library/LaunchDaemons/com.mysql.mysqld.plist

MySQL should now be running. To enter MySQL, enter in Terminal:
mysql -u root

You should see the following:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.45 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>

Congratulations, you’ve installed MySQL from binary source!

If you do not see the MySQL monitor, make sure you followed all the steps above. When I did this for the first time, it didn’t work so I disabled the install and started over from the top and it worked fine the second time around.

That's it. What Next?

Please leave your comment so we know what you think about this article. Trackback URL: Installing MySQL from source on Mac OS X Leopard.