Ruby DBI

2012 April 23 at 11:01 » Tagged as :ruby, mysql, dbi,

This is another left over post from my learn ruby project. Ran into trouble while learning how to use databases with Ruby.

test.rb:1:in `require': no such file to load -- dbi (LoadError) from test.rb:1

  Tried to do the obvious thing, which is yum install ruby-dbi without any luck. Next tried gem install dbi still the problem persisted. Then I learnt at stackoverflow that you need to add require 'rubygems' before the require 'dbi' in the code, and sure enough the error dissappears only to be replaced by something else.

/usr/lib/ruby/gems/1.8/gems/dbi-0.4.5/lib/dbi.rb:300:in `load_driver': Unable to load driver 'Mysql' (underlying error: uninitialized constant DBI::DBD::Mysql) (DBI::InterfaceError) from /usr/lib/ruby.8/monitor.rb:242:in `synchronize' from /usr/lib/ruby/gems/1.8/gems/dbi-0.4.5/lib/dbi.rb:242:in `load_driver' from /usr/lib/ruby/gems/1.8/gems/dbi-0.4.5/lib/dbi.rb:160:in `_get_full_driver' from /usr/lib/ruby/gems/1.8/gems/dbi-0.4.5/lib/dbi.rb:145:in `connect'

  Then I tried gem install dbd-mysql and got an even more serious looking error message

 *** 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 Gem files will remain installed in /usr/lib/ruby/gems/1.8/gems/mysql-2.8.1 for inspection. Results logged to /usr/lib/ruby/gems/1.8/gems/mysql-2.8.1/ext/mysql_api/gem_make.out

  As explained here and more sweetly here, you need to use the --with-mysql-config option and give the path to your mysql_config file in my case it turns out to be /usr/bin/mysql_config . So we have

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

  This time the installation did complete successfully but it produced a set of warnings like the ones below, which I am going to ignore for now.

No definition for next_result

No definition for field_name

No definition for field_table

No definition for field_def

however the code still fails to execute.

/usr/lib/ruby/gems/1.8/gems/deprecated-2.0.1/lib/deprecated.rb:199: warning: already initialized constant Deprecate /usr/lib/ruby/gems/1.8/gems/dbi-0.4.5/lib/dbi.rb:318:in `load_driver': Could not load driver (uninitialized constant MysqlError) (DBI::InterfaceError) from /usr/lib/ruby/gems/1.8/gems/dbi-0.4.5/lib/dbi.rb:160:in `_get_full_driver' from /usr/lib/ruby/gems/1.8/gems/dbi-0.4.5/lib/dbi.rb:145:in `connect' from test.rb:3

It turns out that the above 'no definition' stuff are not just warnings but errors! I should have paid better attention to the article on the mysql site rather than taking the stackoverflow short cut. You need to run the gem install with the --no-rdoc --no-ri flags and then the mysql gem will be installed successfully. But before you do that you need to uninstall both mysql and dbd-mysql gems. So here is how it works.

gem uninstall dbd-mysql

gem uinstall mysql

gem install mysql --no-rdoc --no-ri -- --with-mysql-config=/usr/bin/mysql_config