Aston J

How to install MySQL on Lion (Mac OS X )

Posted on: December 2nd, 2011 by AstonJ 16 Comments

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).

Tags: , , ,

16 Responses

  1. Greg K says:

    Your instructions were correct and I got mysql up on my OS LION 1.2
    my only issue now is I was looking for and couldn’t find /etc/my.cnf

    • AstonJ says:

      Hi Greg

      The OS X install doesn’t create a my.cnf file – it just uses default values. If you would like to create your own you can:

      cd /usr/local/mysql/support-files/
      sudo cp my-huge.cnf /etc/my.cnf

      That will copy over a sample my.cnf file (to /etc/my.cnf) that you can edit.

  2. david says:

    I have installed mysql 5.5.20 for Lion but cannot get it to start from the preferences pane. Any suggestions??

  3. Jay says:

    Thanks Aston, great help!

  4. Cindy says:

    Aston you ROCK!

  5. Shail says:

    Thanks Aston. Your instructions guided me well.

  6. Morgan says:

    I’m trying to download myql community server on my macbook, the download is fine and when you click on it something loads, but then it disappears. there is a file in my downloads folder but I can’t find an installer anywhere. help!

  7. Thanks Dude! After I updated to Ruby 1.9.3p194 and RVM
    I was getting a “Library not loaded: libmysqlclient.18.dylib” error only when using It was fixed by using your symlink suggestion.

  8. Stephen McConnell says:

    Just a note. Lion comes with Postgresql already installed, so you don’t have to install it.

    I have installed MySQL on my laptop and have both, so I can switch back and forth depending upon the one I need.

  9. [...] (4) [...]

  10. Mahesh says:

    i m getting rbenv command not found error

Leave a Reply