September 1, 2007

Installing Ruby with MySQL Support on Mac OS X

Karen came to me today asking how she could get Ruby to connect to MySQL, because she was having a lot of trouble following some instructions she had found online for compiling and installing the MySQL library herself, for her research work at Stanford. I thought it would be easier to help her install the Ruby MySQL library on her Mac instead, and then connect to the Stanford database remotely. She is not working on a web application, and does not want all of the additional stuff associated with Rails, so I needed something that would be very simple for her to install herself, with instructions from me over IM.

I found the easiest combination was the following:

First Karen installed the three items included in the MySQL Mac OS X package: the server, preference pane, and startup support. She doesn't need to run the server, but installing it creates the /usr/local/mysql directory with all of the client-side drivers. The preference pane and startup support are just there to make things easier for her to work with.

Next, she installed the One-Click Installer. Mac OS X comes with Ruby already, but only the base interpreter. I couldn't find gem anywhere, and the One-Click Installer includes a bunch of stuff including RubyGems. So now Karen has a more up-to-date version of Ruby installed in /usr/local.

At this point, Ruby 1.8.6 is installed with RubyGems 0.9.4. Typing which ruby in Terminal showed that the /usr/local/bin/ruby executable was already being picked up instead of the one that came with Mac OS X. Now we needed to install the MySQL libraries so she could write scripts to interact with her database. This is done using RubyGems at /usr/local/bin/gem.

karen$ sudo /usr/local/bin/gem install mysql -- --with-mysql-dir=/usr/local/mysql

The first time, this returned an error about not being able to find the mysql gem. However, running the same command a second time it worked just fine and asked which version of the mysql gem to install.

Select which gem to install for your platform
1. mysql 2.7.3 (mswin32)
2. mysql 2.7.1 (mswin32)
3. mysql 2.7 (ruby)
4. mysql 2.6 (ruby)
5. Skip this gem
6. Cancel installation

Karen picked #4, and after a short while it reported success. However, all is not well yet because there is a bug in the mysql.bundle file that was created by the installation. It references the MySQL dynamic library as /usr/local/mysql/lib/mysql/libmysqlclient.15.dylib when the correct path is /usr/local/mysql/lib/libmysqlclient.15.dylib. Note the extra mysql in the incorrect path.

To fix this, you need to run the install_name_tool which can be used to change the name of dynamic libraries in a Mach-O binary. As it turns out, there will actually be two mysql.bundle files created as a result of the gem installation. I believe /usr/local/lib/ruby/gems/1.8/gems/mysql-2.7/mysql.bundle may be a temporary one, and /usr/local/lib/ruby/gems/1.8/gems/mysql-2.7/lib/mysql.bundle (inside the lib directory) may be the one used during execution. However, just to be safe, since the current working directory appears to be used when Ruby is looking for libraries, I had Karen change the reference in both files.

sudo install_name_tool -change /usr/local/mysql/lib/mysql/libmysqlclient.15.dylib /usr/local/mysql/lib/libmysqlclient.15.dylib /usr/local/lib/ruby/gems/1.8/gems/mysql-2.7/mysql.bundle
sudo install_name_tool -change /usr/local/mysql/lib/mysql/libmysqlclient.15.dylib /usr/local/mysql/lib/libmysqlclient.15.dylib /usr/local/lib/ruby/gems/1.8/gems/mysql-2.7/lib/mysql.bundle

Now everything is done and you can use the mysql library from within your Ruby scripts. However, you need to require RubyGems first, otherwise it won't find the mysql.bundle file.


require "rubygems"
require "mysql"

Unfortunately, after doing all of this, Karen was unable to connect to her database because it does not accept remote connections. :(

Posted by josuah at September 1, 2007 12:45 AM UTC+00:00

Trackback Pings

TrackBack URL for this entry:


Post a comment

July 2013
Sun Mon Tue Wed Thu Fri Sat
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31