MySQL Bindings for Ruby under Mac OS X Tiger

Update: These instructions are not for Mac OS X Server — it comes with it’s own version of MySQL installed, and thus the procedure below probably won’t work without some modifications (then again, if you’re running Server, you’re probably skilled enough to figure out what to change yourself). Apologies for anyone on Server who has tried to follow these instructions.

OK, after yesterdays mammoth post this one should be a bit quicker. Basically, there are a few issues with the building of native gems under Mac OS X at the moment, and until they’re dealt with up-​stream, getting certain rubygems to install is going to be trial and error.

1. Download and install MySQL

I’ll leave you to figure this out — I use the latest 5.x release myself, but version 4.x will work just as well. You can get the disk images from MySQL​.com. There is also a launch daemon written by Jacob Stetser, which you can grab from his blog (I would recommend using this over the MySQL​.com bundled startup item).

2. Un-​install any MySQL rubygems you may already have installed

We need to remove any earlier – possibly failed – attempts at installing the binding:

Powerbook:~ tony$ sudo gem uninstall mysql

If all goes well, you’ll now be ruby-​mysql-​less.

3. Download and install the latest MySQL bindings

For some reason, people have been seeing dynamic linker issues with the latest released version of the mysql bindings, but Courtenay over at http://​habtm​.com/ found that using the version 2.7 bindings resolves this issue

So go ahead and download http://​tmtm​.org/​d​o​w​n​l​o​a​d​s​/​m​y​s​q​l​/​r​u​b​y​/​m​y​s​q​l​-​r​u​b​y​-​2​.​7​.​t​ar.gz, I’ll wait. When you’re done, extract the archive and switch back to the terminal and cd to where you’ve extracted the source. The parts you need to type are in bold below.

Powerbook:~/mysql-ruby-2.7 tony$ sudo gcc_select 4.0
Powerbook:~/mysql-ruby-2.7 tony$ export PATH=/usr/local/mysql/bin:$PATH
Powerbook:~/mysql-ruby-2.7 tony$ ruby extconf.rb --with-mysql-config
checking for mysql_ssl_set()… yes
checking for mysql.h… yes
creating Makefile
Powerbook:~/mysql-ruby-2.7 tony$ make
gcc -fno-common  -arch ppc -g -Os -pipe -fno-common  -arch ppc -pipe -pipe -fno-common  -I. -I/usr/lib/ruby/1.8/powerpc-darwin8.0 -I/usr/lib/ruby/1.8/powerpc-darwin8.0 -I. -DHAVE_MYSQL_SSL_SET -DHAVE_MYSQL_H  -I/usr/local/mysql/include -Os -arch ppc -c mysql.c
 cc -dynamic -bundle -undefined suppress -flat_namespace  -L"/usr/lib" -o mysql.bundle mysql.o  -L/usr/local/mysql/lib -lmysqlclient -lz -lm  -lpthread -ldl -lobjc
Powerbook:~/mysql-ruby-2.7 tony$ sudo make install
install -c -p -m 0755 mysql.bundle /usr/lib/ruby/site_ruby/1.8/powerpc-darwin8.0

Guess what? If everything proceeded as above, you’re done! Next time, I’ll post some information about actually using the mysql bindings (even given my preference for sqlite). For now, the Ruby on Rails wiki has some great tutorials and starting points, as well as information about contacting other rails developers. Getting in contact with other Rails developers is something I really recommend you do, as the Rails community has some great minds you can learn from, and some friendly people who can help you when you get stuck. Good luck!

Comments

Generic user icon.

Thanks for this. I actu­ally googled for part the error I was receiv­ing. The full error was:

dyld: NSLinkMod­ule() error dyld: Sym­bol not found: _sprintf$LDBLStub Ref­er­enced from: /usr/lib/ruby/site_ruby/1.8/powerpc-darwin8.0/mysql.bundle Expected in: flat namespace

