MySQL gem problem on OS X

Help.
On OS X 10.4.9

When trying to run a simple file starting with:
require ‘rubygems’
require ‘mysql’

I get this:
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
gem_original_require': no such file to load -- mysql (LoadError) from /usr/local/lib/ruby/site_ruby/1.8/rubygems/ custom_require.rb:27:inrequire’
from mysql_ruby.rb:3

But when I do a local:
gem list my

I do get this:

*** LOCAL GEMS ***

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

So I know it is there. Other gems seem fine. irb also says there is
no mysql gem.
MySQL is installed and running. With Rails it works.
What I am doing is not Rails, but is Ruby.
Can anybody help out?

John J.

On May 2, 2007, at 08:57 , John J. wrote:

    from /usr/local/lib/ruby/site_ruby/1.8/rubygems/ 

MySQL is installed and running. With Rails it works.
What I am doing is not Rails, but is Ruby.
Can anybody help out?

First, this is a good problem report. I’d like to see more people
writing the list with such good problem descriptions.

Has it ever worked? If not, then you probably ignored/missed an error
on gem install with it complaining about not being able to build.
There will be a log file in the gem’s dir stating what went wrong.
Chances are it is missing/unknown config/headers. Rebuild it by doing
gem install again. You’ll probably have to do something similar to:

sudo gem install mysql – --with-mysql-config=/opt/local/bin/
mysql_config5

On May 3, 2007, at 1:16 AM, Ryan D. wrote:

I get this:

error on gem install with it complaining about not being able to
build. There will be a log file in the gem’s dir stating what went
wrong. Chances are it is missing/unknown config/headers. Rebuild it
by doing gem install again. You’ll probably have to do something
similar to:

sudo gem install mysql – --with-mysql-config=/opt/local/bin/
mysql_config5

Thanks for the compliment. I’ve seen enough help requests over the
years on lists like this and in person where people are indeed in
need but haven’t thought about the right questions or presented all
the details. It’s not their fault usually, they need to be gently
educated on how to ask for help with problems. It’s like going to the
mechanic and saying “my car makes a strange noise”. So the mechanic
learns to teach everyone how to report problems. We have to do that too.

Anyway, I see from your response you are using MacPorts (a.k.a
DarwinPorts).
I’m not using that. (the convenience of package management, gem is no
exception, is also a problem when things do go wrong, because the
dependencies are hidden, so we don’t know what they are)

Yes, the mysql gem has worked. Rails with mysql depends on it. So it
works, or has worked before, or seemed to.
I stopped messing with Rails and decided to focus on getting a good
grounding in Ruby first since one of the big problems with the Rails
community is all the people who mistakenly believe Rails somehow
doesn’t require Ruby or other skills. (some Rails based apps are
getting closer to being consumer-friendly like many PHP apps are, but
not quite there yet, and developing a Rails app will never be a no-
Ruby-knowledge-needed situation.)

It’s been a while and I didn’t make notes at the time (my mistake)
but I seem to remember reinstalling this gem long ago.

Hmm… in the directory
/usr/local/lib/ruby/gems/1.8/gems/

There is indeed the gem as listed, and an html readme file!
/usr/local/lib/ruby/gems/1.8/gems/mysql-2.7/README.html

It isn’t the best documentation, but leads to some conclusions that I
do indeed need to re-install the gem or at least configure it.
The options for step 1 are a bit sparsely explained.
ANY advice on the options for extconf.rb would be greatly appreciated.

You guys are right!
It is a bad install.

I didn’t know Rails shipped with its own mysql drivers. That is an
interesting thing to know though.

I took a look at the mkmf log file and sure enough, it has as many
errors as the moon has craters! (that’s a lot of holes)
I had the same trouble with BlueCloth before.

I do wish gems would fail to install if they’re not properly install.
This is definitely a cause for many posts from Rails people.
On OS X at least, this may be less of a trouble with the next version
of the OS, since it should have much of these things installed in a
good way. (we’ll see …)

But I can blame mysql as well. It isn’t the most friendly install
either.

I just hope this doesn’t turn into one of those all day installs that
leads me learning more about mysql internals than I want to know…

On 5/2/07, John J. [email protected] wrote:

require ‘mysql’
gem list my
writing the list with such good problem descriptions.

I’m not using that. (the convenience of package management, gem is no
not quite there yet, and developing a Rails app will never be a no-
Ruby-knowledge-needed situation.)

It’s been a while and I didn’t make notes at the time (my mistake)
but I seem to remember reinstalling this gem long ago.

Rails works is no guarantee of the fact that, you have broken build
of mysql C extension. Its because rails ships with its own drivers,
which it will use, if it can’t find native mysql drivers, which are a
lot more faster.

Now as zenspider said, please try to do “gem install mysql” and see if
it gets installed properly. Because of various reasons I have seen,
when native C extensions fail to build using gem command, they still
get installed in WHATEVER_GEM_PATH. Of course, they are of little use
and usually as zenspider suggested mkmf.log, contains a log of build
errors.

May be, in future, a failed build shouldn’t install anything in
WHATEVER_GEM_PATH and do proper reporting of errors.

Hey, I want to say thanks for pointing me in the right direction!
Rather than completely start with new files, I new the stuff was
there, it just needed a little massaging.
Here is the account I kept of what I did to fix the problem. It’s not
the most graceful thing, but it’s not bad:

