Connecting Ruby and MySQL

I installed MySQL 5.5 ( mysql-installer-5.5.15.0.msi ) and mysql gem (
with “gem install mysql” command ) . Now I write a simple program ( from
this website :
Learn How to Blog and Build Websites for Profit! ) and when
I run it ruby give me this message :

“C:/Ruby192/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in
`require’: 126: The specified module could not be found. -
C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql-2.8.1-x86-mingw32/lib/1.9/mysql_api.so
(LoadError)”

**the code is :

require ‘mysql’

#my = Mysql.new(hostname, username, password, databasename)
con = Mysql.new(‘localhost’, ‘’, ‘’, ‘ruby’)
rs = con.query(‘select * from student’)
rs.each_hash { |h| puts h[‘name’]}
con.close

please help me
thanks

amir e. wrote in post #1018955:

I installed MySQL 5.5 ( mysql-installer-5.5.15.0.msi ) and mysql gem (
with “gem install mysql” command )

“C:/Ruby192/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in
`require’: 126: The specified module could not be found. -
C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql-2.8.1-x86-mingw32/lib/1.9/mysql_api.so
(LoadError)”

judging from the error it looks like mysql was not installed
properly… maybe you’re missing dependencies? try:

“gem install -r mysql”

…which should also install any dependencies you are missing.

#my = Mysql.new(hostname, username, password, databasename)

…once you get mysql installed properly, i imagine you’ll want to
change this line. the pound sign ("#") makes it a comment, and
therefore the variable “my” is never defined, it should probably look
like this:

my = Mysql.new(hostname, username, password, databasename)

…you’ll also have to define all of those variables (hostname,
username, password, databasename,) otherwise it’s going nowhere!

good luck,

  • j

Looks to me, you’re installing mysql gem successfully, but not the
build mysql_api successfully.
While installing mysql, you will need to provide mysql lib or mysql
dir, to help gem to build your mysql_api.so

While using gem install mysql, gem usually hides this error.

To fix this, I suggest you download a ruby-mysql-2.8.1.tar.gz source
code.
$ gem uninstall mysql
$ gem list --local # see mysql uninstalled
$ tar xvzf ruby-mysql-2.8.1.gz
$ ruby ext-conf.rb
$ make
$ make install
This is the exact step gem use to install mysql on your machine, but
by doing this step by step, you will see what’s going wrong.
If you need to provide mysql lib or mysql dir during install, you are
doing the right thing.

Yes. I had the same problem. Download the libmySQL.dll into your
ruby/bin folder where u have your ruby installed (e.g.: C:/ruby/bin).

You can download it from:
http://instantrails.rubyforge.org/svn/trunk/InstantRails-win/InstantRails/mysql/bin/libmySQL.dll

That should work.

On Mon, Aug 29, 2011 at 3:11 AM, amir e. [email protected] wrote:

I am in Winows 7 and I think these commands are in Linux.

They will work if you have DevKit installed. You’ll need DevKit to
install gems that require building C extensions.

The mysql gem isn’t maintained anymore, AFAIK. If that matters to you,
you might want to try the mysql2 gem. There is also a gem called
ruby-mysql by the author of the mysql gem, written in 100% Ruby, so it
doesn’t need compilation.

I am in Winows 7 and I think these commands are in Linux.

amir e. wrote in post #1018955:

I installed MySQL 5.5 ( mysql-installer-5.5.15.0.msi ) and mysql gem (
with “gem install mysql” command ) . Now I write a simple program ( from
this website :
Learn How to Blog and Build Websites for Profit! ) and when
I run it ruby give me this message :

“C:/Ruby192/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in
`require’: 126: The specified module could not be found. -
C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql-2.8.1-x86-mingw32/lib/1.9/mysql_api.so
(LoadError)”

The problem lies in the incompatibility of the mysql gem (binary
version) and the installed MySQL.

When you do “gem install mysql” it prints out some notes about this.

To solve this, please follow the instructions on this blog post:

http://blog.mmediasys.com/2011/07/07/installing-mysql-on-windows-7-x64-and-using-ruby-with-it/

You can skip the MySQL installation steps as you already installed it.

You need RubyInstaller’s DevKit installed. All the links are provided in
the blog post.


Luis L.