problems with MySQL libraries while upgrading rails

I've just upgraded to Rails 2.3.2 on a RedHat Linux machine, but I had a lot of problems. Eventually I got there. I've seen other people posting questions about similar problems, but no answers so far, so here goes.

As specified in the instructions I did this:

gem install rails

No errors, but when I started my rails app, it fell over with error messages advising me to look at the mkmf.log for errors during the installation of the mysql connection package.

The next hurdle is to find the appropriate mkmf.log file. I had to use the UNIX find command to search the whole filestore:

    $ find / -name mkmf.log 2>/dev/null

This produced a long list of log files. Looking at the directory names I found one for mysql. Mine was called

/usr/local/lib/ruby/gems/1.8/gems/mysql-2.7/mkmf.log

It complained that mysql-devel package was missing from my MySQL installation. I think this is an extension to MySQL containing stuff for developers. Rails didn't use to use it, now it does. I fixed that problem by finding the appropriate RPM for my version of Linux and installing it.

Now the gem install threw another error, advising me to install the Rails MySQL connection software, but that led to further errors:

    # gem install mysql     Building native extensions. This could take a while...     ERROR: Error installing mysql:             ERROR: Failed to build gem native extension.

    /usr/local/bin/ruby extconf.rb     checking for mysql_query() in -lmysqlclient... no

    Could not create Makefile due to some reason, probably lack of     necessary libraries and/or headers. Check the mkmf.log file for more     details. You may need configuration options.

    Provided configuration options:           --with-opt-dir           --without-opt-dir

I've removed lots of detail from the above, in particular, it gives a long list of configuration options. As you will see, these are the key to the solution. You just need to know which option to supply and how to set it ....

Looking again at the mkmf.log, I could see that the errors were caused by failures when gem tried to run a makefile:

# more /usr/local/lib/ruby/gems/1.8/gems/mysql-2.7/mkmf.log find_library: checking for mysql_query() in -lmysqlclient... -------- no

"gcc -o conftest -I. -I/usr/local/lib/ruby/1.8/i686-linux -I. -I/usr/local/inclu de -g -O2 conftest.c -L'/usr/local/lib' -Wl,-R'/usr/local/lib' -L'/usr/local/l ib' -Wl,-R'/usr/local/lib' -lruby-static -lmysqlclient -ldl -lcrypt -lm -lc" /usr/bin/ld: cannot find -lmysqlclient

What's happening here is that gem is calling the standard UNIX make command. This is trying to find a library of C functions, and failing, because it's looking in the wrong place. This is basic C programming under UNIX stuff, nothing to do with Ruby. The solution is to get gems to call make and hand it the correct information. This is where the config options come in.

With a bit more searching on the web, I found this solution:

# gem install mysql -- --with-mysql-config=/usr/bin/mysql_config

(Note the extra "--". You need this to use the config options.)

What you are doing here is handing the name of a MySQL configuration file to gem. On your system, there should be a file called mysql-config, possibly in /usr/bin, possibly somewhere else. You need to find it and supply the right pathname. Basically, this file tells tools like gem where to find all the bits and pieces that make up your MySQL system.

This installed the rails MySQL component.

Finally, complete the rails upgrade:

# gem install rails