PDA

View Full Version : Problem using MySQL from Ruby (not RoR)


Ponder
05-08-2006, 11:23 AM
I'm trying to write a ruby script to contact my MySQL database and am running into problems. This script:


#!/usr/bin/ruby
require "mysql"


returns:

./foo.rb:2:in `require': no such file to load -- mysql (LoadError)
from ./foo.rb:2


"gem list --local" claims mysql 2.7 is installed. Any idea what's going on?

Thanks.

Plec
02-15-2007, 03:16 PM
I also got this problem.

The solution : export RUBYOPT=rubygems :)

This variable tell Ruby to load RubyGems every time it starts up

terrpy
04-17-2007, 02:45 PM
Was also having the same problem, until now. :)
Thanks very much!

terrpy
04-17-2007, 06:23 PM
I put the line:

export RUBYOPT=rubygems

in my .bashrc file so that whenever I login via ssh ruby works with mysql and other ruby gems.

However, my eventual use will not have anything to do with the bash shell (AFAIK). The idea is to have cron start a ruby script and process all the emails in a certain mailbox, extract the data, and put it in mysql.

Where would I need to put the "export RUBYOPT=rubygems" to ensure that mysql is loaded?

--

Plec
05-02-2007, 02:00 PM
Where would I need to put the "export RUBYOPT=rubygems" to ensure that mysql is loaded?
--

In /etc/crontab, you can set RUBYOPT before calling your ruby script.
Example:
1 3 * * * root export RUBYOPT=rubygems;/home/terrpy/rails/your_script.rb

Or you can set RUBYOPT in the global variables at the beginning of /etc/crontab (like SHELL or PATH variables)

terrpy
05-02-2007, 05:52 PM
Thanks! I'm learning. :o :)