Installing mySQL server and lib-mysql for Ruby on Ubuntu 7.10
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:
- Line 1 – the DBI api is called
- Line 2 – a connection is created
- line 3 to 5 – some values are inserted into database
- line 6 – a query is created and the result is storage in a result set variable
- 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.


Brazilian guy, IT Specialist, Linux and Mac User. Work with Java/JEE and IBM Products, such as: WebSphere and DB2. Like studying Ruby, Android and IOS. Also, I like playing tennis, however I am not good enough. Write a post in this blog once a year. Follow me on twitter if you understand portuguese: @jairrillo.
Ubuntu looks very sweet. I’m going to install it as dualboot on my laptop.
Thanks for the help!
thanks! this article help me so much.