I found only partial documentation on how to install the Ruby driver for MySql. There are two version of drivers, one is MySql/Ruby and is written in C, the second is Ruby/MySql and is written in Ruby. Ruby/MySql is included in Rails but has slower performance, plus it was not working with MySql 5.0, it does not work with MySql 4.something too!
So I decieded to compile the C driver. Below you can find the commands:
curl http://tmtm.org/downloads/mysql/ruby/mysql-ruby-2.7.tar.gz -o mysql-gzip -d gzip -d mysql-ruby-2.7.tar.gz tar xfv mysql-ruby-2.7.tar cd mysql-ruby-2.7 ruby extconf.rb --with-mysql-include=/usr/local/mysql/include/mysql --with-mysql-lib=/usr/local/mysql/lib/mysql make sudo make install
Make sure that 'make install' will be executed as root. Now you can test the connection with:
ruby test.rb [hostname] [username] [dbpassword]
Of course you have to put the host, username and password. Another nice script to test your MySql connection is:
First of all I am using Windows as OS (yes, I have to use it here).
Rails has a very nice ORM: ActiveRecord. However ActiveRecord is quite complex and a simple library to manipulate data in a script can be more than enough. In this post I will use MySql as DBMS. First of all you need the MySql adapter and the MySql gem:
gem install mysql
If you have troubles when installing the gem and you have the MySql 5.1 client, download and install the 5.0 instead, there are, at the moment of writing some issues in Windows. Now you can check if you can connect to the database. The following example is the code to manipulate data in a MySql table named users. As you can see it is not so different from the previous example, but it can be use with other DBMSs, this is the beauty of a database abstracion layer, using the adapter design pattern.
require 'dbi'
# Connect to a database, old style dbh = DBI.connect('DBI:Mysql:test:localhost', 'root', 'asdf')
# Insert some rows, use placeholders 1.upto(13) do |i| sql = "insert into users (username) VALUES (?)" dbh.do(sql, "user#{i}") end
# Select all rows from users sth = dbh.prepare('select * from users') sth.execute
# Print out each row while row=sth.fetch do p row end
# Close the statement handle when done sth.finish
# Don't prepare, just do it! dbh.do('delete from users where id > 10')
JRuby, a thread safe Ruby implementation for multi cores
Ruby 1.8 was not thread safe, now the problem with 1.9 is ended, but there is the global interpreter lock, that prevents the MRI interpreter to scale on multiple cores. JRuby is not written in C, it is written using Java and uses Java threads. Java threads are thread safe and can scale on multiple cores, as any J2EE application.
Please, have a look this Google Tech video by Ola Bini, a JRuby core developer. He talks about JRuby, his threads, garabage collection and more.
Ola Bini works for ThoughtWorks, he has developed another language called Loke. Loke is built to run on the JVM and borrows many ideas from Lisp.
Freelance Ruby and PHP developer. System administrator. I have experience with Linux, Ruby, PHP, MySql, Oracle. You can reach me by email rtaconi at gmail dot com. Twitter user is rtacconi