Connecting to a non-localhost Mysql with Ruby-DBI

F

Francis Hwang

I'm using DBI 0.0.23 on Slackware 10 and trying to connect to Mysql
running on another server. Here's the code I'm trying out:

require 'dbi'

dbh = DBI.connect(
'dbi:Mysql:dbname=my_db;host=dc2-mysql-01.kattare.com', 'username',
'password'
)
row = dbh.select_one 'select * from some_table'

And the error I get is

/usr/local/lib/ruby/site_ruby/1.8/DBD/Mysql/Mysql.rb:403:in `execute':
No database selected (DBI::DatabaseError)
from /usr/local/lib/ruby/site_ruby/1.8/dbi/dbi.rb:1049:in
`execute'
from /usr/local/lib/ruby/site_ruby/1.8/dbi/dbi.rb:640:in
`execute'
from /usr/local/lib/ruby/site_ruby/1.8/dbi/dbi.rb:662:in
`select_one'
from test.rb:6

I also get errors using other DSNs, such as:

'dbi:Mysql:my_db;db.host.com' => "Can't connect to local MySQL server
through socket '/tmp/mysql.sock' (2)"
'dbi:Mysql:database=my_db;host=dc2-mysql-01.kattare.com' =>
"Segmentation fault" (happens during DBH#select_one)

Any ideas as to what else I should try?

Francis
 
P

Paul

can you connect via a CLI to the db form the non-localhost?
ie.
%mysql -uroot -ppassword -hdc2-mysql-01.kattare.com my_db
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1 to server version: 4.1.12a-nt

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

Sort that out fist via mysql.
 
P

Paul

What mysql.sock is the server using? probably /var/mysql/mysql.sock if
so.
You may be able to create a symbolic link to it.

ln /var/mysql/mysql.cosc /tmp/mysql.sock
& try again.

Havn' t used dbi:Mysql so there may wall be a way to set the sock when
you call it.


ie.
dbh = DBI.connect(

'dbi:Mysql:dbname=my_db;host=dc2-mysql-01.kattare.com;sock=/var/mysql/mysql.sock',
'username',
'password'
)
 
F

Francis Hwang

I tried messing with the sock, it didn't seem to do anything.

Here's something possibly relevant, though: The server has both
Ruby/MySQL (pure Ruby version) and MySQL/Ruby (C extension) installed.
I wrote a test script that can use either, and it works fine with
Ruby/MySQL and bombs with MySQL/Ruby. I've also found other posts in
ruby-talk that say essentially "MySQL/Ruby doesn't work, don't use it."

So, is there anyway of coercing DBI to specify using "mysql.rb" as a
driver instead of "mysql"? I'd skip DBI entirely if it weren't for the
fact that Lafcadio uses it. (Probably foolishly so, I'd say at this
point.)
 
J

Jesper Rønn-Jensen

I just found this thread and setting the symbolic link did it for me:

ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock

Thanks for sharing this trick!

/Jesper
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top