Get­ting the beta mysql bind­ing and com­pil­ing with gcc 4 instead of 3 solved the prob­lem as you outlined.

Posted by Noah on

Generic user icon.

Same prob­lem as above, didn't got lucky with google but con­tacted the author. Very clear fix, congrats.

Posted by Stephane on

Generic user icon.

This (or any other solu­tion that I can find) doesn't seem to work for tiger server (10.4.2) — does any­one have any ideas for get­ting the mysql bind­ings work­ing on server?

Posted by Mal­colm on

Gravatar for Tony.

Hi Mal­colm, Tiger Server comes with a copy of MySQL installed by default, so I think it's prob­a­bly a case of fig­ur­ing out where it's installed, and adding it's bin direc­tory to your PATH before you try con­fig­ur­ing and installing the gem. Happy to walk you through it if you want to send me an e-​mail (see the side bar).

Posted by Tony on

Generic user icon.

Thanks for the tips, you saved me a lot of time.

Posted by Dan on

Generic user icon.

I get a fail­ure when run­ning make. mysql.c:71: error: parse error before “MYSQL_​STMT”. This is fol­lowed by many more mes­sages, prob­a­bly caused by this first error.
Rails was setup as described in your other blog post. I have MySql 4.1 run­ning.
Any ideas?

Posted by Erik on

Generic user icon.

which sql edi­tors are you guys using?

I'm using cocoamysql, but am expe­ri­enc­ing a few prob­lems with it.

Posted by ramin on

Gravatar for Tony.

I use the offi­cial MySQL Query Browser. it's pretty smooth :)

Posted by Tony on

Generic user icon.

i might check that out. the only prob­lems I'm hav­ing with cocoamysql is when try­ing to insert a field that has a time­stamp type, it doesn't seem to want to take, keeps giv­ing me a mysql error regard­ing the default value (which i haven't set).

Posted by ramin on

Generic user icon.

I'm get­ting the same error as Eric (#6):

mysql.c:71: error: parse error before “MYSQL_​STMT

fol­lowed by a whole host of error messages.

Any­one got any ideas? Eric, did you man­age to sort it out?

Many thanks.

Posted by James on

Generic user icon.

Still get­ting

check­ing for mysql_​ssl_​set()… no
check­ing for mysql.h… no
check­ing for mysql/mysql.h… no

on 10.4.2 with 4.1.12 and gc set to 4

Posted by utter on

Gravatar for Ken Ander­son.

Just a quick com­ment to say that all of these instruc­tions worked just fine on my lap­top under 10.4.2.

Thanks Tony for the clear instruc­tions and excel­lent pointers!

Ken

Posted by Ken Ander­son on

Generic user icon.

I have a clean install of Tiger, from which I migrated my old sys­tem over using Apple's migra­tion tool. MySQL is installed at /​Library/​MySQL/​bin/​mysql and already work­ing and acces­si­ble for other apps.

The errors I get are (in part):

gcc –fno-​common –g –Os –pipe –fno-​common –I. –I/usr/lib/ruby/1.8/powerpc-darwin8.0 –I/usr/lib/ruby/1.8/powerpc-darwin8.0 –I. –DHAVE_​MYSQL_​SSL_​SETDHAVE_​MYSQL_​H –I”/Library/MySQL/include/mysql” –c mysql.c
mysql.c:71: error: parse error before “MYSQL_​STMT
mysql.c:71: warn­ing: no semi­colon at end of struct or union

Posted by Nick on

Generic user icon.

Notably, 2.7 is out of beta. I tried using that ver­sion per the above instruc­tions, but still received errors.

<http://​www​.tmtm​.org/​e​n​/​m​y​s​q​l​/​ruby/>

Posted by Nick on

Generic user icon.

For what it&apos;s worth, here&apos;s what I learned:

1) My clean install of Tiger, fol­lowed by the migra­tion, brought serverlogistics.com&apos;s 4.0.15 ver­sion of MySQL over. I needed to upgrade this, and chose 4.1.11.

