The eclectic musings of a bitter software engineer.

Here’s how to get the latest version of MySQL (5.0.51b as of this writing) working with the MySQL/Ruby gem and the Apple-supported version of Ruby that comes with Mac OS X Leopard. These instructions are scattered around the Internets in various places, but I’m sick of searching for them every time I need them so I’m compiling them here in a single place for convenience.

First, download and install MySQL for Mac OS X 10.5 (x86). Don’t install the x86_64 build or Ruby will refuse to speak to it. If you’ve already installed the x86_64 build, backup your databases, install the x86 build on top of it, and restore your databases.

Once you’ve got the correct build of MySQL installed, pop open a terminal and run the following to install the MySQL/Ruby gem:

sudo env ARCHFLAGS="-arch i386" gem install mysql -- \
  --with-mysql-dir=/usr/local/mysql --with-mysql-lib=/usr/local/mysql/lib \
  --with-mysql-include=/usr/local/mysql/include

That should do the trick.

Comments

I’ve been looking everywhere for this. Thanks a bunch.

Wednesday August 06, 2008 @ 09:39 AM (PDT) Posted by DM

Hi,

I found your page through google and installed the ruby mysql gem on my mac with your line above. Thanks. After installing, I played around with it in irb on a mysql database that I have and it worked find. However when I tried to run a ruby script with it, the “require ‘mysql’” line fails and it doesn’t work:

./msql.rb:4:in `require’: no such file to load — mysql (LoadError)
from ./msql.rb:4

So, when I go back to irb and try to see what happened, irb works fine:

%irb
>> require ‘mysql’
=> true
>>

I’ve tried for a while to track this problem down (maybe it’s a path problem somewhere?)
But, haven’t found anything yet. Google doesn’t seem to be very helpful for this either.
Do you have any pointers? I’m assuming everything works for you. BTW, I’m running
10.5.4 on a 32-bit macbook.

Thanks for any help you can give.

-Jason

The test script from error above was:
#!/usr/bin/ruby

#puts `env`
require ‘mysql’

Tuesday September 02, 2008 @ 03:12 PM (PDT) Posted by Jason

Try requiring RubyGems first (this is necessary since the mysql lib is a gem):

#!/usr/bin/ruby

require 'rubygems'
require 'mysql'
Tuesday September 02, 2008 @ 03:51 PM (PDT) Posted by Ryan Grove

I was just looking back at your page since I got the script to work doing:
require ‘/usr/lib/ruby/user-gems/1.8/gems/mysql-2.7/mysql’

But, your require ‘rubygems’ is much better. I haven’t used gems that much… as
you can probably tell.

Thanks,
-Jason

Tuesday September 02, 2008 @ 03:58 PM (PDT) Posted by Jason

Jason,

Same problem for me. If I don’t specify the absolute path I got the following:

NameError: uninitialized constant Mysql

Changing it to: require ‘/usr/lib/ruby/user-gems/1.8/gems/mysql-2.7/mysql’

Made everything work.

Friday September 12, 2008 @ 04:48 AM (PDT) Posted by Travis Bell

I’m getting the following error when trying to install – any ideas?

Macintosh-2:myequity greg$ sudo env ARCHFLAGS=“-arch i386” gem install mysql — —with-mysql-dir=/usr/local/mysql —with-mysql-lib=/usr/local/mysql/lib —with-mysql-include=/usr/local/mysql/include -V

WARNING: RubyGems 1.2+ index not found for:

RubyGems will revert to legacy indexes degrading performance.
Updating metadata for 69 gems from http://gems.rubyforge.org/
……………………………………………………………
complete
Building native extensions. This could take a while…
ERROR: Error installing mysql:
ERROR: Failed to build gem native extension.

/opt/local/bin/ruby extconf.rb install mysql — —with-mysql-dir=/usr/local/mysql —with-mysql-lib=/usr/local/mysql/lib —with-mysql-include=/usr/local/mysql/include -V
checking for mysql_query() in -lmysqlclient… no
checking for main() in -lm… yes
checking for mysql_query() in -lmysqlclient… no
checking for main() in -lz… yes
checking for mysql_query() in -lmysqlclient… yes
checking for mysql_ssl_set()… yes
checking for mysql.h… yes
creating Makefile

make
/usr/bin/gcc-4.0 -I. -I. -I/opt/local/lib/ruby/1.8/i686-darwin9.3.0 -I. -DHAVE_MYSQL_SSL_SET -DHAVE_MYSQL_H -I/usr/local/mysql/include -I/usr/local/mysql/include -I/opt/local/include -fno-common -O2 -fno-common -pipe -fno-common -c mysql.c
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: syntax error before numeric constant
mysql.c:2018: error: syntax error before numeric constant
make: * [mysql.o] Error 1

Gem files will remain installed in /opt/local/lib/ruby/gems/1.8/gems/mysql-2.7 for inspection.
Results logged to /opt/local/lib/ruby/gems/1.8/gems/mysql-2.7/gem_make.out
Macintosh-2:myequity greg$

Sunday September 14, 2008 @ 01:18 PM (PDT) Posted by Greg H

Thanks. I’d been ignoring the problem and using the default Ruby-based MySQL drivers until I saw they were going to be removed from Rails 2.2, this worked.

Monday September 22, 2008 @ 09:33 PM (PDT) Posted by Marcus

Thanks for posting this!

Saturday October 11, 2008 @ 11:47 AM (PDT) Posted by Jimmy
Post a comment

Basic XHTML (including links) is allowed, just don't try anything fishy. Your comment will be auto-formatted unless you use your own <p> tags for formatting. You're also welcome to use Textile.

Don't type anything here unless you're an evil robot:


And especially don't type anything here:

Copyright © 2002-2008 Ryan Grove. All rights reserved.
Powered by Thoth.