MySQL
Ruby DBI Module Installation and Setup
Introduction
This document describes the steps required on Solaris for connecting to MySQL database using Ruby DBI module
Prerequisites
For DBI scripts to access MySQL databases, you'll need to have both the C API installed and the Ruby MySQL module.
The C API is part of MySQL source code distribution. You can refer to my blog entry "How to Build MySQL source code on Solaris".
To install Ruby MySQL module, you can refer to my blog entry "Ruby MySQL Module Installation and Setup".
Download Ruby DBI driver
Go
to this site.
http://rubyforge.org/projects/ruby-dbiClick on view all projects. Scroll down and download dbi-0.2.0.tar.gz
Installation
#pwd
/export/home/mysql
# gzip -d dbi-0.2.0.tar.gz
# tar xvf dbi-0.2.0.tar
#cd dbi-0.2.0
#
pwd
/export/home/mysql/dbi-0.2.0
Configure
After unpacking the distribution, change the location into its top-level directory and configure it using the setup.rb script in that directory.
#
pwd
/export/home/mysql/dbi-0.2.0
The most general configuration command looks like this, with no arguments following the config argument:
# ruby setup.rb config
That command configures the distribution to install all drivers by default. To be more specific, provide a --with option that lists the particular parts of the distribution you want to use. For example, to configure only the main DBI module and the MySQL DBD-level driver, issue the following command:
I used the following in my installation
# ruby setup.rb config --with=dbi,dbd_mysql
entering config phase...
config done.
Setup
After configuring the distribution, build it:
#
pwd
/export/home/mysql/dbi-0.2.0
# ruby setup.rb
setup
entering setup phase...
setting #! line to
"#!/usr/ruby/1.8/bin/ruby"
setup.rb: skip
bin/proxyserver(dbd_proxy) by user option
setup.rb: skip
ext/dbd_sybase(dbd_sybase) by user option
setup done.
Install Ruby DBI
After building it, install it with:
#
ruby setup.rb install
entering
install phase...
mkdir -p /usr/ruby/1.8/bin
install sqlsh.rb
/usr/ruby/1.8/bin
setup.rb: skip bin/proxyserver(dbd_proxy) by
user option
mkdir -p
/usr/ruby/1.8/lib/ruby/site_ruby/1.8/DBD/Mysql
install Mysql.rb
/usr/ruby/1.8/lib/ruby/site_ruby/1.8/DBD/Mysql/Mysql.rb
mkdir -p
/usr/ruby/1.8/lib/ruby/site_ruby/1.8/dbi
install columninfo.rb
/usr/ruby/1.8/lib/ruby/site_ruby/1.8/dbi
install row.rb
/usr/ruby/1.8/lib/ruby/site_ruby/1.8/dbi
install sql.rb
/usr/ruby/1.8/lib/ruby/site_ruby/1.8/dbi
install utils.rb
/usr/ruby/1.8/lib/ruby/site_ruby/1.8/dbi
install trace.rb
/usr/ruby/1.8/lib/ruby/site_ruby/1.8/dbi
install version.rb
/usr/ruby/1.8/lib/ruby/site_ruby/1.8/dbi
install dbi.rb
/usr/ruby/1.8/lib/ruby/site_ruby/1.8
setup.rb: skip
ext/dbd_sybase(dbd_sybase) by user option
install done.
Testing installation
Open
up IRB (which stands for Interactive Ruby). Type irb to invoke. Check
if “require dbi” returns true.
# irb
irb(main):001:0>
require "dbi"
=>
true
irb(main):004:0> quit
Creating DBI script file to get MySQL version
# pwd
/export/home/mysql/dbi-0.2.0
Add the following to simple.rb file
# more simple.rb
#!/usr/bin/ruby -w
# simple.rb - simple MySQL script using Ruby DBI module
require "dbi"
begin
# connect to the MySQL server
dbh = DBI.connect("DBI:Mysql:test:localhost", "testuser", "testpass")
# get server version string and display it
row = dbh.select_one("SELECT VERSION()")
puts "Server version: " + row[0]
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
ensure
# disconnect from server
dbh.disconnect if dbh
end
#
Run the Ruby DBI script
Run the script and the MySQL server version is displayed.
#
ruby simple.rb
Server
version: 5.1.30
#
References
Supported Drivers, Docs, Discussion forms:
http://www.sun.com/software/products/mysql/drivers.jsp
Articles for installing the drivers:
http://www.kitebird.com/articles/
The Ruby home page provides general
information about Ruby itself:
Posted at 11:48AM Jan 05, 2009 by Arathi Krishnaiyengar in MySQL | Comments[0]
Monday Jan 05, 2009