Installing Ruby MySQL driver on OS X

Posted by # October 3rd 01:15 PM

Just noticed that I hadn’t installed the native Ruby mysql driver after getting the laptop back from repair. The reason it took a few days is that Rails just happily churns away with it’s built-in pure-ruby driver. However, the native binary driver is considerably faster and more stable, so you should never go into production without it. It won’t hurt having it installed on all your dev boxes, too.

However, installing the native driver on OS X wasn’t as easy as one would assume. If you have the MySQL binary OS X version from MySQL.com, it’s installed under /usr/local/mysql, and the gem can’t find it there. You can fix this by hinting the gem install command about the location of MySQL:

sudo gem install mysql -- --with-mysql-dir=/usr/local/mysql

Unfortunately, this resulted in errors:

Probutanol:~ jarkko$ sudo gem install mysql -- --with-mysql-dir=/usr/local/mysql/ 
Attempting local installation of 'mysql'
Local gem file not found: mysql*.gem
Attempting remote installation of 'mysql'
Select which gem to install for your platform (i686-darwin8.6.1)
 1. mysql 2.7.1 (mswin32)
 2. mysql 2.7 (ruby)
 3. mysql 2.6 (ruby)
 4. mysql 2.5.1 (ruby)
 5. Cancel installation
> 2
Building native extensions.  This could take a while...
mysql.c: In function âInit_mysqlâ:
mysql.c:2015: error: âulongâ undeclared (first use in this function)
mysql.c:2015: error: (Each undeclared identifier is reported only once
mysql.c:2015: error: for each function it appears in.)
mysql.c:2015: error: parse error before numeric constant
mysql.c:2018: error: parse error before numeric constant

Googling around helped, and Bob Silva came to the rescue: you can fix the errors by adding the following line to the mysql.c file of the mysql gem (/opt/local/lib/ruby/gems/1.8/gems/mysql-2.7/mysql.c for me):

#define ulong unsigned long

However, just adding it and re-running the gem install command doesn’t work. It turns out the mysql.c file is recreated by the extconf.rb configuration, so you need to do a bit of handiwork:

cd /opt/local/lib/ruby/gems/1.8/gems/mysql-2.7
sudo ruby extconf.rb install mysql -- --with-mysql-dir=/usr/local/mysql/

Now add the line above to the created mysql.c file, and after that compile and install the driver by hand:

sudo make
sudo make install

That’s it, the driver should be installed. You can check it by running the gem list command:

$ gem list

*** LOCAL GEMS ***

...

mysql (2.7)
    MySQL/Ruby provides the same functions for Ruby programs that the
    MySQL C API provides for C programs.

...

Note that you might have the native driver installed even if you don’t have the gem (e.g. via darwinports). You can check whether the library is installed in irb:

$ irb
irb(main):001:0> require 'mysql'
=> true

If it’s not installed, you will get something like this:

$ irb 
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'mysql'
LoadError: no such file to load -- mysql
        from /opt/local/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:21:in `require__'
        from /opt/local/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:21:in `require'
        from (irb):2

Jump to comment form

Comments

  1. Christian Romney 10.03.06 / 16PM

    Actually, I’ve had success with a simpler method:

    sudo gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
    
  2. Jarkko 10.05.06 / 23PM

    Christian,

    It doesn’t matter in this case, the problem arises with both ways. And mine is a bit shorter to write ;-)

  3. Mark Dillon 10.11.06 / 16PM

    You’re a lifesaver. Wasted a day trying to figure this out. Much appreciated!

  4. Paul Lambert 10.15.06 / 22PM

    Just add the line to the mysql.c.in file instead.

  5. Paul Lambert 10.15.06 / 22PM

    Oh, and it might be better to use

    #define ulong unsigned long long

    It looks like that’s correct on my Mac Pro, but it’s tricky—not sure what’s right.

  6. Jarkko 10.17.06 / 09AM

    Paul: Thanks, mysql.c.in is indeed a good idea :-)

    As for what to define ulong as, does it differ because your box runs 64-bit Xeons and we mere mortals have to do with “just” the 32-bit Core Duos?

  7. Joe 11.04.06 / 22PM

    Thank you very much for posting this fix! I have been using PStore for far too long since it was more stable than a db store for my sessions without the gem.

    So far I’m not getting anymore lost connections which is what happened all too frequently without the gem.

    Again, thanks!