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 mysql.com (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');
FLUSH PRIVILEGES;

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: , , ,
  • Greg K

    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

      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.

  • david

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

    • AstonJ

      What does it say David? Have you tried restarting the machine? Did you double click the pref pane installer? (MySQL.prefPane)

      • david

        I installed MySqil.prefPane. The prefpane exists in my system preferences. After attempting to start the server (even after a restart) it asks for password then does not start. Prefpane “mySQL server instance is stopped” and remains stopped. I had the same problem with OSX 10.6.8 and upgraded to Lion in attempt to resolve this. I tried this fix but the basedir and datadir are correctly defined.

        http://forums.mysql.com/read.php?11,399397,399606#msg-399606
        Any help is greatly appreciated. Thanks.

        • murat

          I have exactly the same problem with Lion. I tried almost everything on the web about this issue. Even noone points the problem this much clearly.

          I have tried to install 5.5.21 DMG, even the beta 5.6.x; nothing works. You can’t start the database from preferences pane. It remains stuck as “stopped” even after clicking the start button and entering the admin password, and then the computer thinking for a little while…

          Any comment on this is highly appreciated…

  • Jay

    Thanks Aston, great help!

  • Cindy

    Aston you ROCK!

  • Shail

    Thanks Aston. Your instructions guided me well.

  • Morgan

    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!

  • http://www.jonathanspooner.com Jonathan Spooner

    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 pow.cx. It was fixed by using your symlink suggestion.

  • Stephen McConnell

    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.

  • Pingback: Going from MacPorts to Homebrew

  • Mahesh

    i m getting rbenv command not found error