tags:

views:

731

answers:

5

I'm starting to get a bit desperate in my attempts to get a ruby script to connect to MySQL.

I've given up on DBI, as I just don't seem to be able to get it to work no matter what I do. I figured I might have more luck with just using mysql.rb from ruby-mysql. However, in using that I get the following error:

./mysql.rb:453:in `read': Client does not support authentication protocol requested by server; consider upgrading MySQL client (Mysql::Error)

I gather from googling that error message that this means my version of MySQL is too recent and doesn't support old-style passwords. I'm on a shared server and don't have root access, so I can't make the changes recommended to the MySQL config.

My version, btw, is:

mysql  Ver 14.7 Distrib 4.1.22, for pc-linux-gnu (i686) using readline 4.3

Has anyone succeeded in getting ruby to connect to MySQL? I've been trying under Windows, since I have admin access on my Windows machine, but if there's a way to do it without root access on Linux, that'd be even better.

+1  A: 

What are you looking to do? I think it is possible to use the ActiveRecord component of Rails to establish a connection to MySQL. I'll see if i can find some documentation to back this up, and i'll update this answer.

link to ActiveRecord/MySQL integration

Update: to use ActiveRecord for the purpose are looking for, you will need to create the various model classes that represent the tables you are pulling data from, and specify their relationships. Once that is done, you will be able to do something like:

SomeModel.find(:all, :conditions => ['some_column = ?', some_value]

I've used this approach in the past to provide my Rails apps with access to WordPress data.

Jason Miesionczek
If it's not too heavy-weight, that would be great. I just want some way to send SQL queries to a MySQL server, and deal with the results. I do this with PHP all the time, but I need to use the Matrix capabilities of Ruby. I kind of figured that Ruby would have MySQL access stuff built in...
Ben
Thanks -- this does look like a good approach. I'm going to keep trying to see if I can get Ruby to talk directly to MySQL, though, as I don't feel like I want to learn the ActiveRecord syntax and idiom just for a small task... I'd really prefer to use SQL. I can see the merit of your suggestion tho
Ben
+1  A: 

I haven't had any issues with connecting to MySQL from Ruby on my test machines.

You mention that you're using a shared hosting provider. If they have that old of a ruby-mysql version that it's not compatible with the new style passwords, perhaps their support staff can make the changes you need made to the user row(s) in MySQL.

Chris Ess
+2  A: 

There is a good summary of how to do this here: http://rubylearning.com/satishtalim/ruby_activerecord_and_mysql.html

Geoffrey Chetwood
+2  A: 

Sounds like you've run in to a MySQL quirk. Some time around version 5.0 they changed the format of connect passwords. It's an easy fix, though: http://dev.mysql.com/doc/refman/5.1/en/old-client.html

(Also, check out the Ruby Sequel gem, it lets you do some real fun stuff with all sorts of databases and without the overhead of ActiveRecord: http://www.ruby-sequel.org/documentation/getting_started )

Eli
Interesting. Thanks. I've asked my hosting company if they'll make the change to a user password. We'll see if that works. I don't suppose you happen to know where I can get hold of the Ruby Sequel gem? I tried the gem install route, and that couldn't find it...
Ben
http://www.ruby-sequel.org/documentation/getting_started
Eli
A: 

It may be that you're using an older version of mysql and need to upgrade it.

Ryan Bigg
It's exactly the opposite - the newer version of MySQL has the alternate password hashing algorithm.
Thelema