2) it&apos;s pos­si­ble to gem install mysql by spec­i­fy­ing the cor­rect direc­tory of mySQL (which changed for me to /​usr/​local/​mysql for 4.1.11 from /​Library/​MySQL/​for the server­l­ogis­tics version.

3) There&apos;s a gem, fixr­b­con­fig, which assist in com­pil­ing the above.

Hope this helps some­one else migrat­ing to Tiger.

Posted by Nick on

Generic user icon.

Thank You! After bang­ing my head try­ing to get the mysql pack­age to com­pile the 2.7 beta finally worked. My Rails app will now con­nect to MySQL.

Thanks for the link.

Posted by Gerry Shaw on

Gravatar for Laura Her­ald.

i have the same prob­lem as erik above

error: parse error before "MYSQL_STMT" and then a lot of other errors

Posted by Laura Her­ald on

Generic user icon.

Thanks for this writeup. I wish your writeup was at the top of the list insted of the bottom.

Posted by Doug on

Generic user icon.

Tony, thankyou. I come from a design back­ground and I find your clear expla­na­tion extremely help­ful. I was about to throw my “Agile Web dev in rails” book right out the win­dow until I stum­bled onto your post clearly explain­ing how to install mysql-ruby-2.7.tar.gz

I&apos;m such a new­bie I didnt know I how to cd to the direc­tory until recently…

THANKS!,
Mike Moscow dot com

Posted by Mike Moscow on

Generic user icon.

Tony, thank you very much! I strug­gled with MySQL quite a bit on my Power­Book. Now, I am ready for some seri­ous Ruby munch­ing away at my data­base… Your instal­la­tion descrip­tion was really great, thanks again.

Posted by norb on

Gravatar for Iwan.

Hi!

I try install mysql-ruby-2.7 on my Mac G5 (MySQL mysql-standard-5.0.15-osx10.4-powerpc-64bit Mac OS10.4.2, Ruby 1.8.2) and got error

====

KrynkaCard:~/src/Ruby/mysql-ruby-2.7 big­mac$ ruby extconf.rb –with-​mysql-​config

check­ing for mysql_​ssl_​set()… no

check­ing for mysql.h… yes

cre­at­ing Makefile

KrynkaCard:~/src/Ruby/mysql-ruby-2.7 big­mac$ make

/​usr/​bin/​ld: trun­cated or mal­formed archive: /usr/local/mysql/lib/libmysqlclient.a (ran­lib struc­tures in table of con­tents extends past the end of the table of con­tents, can&apos;t load from it)

collect2: ld returned 1 exit status

make: ***| [mysql.bundle] Error 1

—| — |–

What&apos;s wrong?

Sorry, my eng­lish very poor

Thanks

Posted by Iwan on

Gravatar for Tony.

Hi Iwan — it looks like your copy of MySQL isn&apos;t right (one of the libraries is bro­ken). First try this:

ranlib /usr/local/mysql/lib/libmysqlclient.a Then try com­pil­ing again. If that doesn&apos;t work, you&apos;ll need to re-​download MySQL from MySQL​.com and install again.

Posted by Tony on

Gravatar for Devon.

This is the most help­ful Rails tuto­r­ial on the inter­net. Good work!

Posted by Devon on

Generic user icon.

Tony, you are a saint. thanks so much

Posted by Gregg Seib­ert on

Generic user icon.

To hope­fully save some­one some headache(s):

the MySQL 64bit ver­sion for Tiger (oth­ers?) will con­sis­tently fail with:

(ran­lib struc­tures in table of con­tents extends past the end of the table of con­tents, can&apos;t load from it)

Using the non-​64bit ver­sion works successfully.

Posted by josh­paul on

Gravatar for Iwan.

Hi!

Thanks Tony and joshpaul!

