How to install MySQL on Lion (Mac OS X )

If you want to install MySQL on Lion it’s pretty easy, just head on over and download the DMG from (for lion you’ll need the 64bit DMG Archive).

Then double click on mysql package to install it, then on MySQLStartupitem.pkg, and then on the MySQL.prefPane. Once that’s done start it up via system prefs.

Finally, you will need to add ‘/usr/local/mysql/bin’ to your path environment. To do this, open up terminal and type in:

nano .bash_profile

Or if you have textmate you can use:

mate .bash_profile

Then just update the export PATH if there’s one already there, my file looks like this:

export PATH="$HOME/.rbenv/bin:/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH"
eval "$(rbenv init -)"

PS1="[\W:]$ "
alias ll="ls -lahG"
alias irbs="irb --simple-prompt"
alias rake="bundle exec rake"

Then save it. Rbenv already created the PATH so I just updated it. The PS1 line tells terminal to show the directory you are currently in as the prompt (you can always use ‘pwd’ to get the full path to working directory if you forget where you are). I’ve also added the alias to ll, which shows you all your files in the directory (including hidden files) with permissions, when you type in ll in terminal. The alias, irbs, will give you irb –simple-prompt, and the last one for rake, will use bundler.

If you want your terminal prompt to be in red, use the following instead:

PS1="\e[0;31m[\W]\$ \e[m"

Close the terminal window and restart it for changes to take effect, and now you should be able to type ‘mysql’ and get to MySQL.

Set root password for MySQL

It’s good practise to set your root password, here’s how:

mysql -u root
SET PASSWORD FOR root@localhost=PASSWORD('your_password_here');

Install MySQL gem for Ruby

If you’re using Ruby, simply open a terminal window and:

sudo gem install mysql2

Enter your user password (not your mysql one) when asked.

If you get an ‘Library not loaded: libmysqlclient.18.dylib’ when doing your first rake db:migrate, you will need to add this to your .bash_profile

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH

(Then simply close and open a new terminal window)

If that doesn’t work try running this in your terminal:

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

All done 🙂

This is what my .bash_profile looks like btw, I’ll probably keep it updated in this post given I mention it above:

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH
export PATH="$HOME/.rbenv/bin:/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH"
eval "$(rbenv init -)"

PS1="[\W]\$ "
alias ll="ls -lahG"
alias irbs="irb --simple-prompt"
alias rake="bundle exec rake"
alias bec="bundle exec cucumber"
alias ber="bundle exec spec"
alias pow="touch tmp/restart.txt"

Why not give PostgreSQL a go?

Since writing this guide I have moved to Postgres – it is a much better relational database and includes many features such as a nosql option, queueing, transactions plus lots more. It is also very fast and very stable – and now seems to be the database of choice for Rails developers. You can install easily by brew install postgresql (you can install it in addition to MySQL btw – so you don’t have to choose one or the other).