Installing mySQL server and lib-mysql for Ruby on Ubuntu 7.10

January 14th, 2008  | Tags: , , , , ,

Hello guys,

In this topic I am going to show you how to install the MySQL Server and the MySQL Ruby Support on Ubuntu 7.10.

Although there are many blogs/websites talking about this issue, I had some troubles and because of this I am going to show you how to fix some problems in the installation process.

Also, I am going to show how to install the mysql ruby support and how to use the DBI API with MySQL.

Installing MySQL Server on Ubuntu 7.10

The first step is run the apt-get and install the mysql server. Below following the line to install the mysql server, as well as the libmysql and mysql-client (these two programs are required to use with Ruby).

sudo apt-get install mysql-server libmysqlclient15-dev mysql-client

In the final of installation process, you will be prompt to enter the “root” password.

When the installation finish, Ubuntu already starts the MySQL service, so type on shell the following commando to connect into mysql database.

mysql -u root -p

Enter the password when it is required. Done!, your mysql server is installed and running properly.

Before keep going to the next step, let’s create a database and a simple table in the MySQL server. Run the following lines into MySQL console:

create database test;
create table customers (id int not null, name varchar(40), primary key(id));

The lines above (1) create a database called “test” and (2) create a table called “customers” with two fields (“id”,”name”).

As you can see, the mysql console is the default place to manage your server. You can run selects, inserts, deletes, creates commands on mySQL console, however htere is a GUI tool to run these commands as well. This tool is called “MySQL Query Browser”. Like the MySQL server, the installation of query browser is really easy on Ubuntu, simply type the following command on shell:

sudo apt-get install mysql-query-browser

The MySQL Query Browser should be installed on Applications -> Programming menu

Installing mysql ruby support

Ruby has a DBI api to work with Databases. If you want to use the DBI api with mysql, you must install mysql-ruby lib.

The first step is install the lib. Type on Linux shell the following commands:

sudo apt-get install libdbd-mysql-ruby libdbd-mysql-ruby1.8
sudo gem install mysql

So far so good!.

The next step is download the MySQL Ruby API. Go to http://www.tmtm.org/en/ruby/mysql/ and download the latest version.

After you get the source file, unzip it and type the following commands inside the directory created:

ruby ./setup.rb
ruby ./test.rb localhost root <root password setup on mysql install>
ruby ./install.rb

Note: I got problem when I ran the second line (ruby ./test…). The problem I got was:
connect…………../mysql.rb:453:in `read’: Client does not support authentication protocol requested by server; consider upgrading MySQL client (Mysql::Error)
from ./mysql.rb:130:in `real_connect’
from ./mysql.rb:91:in `initialize’
from ./mysql.rb:1085:in `new’
from ./mysql.rb:1085:in `connect’
from ./t/00connect.rb:1
from ./test.rb:23:in `load’
from ./test.rb:23
from ./test.rb:19:in `each’
from ./test.rb:19

If you got this problem as well, connect into mysql (mysql -u root -p) and type the following command:

set password for root@localhost = old_password("password");

Repeat the line 2 again and now the MySQL-Ruby-API should be installed.

Testing the MySQL-Ruby-API

Create a simple Ruby program to insert some values on database and retrieve these values. (You can test the code directly on the IRB tool).

The code content is:

require "dbi"
con = DBI.connect("DBI:Mysql:test:localhost","root","yourpassword")
con.do("insert into customers (id,name) values (1,"Test Value 1")
con.do("insert into customers (id,name) values (2,"Test Value 2")
con.do("insert into customers (id,name) values (3,"Test Value 3")
rs = con.execute("select * from customers")
rs.fetch {|row| puts row["name"]}

In the example above, we have:

  1. Line 1 – the DBI api is called
  2. Line 2 – a connection is created
  3. line 3 to 5 – some values are inserted into database
  4. line 6 – a query is created and the result is storage in a result set variable
  5. line 7 – the names of the result are shown

You are able to use the MySQL API rather than DBI. Look at the MySQL API documentation to see the commands available for this API.

Well guys, I hope this topic is useful for someone.

  1. January 30th, 2008 at 17:47
    #1

    Ubuntu looks very sweet. I’m going to install it as dualboot on my laptop. :)

  2. Muyowa Mutemwa
    March 2nd, 2008 at 14:28
    #2

    Thanks for the help!

  3. heron
    July 27th, 2010 at 15:54
    #3

    thanks! this article help me so much.

TOP