# Problems using the Mysql gem

**URL:** <https://rubytalk.org/t/problems-using-the-mysql-gem/59940>\
**Category:** ruby-talk\
**Created:** [6 September 2010 09:27 UTC](https://rubytalk.org/t/problems-using-the-mysql-gem/59940 "2010-09-06T09:27:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jim\_Burgess1](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@Jim\_Burgess1](https://rubytalk.org/u/Jim_Burgess1)\
**Post date:** [6 September 2010 09:27 UTC](https://rubytalk.org/t/problems-using-the-mysql-gem/59940/1 "2010-09-06T09:27:28Z")

</div>

Hi,

I'm using the mysql (2.8.1 x86-mingw32) gem and MySQL Server version  
5.0.90 on Windows 7, 64 bit. I'm also using ruby 1.8.7.

When I run the following script (replacing user name and password  
appropriately)

require "rubygems"  
require "mysql"  
dbh = Mysql.real\_connect("localhost", "user", "pass", "test")  
puts "Server version: " + dbh.get\_server\_info  
dbh.close

I get the following error message:

./test.rb:3: uninitialized constant Mysql (NameError)  
from C:/Ruby187/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:32:in  
`gem\_original\_require'  
from C:/Ruby187/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:32:in  
`require'  
from mysql.rb:2

> Exit code: 1

Can anyone give me a pointer as to what is going wrong?  
I have tried googling the error message, but with no luck.  
I have also read the documentation installed with the gem and as far as  
I can tell I am using the correct syntax.

Thanks in advance.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [6 September 2010 10:02 UTC](https://rubytalk.org/t/problems-using-the-mysql-gem/59940/2 "2010-09-06T10:02:00Z")

</div>

I can't test it in Windows, but I've tested in Ubuntu with Ruby 1.8.7  
and mysql gem 2.8.1 and it works.  
Line 2 of mysql.rb is doing a require of mysql\_api, which in Ubuntu  
loads a mysql\_api.so from the same folder.  
Did the gem install correctly in Windows? I guess you should have a  
mysql\_api dll somewhere in your gem structure.  
I'm just guessing, since I don't know how the mysql gem installs under  
Windows, but maybe this gives you some clue.

In fact, taking a look at the code:

begin  
&nbsp;&nbsp;require 'mysql\_api'  
rescue LoadError  
&nbsp;&nbsp;if RUBY\_PLATFORM =~ /mingw|mswin/ then  
&nbsp;&nbsp;&nbsp;&nbsp;RUBY\_VERSION =~ /(\d+.\d+)/  
&nbsp;&nbsp;&nbsp;&nbsp;require "#{$1}/mysql\_api"  
&nbsp;&nbsp;end  
end

it seems that if requiring mysql\_api fails and the platform is  
Windows, it tries to load a specific version. Does that help?

Jesus.

> **···**
>
> On Mon, Sep 6, 2010 at 11:27 AM, Jim Burgess \<jack.zelig@gmail.com\> wrote:
> 
> > Hi,
> > 
> > I'm using the mysql (2.8.1 x86-mingw32) gem and MySQL Server version  
> > 5.0.90 on Windows 7, 64 bit. I'm also using ruby 1.8.7.
> > 
> > When I run the following script (replacing user name and password  
> > appropriately)
> > 
> > require "rubygems"  
> > require "mysql"  
> > dbh = Mysql.real\_connect("localhost", "user", "pass", "test")  
> > puts "Server version: " + dbh.get\_server\_info  
> > dbh.close
> > 
> > I get the following error message:
> > 
> > ./test.rb:3: uninitialized constant Mysql (NameError)  
> > from C:/Ruby187/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:32:in  
> > `gem\_original\_require'  
> > from C:/Ruby187/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:32:in  
> > `require'  
> > from mysql.rb:2
> > 
> > > Exit code: 1
> > 
> > Can anyone give me a pointer as to what is going wrong?  
> > I have tried googling the error message, but with no luck.  
> > I have also read the documentation installed with the gem and as far as  
> > I can tell I am using the correct syntax.

---

<div class="post-metadata">

**Author:** ![Luis\_Lavena1](https://avatars.discourse-cdn.com/v4/letter/l/f08c70/32.png) [@Luis\_Lavena1](https://rubytalk.org/u/Luis_Lavena1)\
**Post date:** [6 September 2010 17:50 UTC](https://rubytalk.org/t/problems-using-the-mysql-gem/59940/3 "2010-09-06T17:50:24Z")

</div>

> Hi,
> 
> I'm using the mysql (2.8.1 x86-mingw32) gem and MySQL Server version  
> 5.0.90 on Windows 7, 64 bit. I'm also using ruby 1.8.7.

Is MySQL installed in 64bits? Or is 32bits MySQL?

Please note that MySQL binary needs a 32bits version of MySQL 5.0  
"libmysql.dll"

You can find more details about copying libmysql.dll or about  
compiling MySQL gem for 5.1 in RubyInstaller tutorial page:

> **[Tutorials](https://github.com/oneclick/rubyinstaller/wiki/Tutorials)**
>
> RubyInstaller for Windows - Build recipes. Contribute to oneclick/rubyinstaller development by creating an account on GitHub.

> Can anyone give me a pointer as to what is going wrong?  
> I have tried googling the error message, but with no luck.  
> I have also read the documentation installed with the gem and as far as  
> I can tell I am using the correct syntax.

It might be that you don't have 32bits libmysql.dll somewhere in the  
PATH for Ruby to use.

Placing a 32bits libmysql.dll in Ruby bin directory should help.

> **···**
>
> On Sep 6, 6:27 am, Jim Burgess \<jack.ze...@gmail.com\> wrote:
> 
> --  
> Luis Lavena

---

<div class="post-metadata">

**Author:** ![Jim\_Burgess1](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@Jim\_Burgess1](https://rubytalk.org/u/Jim_Burgess1)\
**Post date:** [7 September 2010 08:45 UTC](https://rubytalk.org/t/problems-using-the-mysql-gem/59940/4 "2010-09-07T08:45:40Z")

</div>

Hi,

Thanks for the replies.

After much hair pulling and messing around with environment variables I  
finally figured out what was going wrong.

It is quite embarrassing, but I'll post it here as it might help someone  
else.

The problem was that I'd actually called the file I was working on  
'mysql.rb'.  
As the require 'mysql' line at the beginning of my code looks in the  
current directory first, my code was just including itself, and not the  
gem I wanted.  
Doh!

Thanks again for your help.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