I exper­i­mented with MySQL — 4, 5 and 5-​64bit and only 5 work fine. Another ver­sion got error:

check­ing for mysqlsslset()… no

Posted by Iwan on

Generic user icon.

Ok. I&apos;m on Tiger Server, 10.4.3, run­ning MySQL 5.0.1164bit. I&apos;ve tried nearly every­thing thus far on this thread, but I&apos;m con­fused as to how to fix a few of the prob­lems, namely:

1.) The newly relo­cated “ruby.h” file in Xcode 2.2

2.) Issues in the make process after copy­ing ruby.h to the expected directory

Some instruc­tions for get­ting this up and run­ning on OS X Server would be very help­ful, I think. We&apos;re want­ing to use Rails for an upcom­ing project but may have to scratch that idea if we can&apos;t fig­ure this out. Thanks, in advance, for your help.

Posted by Brooks on

Generic user icon.

I have been try­ing to do this for awhile, but when­ever I try to extconf.rb I get the error “can&apos;t find header files for ruby.”. Every­one always says to install the Xcode devel­oper tools (includ­ing BSD sub­sys­tems) and I already had Xcode installed fully. I down­loaded a new ~800 meg dmg from Apple, rein­stalled Devel­oper TOols com­pletely, and .. got the same error message.

Any thoughts?

locate finds ruby.h in :

/usr/lib/ruby/1.8/universal-darwin8.0/ruby.h

Posted by Gary Burke on

Generic user icon.

extconf.rb runs fine, but then I get this when I try to make:

In file included from mysql.c:5:
/usr/lib/ruby/1.8/powerpc-darwin8.0/ruby.h:74:3: #error ----&gt;&gt; ruby requires sizeof(void*) == sizeof(long) to be compiled. &lt;&lt;----
In file included from /usr/lib/ruby/1.8/powerpc-darwin8.0/ruby.h:670,
                 from mysql.c:5:
/usr/lib/ruby/1.8/powerpc-darwin8.0/missing.h:19: error: redefinition of `struct timeval';
In file included from /usr/lib/ruby/1.8/powerpc-darwin8.0/missing.h:133,
                 from /usr/lib/ruby/1.8/powerpc-darwin8.0/ruby.h:670,
                 from mysql.c:5:
/usr/include/gcc/darwin/3.3/varargs.h:10:2: #error "GCC no longer implements &lt;varargs.h&gt;."
/usr/include/gcc/darwin/3.3/varargs.h:11:2: #error "Revise your code to use &lt;stdarg.h&gt;."
In file included from /usr/lib/ruby/1.8/powerpc-darwin8.0/ruby.h:670,
                 from mysql.c:5:
/usr/lib/ruby/1.8/powerpc-darwin8.0/missing.h:135: warning: conflicting types for built-in function `snprintf';
In file included from mysql.c:5:
/usr/lib/ruby/1.8/powerpc-darwin8.0/ruby.h:682: error: parse error before "rb_nativethread_t"
/usr/lib/ruby/1.8/powerpc-darwin8.0/ruby.h:682: warning: data definition has no type or storage class
make: *** [mysql.o] Error 1

Some expla­na­tion: the dif­fer­ence lies in /usr/lib/ruby/1.8 on my machine (g4 power­book, 10.4.3) in which one direc­tory is universal-darwin8.0 and the direc­tory it&apos;s look­ing for the head­ers in is powerpc-darwin8.0. So I put in sym­links from the wrong direc­tory to the right one, but now I get these errors. So obvi­ously sym­links are a bad idea. what should I do instead?

–Gary

Posted by Gary Burke on

Generic user icon.

Hi,

besides installing mysql locally, how do i use a remote mysql server for devel­op­ing?
i can con­nect to my db server for view­ing data (in radrails), but when test­ing the app locally with webrick, it tries to con­nect to local­host and gives an error.

Hen­drik

Posted by Hen­drik on

Generic user icon.