Part 1: Configuration You Never Knew You Had To Do

mysql gem manual setup for OS X 10.4.9 (probably any 10.4.x)

I’ve got Ruby 1.8.4 installed in /usr/local/bin
(I fixed the bad factory install long ago)

My mysql gem 2.7 is installed in the following directory:
/usr/local/lib/ruby/gems/1.8/gems/mysql-2.7

My MySQL is installed in:
/usr/local/mysql/

(actually in:
/usr/local
I have a symlink to the full name of the install to simplify the path
name. I got this idea from the way other software is often installed.
(such as the Entropy PHP distro))

You should navigate (in Terminal, using UNIX command cd) your way to
each of those directories in separate terminal windows. This should
keep you ready to check stuff. To open them as finder windows (much
easier to browse) just use OS X’s open command.
open /path/name/here
So, on my machine
open /usr/local/lib/ruby/gems/1.8/gems/mysql-2.7

The terminal window open to the mysql gem’s directory is where you
will type all of these commands.

extconf.rb has following options:

–with-mysql-include=dir
MySQL header file directory. Default is /usr/local/include.
Mine would be
/usr/local/mysql/include
So,
–with-mysql-include=/usr/local/mysql/include

–with-mysql-lib=dir
MySQL library directory. Default is /usr/local/lib.
Mine would be
/usr/local/mysql/lib
so,
–with-mysql-lib=/usr/local/mysql/lib

–with-mysql-dir=dir
Same as --with-mysql-include=dir/include, --with-mysql-lib=dir/lib.
Mine would be
/usr/local/mysql
So, in theory, I could simply use only :
–with-mysql-dir=/usr/local/mysql

–with-mysql-config[=/path/to/mysql_config]
Get compile-parameter from mysql_config command.
Mine would be
/usr/local/mysql/bin/mysql_config
So,
–with-mysql-config=/usr/local/mysql/bin/mysql_config

Well then, let’s try this all together now, shall we? It’s long…
at the command line:
ruby extconf.rb --with-mysql-include=/usr/local/mysql/include --with-
mysql-lib=/usr/local/mysql/lib --with-mysql-dir=/usr/local/mysql –
with-mysql-config=/usr/local/mysql/bin/mysql_config

Oops. Error message:
checking for mysql_ssl_set()… /usr/local/lib/ruby/1.8/mkmf.rb:
174:in initialize': Permission denied - mkmf.log (Errno::EACCES) from /usr/local/lib/ruby/1.8/mkmf.rb:174:in open’
from /usr/local/lib/ruby/1.8/mkmf.rb:201:in postpone' from /usr/local/lib/ruby/1.8/mkmf.rb:541:in checking_for’
from /usr/local/lib/ruby/1.8/mkmf.rb:600:in `have_func’
from extconf.rb:20

What the hell does that mean?! Well, error messages in Ruby can be a
bit cryptic, but mainly because they’re so verbose. This just said
you don’t have the matching permissions with those of the file you’re
trying access.
So what to do?
Easy! Run the same command again, but start with sudo. (often
installing or configuring things at the command line in OS X requires
you to do sudo, remember that when simple commands that should work
fail.)
sudo ruby extconf.rb --with-mysql-include=/usr/local/mysql/include –
with-mysql-lib=/usr/local/mysql/lib --with-mysql-dir=/usr/local/mysql
–with-mysql-config=/usr/local/mysql/bin/mysql_config

Password:
(enter your password for your user account)

checking for mysql_ssl_set()… yes
checking for mysql.h… yes
creating Makefile

Done.
Now part 2: The Test.
The documentation says:
ruby ./test.rb [hostname [user [passwd [dbname [port [socket
[flag]]]]]]]
Ugly. That’s just too many brackets. Too many optional arguments that
are usually not optional to get anything done.
hmm… no file mysql.o
let’s skip the test and see what happens.

Part 3: Make
Now we run the make file and cross our dirty little fingers.
make
No. No dice.
sudo make
nope, same error. Luckily, I found this earlier and it made sense:
http://jlaine.net/2006/10/3/installing-ruby-mysql-driver-on-os-x
What they said was that you need to add the line
#define ulong unsigned long long
in the mysql.c file in the mysql gem’s directory. If you are
unfamiliar with C, just open the file in TextMate and scroll down to
the first instance of #define (you will see it near the top of the
file). Insert the line after the second #define statement and you
should be fine. While you’re there though, just to be safe and
secure, add a comment line telling when you added it and why.
Ok, save and close the mysql.c file. Now run make again.
sudo make
Waiting… make often takes time and don’t you dare expect to
understand half the gobbledy-gook it spits out. Make also tends to
have lengthy periods where there is no feedback on progress and all
you can do is wait and wonder. This one is a quickie though.
Ok, that was painless (I hope) now make install:
sudo make install
And that should have been super fast.
Now go play with Ruby and MySQL as you should.

CAVEAT EMPTOR: We didn’t successfully run the test.rb file. That’s
too bad. It was designed apparently for compiling the gem from source
the old-fashioned way. Or perhaps the unit test is out of date, who
knows. But it would be nice to know if everything passes! But
sometimes we must live dangerously and as we say in Texas:
If it ain’t broke, don’t fix it.