.If you do a search right now on google for “mysql gem mac” you will find 999 blogs about the solution to this (numero 1000 being this one), but unfortunately, NONE of these worked for me. Thanks a lot, Google! I thought you were smart!
Ok, ok, to be fair it kind of worked. The gem would install, but then I would get this error message when trying to start Rails:
Failed to lookup Init function /usr/local/lib/ruby/gems/1.8/gems/mysql-2.7/lib/mysql.bundle
Right. That’s when I decided to just hit the reset button and re-install MySQL.
Basically, I had MySQL5 running on Mac OSX 10.4.11 with ruby and rails and the works. Then I took off for a one-year-long sabbatical, came back, and now the world of Rails has changed a bit. Most glaringly in the beginning is that the MySQL libraries are no longer included in Rails since 2.2. This is probably a good thing as the MySQL libraries that were included were notoriously slow and it was always recommended to use the gem instead. I didn’t on my local machine before because since I am the only one using it, I could care less about performance. However, I’m sure there was someone out there (or many) who used the default MySQL libraries shipped with Rails in production or for some kind of performance test, complained about it, until finally the Rails core team decided to just rip it out. Anyway, good for Rails, bad for me.
To make a long story short, I spent hours trying all the tips out there to get the MySQL gem installed, including modifying the make file which I haven’t done since some absurd class I took in college. So, I did what any reasonable person would do: uninstall mysql and re-installing it with mac ports. For some history, MySQL before was installled from the binary package that can be downloaded from their site.
In any case, if you want to do this on your computer and keep any data in your database, you should use the mysqladmin tool to dump it out into a file for re-import later on. If not, your choice.
mysqldump -u root -p --all-databases > mysql-dump.sql
Anyway, to uninstall MySQL, I did the following in a terminal window (props to this post):
sudo rm /usr/local/mysql
sudo rm -rf /usr/local/mysql*
sudo rm -rf /Library/StartupItems/MySQLCOM
sudo rm -rf /Library/PreferencePanes/My*
edit /etc/hostconfig and remove the line MYSQLCOM=-YES-
sudo rm -rf /Library/Receipts/mysql*
sudo rm -rf /Library/Receipts/MySQL
Then I used macports to install MySQL5:
sudo port install mysql5 +server
That should take a little while. After it’s done, I created the initial MySQL databases. Notice that the command is mysql_install_db5 instead of mysql_install_db. This is a macports thing. All of the executables that I can tell have 5 appended to them (i.e. mysql5 instead of mysql). This shouldn’t be too much of a problem if you can remember it.
sudo -u mysql mysql_install_db5
If you want to start up the server now, you can and check that it’s running.
sudo /opt/local/lib/mysql5/bin/mysqld_safe &
ps aux | grep mysql
Or, if you’re lazy, like me, you can also have it start up when the computer starts:
sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist
Another lazy move is to symlink the socket file to where all programs look for it. I didn’t want to specify this every time, so I just symlinked it to where most apps look for it:
sudo ln -s /opt/local/var/run/mysql5/mysqld.sock /tmp/mysql.sock
And if you want to right now, go ahead and set a password for the root MySQL user.
mysqladmin5 -u root password [your_password]
If you backed up your data, now is a good time to restore it:
mysql5 -u root -p < mysql-dump.sql
Finally, installing the gem is a breeze:
sudo gem install mysql -- --with-mysql-config=/opt/local/bin/mysql_config5
Done! Woohoo!