I was strug­gling to get RoR work­ing prop­erly with mySQL 5 — your installer and these mqsql bind­ings instruc­tions did the trick and got every­thing work­ing. Thanks.

Posted by Grant on

Gravatar for Bren­dan Park.

I get the fol­low­ing behaviour:

$ make
gcc -fno-common  -arch ppc -g -Os -pipe -fno-common  -arch ppc -pipe -pipe -fno-common  -I. -I/usr/lib/ruby/1.8/powerpc-darwin8.0 -I/usr/lib/ruby/1.8/powerpc-darwin8.0 -I. -DHAVE_MYSQL_H  -I/usr/local/mysql/include -Os -arch ppc64 -fno-common -c mysql.c
cc -dynamic -bundle -undefined suppress -flat_namespace  -L"/usr/lib" -o mysql.bundle mysql.o  -L/usr/local/mysql/lib -lmysqlclient -lz -lm  -lpthread -ldl -lobjc
/usr/bin/ld: truncated or malformed archive: /usr/local/mysql/lib/libmysqlclient.a (ranlib structures in table of contents extends past the end of the table of contents, can';t load from it)
collect2: ld returned 1 exit status
make: *** [mysql.bundle] Error 1

Where do i go from here — any help would be appreciated

Posted by Bren­dan Park on

Generic user icon.

I first had trou­ble run­ning the export com­mand, but did some Googling and found that using bash would prob­a­bly help. After typ­ing “bash” into the con­sole, I retried the com­mand with bet­ter suc­cess. Not sure why bash wasn&apos;t being used before, but just a help­ful tip if any­one else has prob­lems like this.

Posted by Matt James on

Generic user icon.

Tony — Thank you for post­ing these MySQL bind­ings instruc­tions. Very clear, very use­ful. They worked for me.

Posted by Chris Joakim on

Generic user icon.

For those of you who may be won­der­ing how to do this using Fink, doing so is not too difficult.

The path to the Ruby libraries as setup by the mkmf is invalid. It expects your header files to be in /sw/lib/ruby/1.8/powerpc-darwin instead of /sw/lib/ruby/1.8/*-darwin/, where they should actu­ally be.

How­ever, /sw/lib/ruby/1.8/powerpc-darwin is a valid path with impor­tant files in it, so your best bet with­out alter­ing code in mkmf.rb is to sim­ply cre­ate sim­links from the .h files in ../​–dar­win to ../​powerpc-​darwin.

After that, it should work fine, although maybe this is a sign I should give dar­win ports another shot…

Posted by Eon­flare on

Gravatar for San­thosh.

If your error is as follows:

I get the following behaviour:

$ make
gcc -fno-common  -arch ppc -g -Os -pipe -fno-common  -arch ppc -pipe -pipe -fno-common  -I. -I/usr/lib/ruby/1.8/powerpc-darwin8.0 -I/usr/lib/ruby/1.8/powerpc-darwin8.0 -I. -DHAVE_MYSQL_H  -I/usr/local/mysql/include -Os -arch ppc64 -fno-common -c mysql.c
cc -dynamic -bundle -undefined suppress -flat_namespace  -L"/usr/lib" -o mysql.bundle mysql.o  -L/usr/local/mysql/lib -lmysqlclient -lz -lm  -lpthread -ldl -lobjc
/usr/bin/ld: truncated or malformed archive: /usr/local/mysql/lib/libmysqlclient.a (ranlib structures in table of contents extends past the end of the table of contents, can't load from it)
collect2: ld returned 1 exit status
make: *** [mysql.bundle] Error 1

Can you try the fol­low­ing command:

sudo env ARCHFLAGS=’-arch i386’ gem install mysql –v=2.7 – –with-mysql-config=/usr/local/mysql/bin/mysql_config

Posted by San­thosh on

Sorry, this conversation has finished.

This post is a bit old now, so I've closed the conversation. If you're keen to keep talking about it, please email me directly.