Warning: I consider this tutorial outdated. Please switch to a better system like brew, rvm and the mysql2 gem.
This is the source approach to get MySQL and MySQL Ruby gem working on Snow Leopard.
I assume you don’t have any previous MySQL installations lying around, this guide covers the installation from scratch.
If you want to install your ruby gems in your self-contained home directory without using sudo, check the end of the article.
Install required software
Let’s start by getting into a directory, I’ll use ~/src.
By convention you may want to use /SourceCache aswell.
Download the latest MySQL source distribution and install Xcode from Snow Leopard DVD.
While you’re downloading MySQL and installing Xcode you need to set your PATH environment variable correctly.
PATH Setup
Open a Terminal and verify what’s your current PATH by doing this:
This will return a string containing the current search paths, you have to make sure that’s containing /usr/local/bin, /usr/local/sbin and /usr/local/mysql/bin.
Don’t rely on your terminal application to set those paths; instead go ahead and edit/create the file .bash_profile (file that starts with a dot are hidden) in your home folder (~).
As a Textmate user I’ll use the mate command to fire up my editor.
Inside this file, write:
PATH="/usr/local/bin:/usr/local/sbin:$PATH" # if not already present
PATH="$PATH:/usr/local/mysql/bin"
export PATH=$PATH
With this in place we are sure that we will find MySQL required files later.
If you don’t want to reopen the terminal or switch to another tab, just execute:
This will pickup changes to $PATH.
MySQL installation
Decompress the MySQL source:
tar xzvf mysql-5.1.40.tar.gz && cd mysql-5.1.40
Of course match the version number you downloaded, the latest as of this writing is 5.1.40.
Configure MySQL this way (one line):
CC=gcc CFLAGS="-arch x86_64 -O3 -fno-omit-frame-pointer" CXX=gcc CXXFLAGS="-arch x86_64 -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 --with-plugins=innobase
Compile MySQL and install:
make && sudo make install
We are not done with MySQL yet, we need to install default tables and set up permissions:
cd /usr/local/mysql
sudo ./bin/mysql_install_db --user=mysql
sudo chown -R mysql ./var
We now have to auto start MySQL upon booting.
Create a file named
and add the following code in it:
<?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>
or see the script on github (raw).
Next move this file in the proper place with sudo and assign correct permissions:
sudo mv ~/src/com.mysql.mysqld.plist /Library/LaunchDaemons
sudo chown root /Library/LaunchDaemons/com.mysql.mysqld.plist
Start up MySQL now with:
sudo launchctl load -w /Library/LaunchDaemons/com.mysql.mysqld.plist
To stop MySQL manually do:
sudo launchctl unload -w /Library/LaunchDaemons/com.mysql.mysqld.plist
MySQL should be running (unless you stopped it, eh), it’s time to change our MySQL root’s password:
mysqladmin -u root password "mypassword"
Replace mypassword with your password.
Test MySQL:
mysqladmin -u root -p version
Write your password when asked and you should see some useful data.
Time to install the ruby mysql bindings gem.
MySQL Ruby gem setup
Please see the section below for a note about sudo.
First, update rubygems.
If this doesn’t work for some reason or you get “Nothing to update” try this instead:
sudo gem install rubygems-update
sudo update_rubygems
Time to build our gem, write:
sudo env ARCHFLAGS="-arch x86_64" gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
And we are done, thanks for reading.
Extras
Not always is desiderable to install gems system-wide, so add this to your .bash_profile
export GEM_PATH="$HOME/.gem/ruby/1.8"
export GEM_HOME="$HOME/.gem/ruby/1.8"
PATH="$HOME/.gem/ruby/1.8/bin:$PATH"
export PATH=$PATH
This will self-contain gem files and binaries in your home directory and you can install gems without using sudo.
Bonus
My entire .bash_profile:
export GIT_EDITOR="/usr/bin/mate -w"
export EDITOR="/usr/bin/mate -w"
export RAILS="$HOME/src/rails"
export GEM_PATH="$HOME/.gem/ruby/1.8"
export GEM_HOME="$HOME/.gem/ruby/1.8"
# CAPP_BUILD, directory for built Cappuccino framework.
export CAPP_BUILD="$HOME/src/cappuccino_framework"
# Cappuccino jake branch.
PATH="$CAPP_BUILD/Release/CommonJS/objective-j/bin:$PATH"
PATH="$HOME/src/narwhal/bin:$PATH"
# Normal stuff.
PATH="$PATH:/opt/local/sbin"
PATH="$PATH:/opt/local/bin"
PATH="$PATH:/usr/local/mysql/bin"
PATH="$HOME/.gem/ruby/1.8/bin:$PATH"
# Finalize PATH
export PATH=$PATH
function parse_git_branch {
ref=$(git symbolic-ref HEAD 2> /dev/null) || return
echo "("${ref#refs/heads/}")"
}
RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
PS1="$RED\$(date +%H:%M) \w$YELLOW \$(parse_git_branch)$GREEN\$ "