# Mysql-ruby won't connect

**URL:** <https://www.ruby-forum.com/t/mysql-ruby-wont-connect/80932>\
**Category:** Ruby\
**Created:** [January 2, 2007, 8:51am UTC](https://www.ruby-forum.com/t/mysql-ruby-wont-connect/80932 "2007-01-02T08:51:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 2, 2007, 8:51am UTC](https://www.ruby-forum.com/t/mysql-ruby-wont-connect/80932/1 "2007-01-02T08:51:04Z")

</div>

I’m trying to get my first ruby box set up. I’m so far unable to  
connect to the mysql database with mysql-ruby.

I’m on an Intel Mac, running macosx tiger 10.4.8. At first, I couldn’t  
even compile the plugin, there is some bug in the software. It gave an  
error like

mysql.c: In function ‘Init\_mysql’:  
mysql.c:2015: error: ‘ulong’ undeclared (first use in this function)

After googling around, I found that this error is known, and can be  
fixed by simply declaring that variable in mysql.c. See  
[http://www.maxdunn.com/typo/articles/2006/12/12/installing-mysql-gem-on-mac-os-x-10-4](http://www.maxdunn.com/typo/articles/2006/12/12/installing-mysql-gem-on-mac-os-x-10-4)  
or

> **[pburkholder.com](http://typo.pburkholder.com/articles/tag/macosx)**
>
> This domain may be for sale!

or  
[http://www.planetrubyonrails.org/show/feed/11](http://www.planetrubyonrails.org/show/feed/11)  
for details.

After declaring that variable, the software compiled successfully, but  
was unable to connect to the database. When I run the test file with

$ /usr/local/bin/ruby test.rb localhost root password

I get 180 errors all looking something like

1. Error:  
test\_sqlstate(TC\_MysqlStmt2):  
Mysql::Error: Access denied for user ‘lethe’@‘localhost’ (using  
password: NO)  
test.rb:441:in `new' test.rb:441:in `setup’

I notice that it’s not trying to connect with the username and password  
I specified on the command line. “lethe” is my account login, whereas  
my sql login is “root”. And it’s not specifying a password. Perhaps  
listing host, user, and password on the command line is not the right  
way to specify for test.rb?

I can connect manually to the database just fine, and here is my user  
table:  
mysql\> select Host,User,Password from mysql.user;  
±----------±------±------------------------------------------+  
| Host | User | Password |  
±----------±------±------------------------------------------+  
| localhost | root | \*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 |  
| avernus | root | \*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 |  
| avernus | | \*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 |  
| localhost | | \*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 |  
±----------±------±------------------------------------------+  
4 rows in set (0.00 sec)

that password is the encrypted version of the word “password”.

Forging ahead blindly, I figure that if these tests are failing because  
they can’t login, they’re not specifying a password, it might help if I  
_remove_ the password. So after removing the passwords from the  
database and flushing privileges, I can connect manually to the dbase  
without a password, like  
$ mysql -u root  
or even just  
$ mysql

Now, when I run the mysql-ruby test, it works!

$ /usr/local/bin/ruby test.rb localhost root password  
Loaded suite test  
Started  
…FF…  
Finished in 0.271991 seconds.

1. Failure:  
test\_fetch\_double(TC\_MysqlStmt2) [test.rb:920]:  
\<-1.79769313486232e+308\> and  
\<-1.79769313486232e+308\> expected to be within  
\<2.22044604925031e-16\> of each other.

2. Failure:  
test\_fetch\_double\_unsigned(TC\_MysqlStmt2) [test.rb:937]:  
\<1.79769313486232e+308\> and  
\<1.79769313486232e+308\> expected to be within  
\<2.22044604925031e-16\> of each other.

113 tests, 376 assertions, 2 failures, 0 errors

Only two of the several hundred tests fail, and those two probably  
don’t mean anything, I figure.

Alas, I still cannot connect to the database from my mod\_ruby script.  
My script contains essentially:

\<%  
require ‘mysql’  
dbh = Mysql.real\_connect(“localhost”, “root”, “”, “test”)  
puts "Server version: " + dbh.get\_server\_info  
%\>

The browser shows me the HTTP 500 error page, and my apache log shows  
me:

[Mon Jan 01 23:46:01 2007] [error] mod\_ruby:  
/usr/local/apache2/htdocs/db.rhtml:13:in `real\_connect’: Can’t connect  
to MySQL server on ‘localhost’ (49) (Mysql::Error)

I suspect that the mysql-ruby plugin is not sending the correct  
host/user/password as specified, but I’m really not sure, nor can I  
figure out a way to debug this further. Any help you can offer me  
would be greatly appreciated.

Thanks,  
lethe

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 2, 2007, 1:09pm UTC](https://www.ruby-forum.com/t/mysql-ruby-wont-connect/80932/2 "2007-01-02T13:09:25Z")

</div>

On Jan 2, 2007, at 2:50 AM, removed\_email\_address@domain.invalid wrote:

> mysql.c:2015: error: ‘ulong’ undeclared (first use in this function)
> 
> password: NO)  
> I can connect manually to the database just fine, and here is my user  
> 4 rows in set (0.00 sec)  
> $ mysql -u root  
> Finished in 0.271991 seconds.  
> \<1.79769313486232e+308\> expected to be within  
> \<%  
> to MySQL server on ‘localhost’ (49) (Mysql::Error)
> 
> I suspect that the mysql-ruby plugin is not sending the correct  
> host/user/password as specified, but I’m really not sure, nor can I  
> figure out a way to debug this further. Any help you can offer me  
> would be greatly appreciated.

Interestingly enough I have the same problem, but hadn’t noticed  
because I don’t usually use mysql passwords on my development machine  
(intel macbook). I suspect this is a bug in the mysql gem itself.  
It already has to get patched to compile, so there may be other issues.

I’ll look at it some today and see if I can dig up any more details,  
but for the moment I can just add a hearty “Me too!”  
-Mat

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 24, 2007, 4:14pm UTC](https://www.ruby-forum.com/t/mysql-ruby-wont-connect/80932/3 "2007-01-24T16:14:17Z")

</div>

On Jan 2, 2007, at 2:50 AM, removed\_email\_address@domain.invalid wrote:

> figure out a way to debug this further. Any help you can offer me  
> would be greatly appreciated.

Sorry this took me forever to get back to, but I think I may have  
stumbled across a fix. Basically the problem seems to surround  
mysql’s replacement of a ulong typedef with my\_ulonglong instead. No  
idea why. Pretty sure it’s something new with MySQL 5, maybe even  
just only the OS X distribution.

Anyway, before you compile the gem insert the following at line 108  
in mysql.h (probably /usr/local/mysql/include/mysql.h)

#if !defined(HAVE\_ULONG) && !defined(TARGET\_OS\_LINUX) && !defined  
(\_\_USE\_MISC)  
typedef unsigned long ulong; /\* Short for unsigned long \*/  
#endif

Then build the gem:  
sudo gem install mysql – --with-mysql-dir=/usr/local/mysql # not  
sure if that mysql-dir field is necesary, but I did it anyway

Now password authentication is working for me again. I’d also remove  
those lines from mysql.h once you’re done. I have no idea what they’d  
do when trying to compile other plugins.

I haven’t researched what the exact cause of the problem, but this  
seems like a workaround till mysql and the mysql gem get back in sync.

Hope it helps!  
-Mat

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 28, 2007, 11:21pm UTC](https://www.ruby-forum.com/t/mysql-ruby-wont-connect/80932/4 "2007-01-28T23:21:11Z")

</div>

On Jan 24, 7:14 am, Mat S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Anyway, before you compile the gem insert the following at line 108  
> in mysql.h (probably /usr/local/mysql/include/mysql.h)
> 
> #if !defined(HAVE\_ULONG) && !defined(TARGET\_OS\_LINUX) && !defined  
> (\_\_USE\_MISC)  
> typedef unsigned long ulong; /\* Short for unsigned long \*/  
> #endif

Alright, I’ve done this step.

> Then build the gem:  
> sudo gem install mysql – --with-mysql-dir=/usr/local/mysql # not  
> sure if that mysql-dir field is necesary, but I did it anyway

Actually, I’m trying to install directly from the mysql-ruby tarball,  
rather than gem. After I run  
$ ruby extconf.rb --with-mysql-dir=/usr/local/mysql  
$ make  
$ ruby test.rb nonexistenthost nonexistentuser stupidpassword

I continue to get errors of the form

1. Error:  
test\_sqlstate(TC\_MysqlStmt2):  
Mysql::Error: Access denied for user ‘lethe’@‘localhost’ (using  
password: NO)  
test.rb:441:in `new' test.rb:441:in `setup’

In other words, the test suite is not receiving the hostname and login  
that I’m specifying on the command line, rather it’s defaulting to my  
userlogin and localhost. Don’t know why.

I try it your way too, though. sudo gem install mysql (the newest  
version I can get this way is apparently 2.7, whereas my tarball is  
2.7.3). But no difference: I can connect to the database in irb, but  
not from a web script. I continue to get the error

[Sun Jan 28 13:55:32 2007] [error] mod\_ruby: /usr/local/apache2/htdocs/  
eruby/dbtest.rhtml:9:in `real\_connect’: Can’t connect to MySQL server  
on ‘localhost’ (49) (Mysql::Error)

from apache logs. So real\_connect works in irb, but not in a mod\_ruby  
script. I’m not sure whether that is related to the failure of the  
mysql test suite to get the user/pass I specify on the command line.

> Now password authentication is working for me again. I’d also remove  
> those lines from mysql.h once you’re done. I have no idea what they’d  
> do when trying to compile other plugins.
> 
> I haven’t researched what the exact cause of the problem, but this  
> seems like a workaround till mysql and the mysql gem get back in sync.
> 
> Hope it helps!  
> -Mat

Thanks for your help, Mat. This is pretty frustrating, so it’s nice  
to have people try to help. Unfortunately, this doesn’t seem to have  
helped me. Maybe our problems are different? Do you have any further  
insight to share about that?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 6, 2007, 6:06am UTC](https://www.ruby-forum.com/t/mysql-ruby-wont-connect/80932/5 "2007-02-06T06:06:57Z")

</div>

On Jan 28, 2:18 pm, “zig” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks for your help, Mat. This is pretty frustrating, so it’s nice  
> to have people try to help. Unfortunately, this doesn’t seem to have  
> helped me. Maybe our problems are different? Do you have any further  
> insight to share about that?

I said that this workaround didn’t fix things for me, but maybe it  
did. At this point, I’m a little foggy on which parts worked when,  
since I’ve only looked at this sporadically over the course of a  
couple of weeks. My test.rb unit testing still does not capture the  
login I hand it on the command line; that thing is not working. For a  
while, I thought I was still broken. I couldn’t connect to the  
database in my rhtml file, so I figured that the problem with my  
test.rb meant that the software just isn’t working on my system. Not  
so. I did notice that I _can_ connect in irb. In fact, my rhtml test  
file will connect to the database if I run eruby on it from the  
command line like

# eruby db.rhtml

When I discovered this, I realized that I must be closer to working  
than I thought. I googled the woefully uninformative error message  
from my apache log (and it’s also worth pointing out that you can send  
that error right to the browser, rather than fishing it out of the  
apache error log, by encapsulating the mysql commands with some  
exception catching), that error again was

/usr/local/apache2/htdocs/db.rhtml:13:in `real\_connect’: Can’t connect  
to MySQL server on ‘localhost’ (49) (Mysql::Error)

Some of the google hits indicated that this error means that the  
mysql.sock file was not found. Mysql#real\_connect takes a parameter  
for the mysql.sock socket file. Specifying this in my rhtml file made  
everything work.

In the end, my file looked like this:

 Eruby + mysql/ruby 

This is a test of the mod\_ruby/Eruby mysql/ruby c bindings (not the pure ruby plugin known as ruby/mysql)

 \<% require 'mysql' begin dbh = Mysql.real\_connect("localhost", "root", "password", "test", 3306,'/tmp/mysql.sock') puts "Server version: " + dbh.get\_server\_info dbh.select\_db("db") res = dbh.query("select \* from table where id = 1") res.fetch\_row().each {|i| puts i} rescue Mysql::Error puts $!.error end

```
%>

```

I guess I’m up and running now, so let me give thanks to the newsgroup  
for support, and especially to Mat for his help.

And by the way, I don’t remember completely, but I’m pretty sure that  
I couldn’t get this far before I implemented Mat’s fix. So I think  
that actually was the resolution of my problem, despite what I said  
above.

Also, I still don’t know why I have to specify the socket file within  
the Apache environment mod\_ruby rhtml file, but not when running irb  
or eruby from the command line. Obvious some environment variable is  
different, which causes the socket file not to be found in the Apache  
environment. But which? I would like to figure that out too, it  
would be nice not to have to type that in, though it’s not too  
important. I played around with the PATH variable, I don’t think  
that’s the problem. So feel free to shoot me the answer to that, if  
you have it.

Otherwise, thanks again all.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 11, 2007, 10:40pm UTC](https://www.ruby-forum.com/t/mysql-ruby-wont-connect/80932/6 "2007-02-11T22:40:51Z")

</div>

On Jan 24, 7:14 am, Mat S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’d also remove  
> those lines from mysql.h once you’re done. I have no idea what they’d  
> do when trying to compile other plugins.

I can confirm that it is indeed necessary to delete those lines after  
installing mysql-ruby. php failed to compile with those lines present  
in my mysql.h.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 6, 2007, 5:35am UTC](https://www.ruby-forum.com/t/mysql-ruby-wont-connect/80932/7 "2007-02-06T05:35:55Z")

</div>

On Jan 1, 11:48 pm, removed\_email\_address@domain.invalid wrote:

> After googling around, I found that this error is known, and can be  
> fixed by simply declaring that variable in mysql.c. Seehttp://www.maxdunn.com/typo/articles/2006/12/12/installing-mysql-gem-…  
> orhttp://typo.pburkholder.com/articles/tag/macosx  
> orhttp://www.planetrubyonrails.org/show/feed/11  
> for details.
> 
> After declaring that variable

For the sake of completeness and self-containedness, let me type in  
what the fix was that was linked in those blogs.

To resolve the

mysql.c: In function ‘Init\_mysql’:  
mysql.c:2015: error: ‘ulong’ undeclared (first use in this function)

error, the workaround to this bug is to add the line

#define ulong unsigned long

to mysql.c near the top with the other preprocessor defs. I can  
confirm that this fix enabled me to successfully compile the  
software. But see Mat’s post later in this thread for what is  
probably a better fix.
