Bin-Blog logoBin-Blog

Learn about the latest in Web Development – as soon as I do.

Error Connecting to MySQL using Ruby on Rails in Linux – and its Solution

By Binny V A • Apr 3rd, 2009 • Category: Linux, MySQL, Ruby, Ruby on Rails

Ruby Logo

I am currently working on a new Rails project. Its been a while since I worked with rails – so I updated Rails using the command ‘sudo gem update --system‘. I started the inbuilt server and tried to open a page – and to my surprise, I found that Rails is having trouble connecting to the MySQL server. It was apparent that the mysql gem was missing. So I tried to install it using the command ‘sudo gem install mysql‘. That failed too – this is the error I got…

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

/usr/bin/ruby extconf.rb install mysql
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... no
checking for main() in -lsocket... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lnsl... yes
checking for mysql_query() in -lmysqlclient... no
*** extconf.rb failed ***
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
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=/usr/bin/ruby
        --with-mysql-config
        --without-mysql-config
        --with-mysql-dir
        --without-mysql-dir
        --with-mysql-include
        --without-mysql-include=${mysql-dir}/include
        --with-mysql-lib
        --without-mysql-lib=${mysql-dir}/lib
        --with-mysqlclientlib
        --without-mysqlclientlib
        --with-mlib
        --without-mlib
        --with-mysqlclientlib
        --without-mysqlclientlib
        --with-zlib
        --without-zlib
        --with-mysqlclientlib
        --without-mysqlclientlib
        --with-socketlib
        --without-socketlib
        --with-mysqlclientlib
        --without-mysqlclientlib
        --with-nsllib
        --without-nsllib
        --with-mysqlclientlib
        --without-mysqlclientlib

Gem files will remain installed in /usr/lib/ruby/gems/1.8/gems/mysql-2.7 for inspection.
Results logged to /usr/lib/ruby/gems/1.8/gems/mysql-2.7/gem_make.out

After a couple of hours on Google, I managed to fix the issue – and I am posting the solution here for others who might run into the same problem.

Install the necessary Libraries

First, you must install a couple of mysql libraries(I am assuming that you have MySQL server already installed). If you are on Red Hat/Fedora/CentOS etc., run the command…

sudo yum install ruby-mysql mysql-devel

If you are on Debian/Ubuntu/etc., run this command…

sudo apt-get install libmysqlclient15-dev libmysqlclient15off libmysql-ruby

Install the gem

Once the installation is done, enter this command to install mysql gem

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

If your mysql_config is not in the standard location, update the command with the right path.

The mysql gem should be installed now. These commands worked for me – your milage may vary. If you still have trouble, try one of the following links…

More Resources


3 Responses »

  1. sachin says:

    fine ,
    but iam getting problem with ruby script/server scaffold Item Manage
    command
    ragards

  2. Eddie says:

    Just diving into Rails and hit this exact same trouble. Knew I needed libraries but didn’t know which ones. THis worked like a charm.

    Thanks!

  3. adam says:

    omg thank you for this information.

    those deb packages had such obscure names!

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>