MySQL
Ruby MySQL Module Installation and Setup
Introduction
This
document describes the steps required on Solaris for connecting to
MySQL database using MySQL-Ruby.
Prerequisite
MySQL driver integration needs MySQL C client API header files and libraries. This is available only through the source code distribution. Please refer to my blog entry "How to Build MySQL source code on Solaris".
Ruby Version Requirement
Ruby 1.8.2-1.8.7, 1.9.0
# ruby -version
ruby 1.8.6 (2008-08-11 patchlevel 287) [sparc-solaris2.11]
Download Ruby MySQL Module
Go to this site http://www.tmtm.org/en/mysql/ruby/
Under download section, select mysql-ruby-2.8.tar.gz .
MySQL-Ruby Installation
#pwd /export/home/mysql #gzip -d mysql-ruby-2.8.tar.gz #tar xvf mysql-ruby-2.8 #cd mysql-ruby-2.8
Configure, Build and Install
After unpacking the distribution, change the location into its top-level directory and configure it using the extconf.rb script in that directory.
# pwd
/export/home/mysql/mysql-ruby-2.8 # ruby extconf.rb
If extconf.rb successfully locates your MySQL header file and library directories, you can proceed to build and install the module. Otherwise, it indicates what it could not find, and you'll need to run the command again with additional options that specify the appropriate directory locations. For example, if your header file and library directories are /usr/local/mysql/include/mysql and /usr/local/mysql/include/lib, the configuration command looks like this:
# ruby extconf.rb \
--with-mysql-include=/usr/local/mysql/include/mysql \
--with-mysql-lib=/usr/local/mysql/lib/mysql
Alternatively, tell extconf.rb where to find the mysql_config program. In that case, extconf.rb runs mysql_config to locate the header and library files:
# ruby extconf.rb --with-mysql-config=/usr/local/mysql/bin/mysql_config
In my installation, I have used the following..
#ruby extconf.rb --with-mysql-include=/usr/local/mysql/include/mysql –with-mysql-lib=/usr/local/mysql/lib/mysql
checking
for mysql_query() in -lmysqlclient... yes
checking for
mysql_ssl_set()... yes
checking for rb_str_set_len()...
no
checking for mysql.h... no
checking for mysql/mysql.h...
yes
creating Makefile
After configuring the distribution, build and install the module:
% make % make install
Create simple.rb script
Add the following contents to file simple.rb and save it.
#!/usr/bin/ruby -w
# simple.rb - simple MySQL script using Ruby MySQL module
require "mysql"
begin
# connect to the MySQL server
dbh = Mysql.real_connect("localhost", "testuser", "testpass", "test")
# get server version string and display it
puts "Server version: " + dbh.get_server_info
rescue Mysql::Error => e
puts "Error code: #{e.errno}"
puts "Error message: #{e.error}"
puts "Error SQLSTATE: #{e.sqlstate}" if e.respond_to?("sqlstate")
ensure
# disconnect from server
dbh.close if dbh
end
Execute the script to get the MySQL version
#
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