Building MySQL on Mac OS X Leopard

Posted December 18th, 2008 by spanky

Hi and welcome back.  This blog is turning into Spanky’s Simple Steps for Starting Services!  See, I always spend all this time searching around for how to do things that I think should be really simple.  In the end, they usually are really simple, but nobody seems to document things in a way that makes sense, or the information is scattered all over the place and you have to piece it together.  Meh.  So, I hope you find these little tutorials helpful.

MySQL is Dolphin

MySQL is Dolphin

OK, so, MySQL is really easy to get running on OS X.  I’m using Leopard, but this should apply to any any version, AFAICT.  The first thing I tried was the binary installer.  I’m sure it works just fine, but when I tried to run it certain commands, I got errors so I wiped it and started from source.  I really prefer building things from source for understanding and control, although if I can have it done through a packaging system, I’m pretty happy with that too, because then you get updates and such free.  I’m sure I’ll write a post on Darwin Ports v MacPorts v source someday, but today is all about MySQL.

First, let me explain what my mission is.  I have to design a really simple database locally for a stop-gap application at work.  We’re migrating some other nightmarish MSSQL database to MySQL, but that could happen “any day” within the next 6 months.  I need to do this now.  So I’m going to build a super-simple database that I can connect to locally.  If you need a ‘real’ MySQL installation, like on a server, do not follow this guide.

Start by downloading the source. From this page click download the Community [Free] Server, and choose the ’source’ option (all the way at the bottom).  I pick the Compressed GNU TAR archive (tar.gz) version.

Before I continue, I owe most of this to this article from Apple, which has lots of good background and explaination, but it was missing a thing or two.  I recommend reading it.  It’s the #1 Google response for “MySQL on Mac OS X”, but like I said, it’s missing a few key things.

I’m going to assume you downloaded the tarball to your Desktop, so all my paths are going to go from there.  Open a Terminal and type:

cd ~/Desktop
tar -zxvf mysql-5.1.30.tar.gz
cd mysql-5.1.30
./configure --prefix=/usr/local/mysql \
--with-unix-socket-path=/usr/local/mysql/run/mysql_socket\
--with-mysqld-user=mysql \
--with-comment --with-debug
make
sudo make install
cd /usr/local/mysql
sudo bin/mysql_install_db --force
sudo mkdir run
sudo chgrp -R mysql /usr/local/mysql

sudo chown -R mysql run
var
sudo bin/mysqld_safe --user=mysql &

OK, so now for the missing steps, you want to add the MySQL bin to your PATH, if oyu know how to do this already, go ahead, or type:

echo 'export PATH=/usr/local/mysql/bin:$PATH' >> ~/.bash_profile
source ~/.bash_profile

And finally you’ll want to secure everything.  This handy provided script does everything you need:

mysql_secure_installation

And seriously, that’s it.  Now you have a MySQL server installed.  To run it, just type:

sudo echo
sudo /usr/local/mysql/bin/mysqld_safe --user=mysql &

(If you don’t do the ’sudo echo’ part first, and you’re not already sudo, you get this weird state where it wants you to type your password but it’s running in the background.)

And to stop it:

sudo mysqladmin -p -u root stop

I hope that this helps you get up and running quickly without having to piece together 5 different posts from forums and blogs.  If you like this article, click my ads!

~Spanky

Comments are closed.