Having Problem with MySQL Connectivity in Ruby

Guys, I am new here. I love Ruby, and after years I have decided to get
a project going. I installed Ruby 2.0, got the new Glade and GTK up, and
Visual Ruby. My plan was to build me a simple GUI that connects to a
core, which in turn connects to a MySQL database that I have prepared
ahead of time. I installed the current XAMPP package for a simple MySQL
installation and a decent admin.

I have attempted several different approaches to fix this problem and
now I am turning to the experts. The problem must be simple, but I am
going nuts. I installed the mysql gem with “gem install”… and allow
me to show you some of my code:

require ‘rubygems’
require ‘mysql’

class DatabaseCommunicator

def initialize(path, login, password, databaseName, table)
@path = path
@login = login
@password = password
@databaseName = databaseName
@table = table

def addSafebag(safeBagID, shop, employeeName, date)
dbh = Mysql.new(@path, @login, @password, @databaseName)

  return "Data entered!  "

rescue Mysql::Error
  exit 1
  return "Oh crap! no database connectivity!"



I mean it’s straight forward enough right? Now this is where it really
grinds me. I get this error message:

require': cannot load such file -- mysql/mysql_api (LoadError) from C:/Ruby200/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:inrequire’
rescue in <top (required)>' from C:/Ruby200/lib/ruby/gems/2.0.0/gems/mysql-2.9.1-x86-mingw32/lib/mysql.rb:2:in<top (required)>’
require' from C:/Ruby200/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:135:inrescue in require’

I am at my wits end here. I tried DBI, that didn’t work. I tried RDBI,
that also didn’t work. No matter what tutorial I look at, or which
forum I search through for answers, I simply cannot come to the
conclusion. Is there anyone here who can help me?


It looks like mysql gem was improperly installed. It tries to load
missing library `mysql_api’ which is compiled as a gem’s extension at

I do not use Windows but like in Linux/BSD you’ll need install mysql
development files (headers, libs) before, so extension library can be
successfuly compiled.

As a side note, be aware mysql gem is deprecated in favor of mysql2 gem.

Hmmm. I did also try installing mysql2. I don’t even remember that
working remotely. How would I go about uninstalling the useless Gems
without having to uninstall Ruby itself. Also, how would I go about
properly installing the mysql developement files properly? I really
would love to get this program working. Any tips?

You would first need to install development libraries, as did mentioned.
I didn’t tested but at http://dev.mysql.com/downloads/connector/c/ you
should find the installer for your platform.

Gems can be maintained by a command line tool called `gem’. See
Command Reference - RubyGems Guides about
uninstalling command.

Ok I installed the connector, and tried to reinstall the mysql gem.
Just noticed this. Now I did what the gem required by copying the file
it needed into the directory… however, it still has that error at the
bottom. Does that have anything to do with all this, and if yes, how do
I go about solving that?

C:>gem install mysql
Fetching: mysql-2.9.1-x86-mingw32.gem (100%)


You’ve installed the binary version of mysql.
It was built using MySQL Connector/C version 6.0.2.
It’s recommended to use the exact same version to avoid potential

At the time of building this gem, the necessary DLL files where
in the following URL:


You can put the lib\libmysql.dll available in this package to your
Ruby bin di
E.g. C:\Ruby\bin


Successfully installed mysql-2.9.1-x86-mingw32
Parsing documentation for mysql-2.9.1-x86-mingw32
unable to convert “\x90” from ASCII-8BIT to UTF-8 for
, skipping
unable to convert “\x90” from ASCII-8BIT to UTF-8 for
, skipping
Installing ri documentation for mysql-2.9.1-x86-mingw32
1 gem installed

Hi, I am facing this issue now. How to resolve?. Please provide the

here is another anomaly that I, for the life of me, cannot explain.
Upon mentioning that the mysql gem is deprecated, I decided to try
installing the mysql2 gem… what a catastrophe -.-

C:>gem install mysql2
Temporarily enhancing PATH to include DevKit…
Building native extensions. This could take a while…
ERROR: Error installing mysql2:
ERROR: Failed to build gem native extension.

C:/Ruby200/bin/ruby.exe extconf.rb

checking for ruby/thread.h… yes
checking for rb_thread_call_without_gvl() in ruby/thread.h… yes
checking for rb_thread_blocking_region()… yes
checking for rb_wait_for_single_fd()… yes
checking for rb_hash_dup()… yes
checking for rb_intern3()… yes
checking for mysql_query() in -lmysqlclient… no
checking for main() in -lm… yes
checking for mysql_query() in -lmysqlclient… no
checking for main() in -lz… no
checking for mysql_query() in -lmysqlclient… no
checking for main() in -lsocket… no
checking for mysql_query() in -lmysqlclient… no
checking for main() in -lnsl… no
checking for mysql_query() in -lmysqlclient… no
checking for main() in -lmygcc… no
checking for mysql_query() in -lmysqlclient… no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details.
You may
need configuration options.

Provided configuration options:

Gem files will remain installed in
3.16 for inspection.
Results logged to

So basically, no matter what I am trying here, it isnt working. I even
installed DBI gem, and even that doesnt work…

I got a list of all installed Gems here, anything missing=?

*** LOCAL GEMS ***

atk (2.2.0 x86-mingw32)
bigdecimal (1.2.0)
cairo (1.12.9 x86-mingw32
dbi (0.4.5)
deprecated (3.0.1, 2.0.1)
epoxy (0.3.1)
gdk_pixbuf2 (2.2.0 x86-mi
glib2 (2.2.0 x86-mingw32)
gtk2 (2.2.0 x86-mingw32)
gtksourceview2 (2.2.0 x86
io-console (0.4.2)
json (1.7.7)
minitest (4.3.2)
mysql (2.9.1 x86-mingw32)
pango (2.2.0 x86-mingw32)
pkg-config (1.1.5)
psych (2.0.0)
rake (0.9.6)
rdbi (1.1.0)
rdoc (4.0.0)
require_all (1.3.2)
test-unit (
typelib (0.1.0)
visualruby (1.0.16)
vrlib (1.0.16)

Sk Kann wrote in post #1170060:

Hi, I am facing this issue now. How to resolve?. Please provide the

There are some solutions mentioned if you google libmysql.dll. I think
it depends on version of ruby and/or version of MySQL. But this works
for me:

The folders here depends on where and what MySQL tools you install, but
you will find it somehow.

Find libmysql.dll in: “C:\Program Files (x86)\MySQL\MySQL Tools for 5.0”
or (newer version) in: “C:\Program Files\MySQL\MySQL Workbench 6.2 CE” -
if you have that installed.
Or get it:

Copy to the MySQL Server bin-folder on your computer (where it used to
be before), in my case “C:\Program Files\MySQL\MySQL Server 5.1\bin”
OR (I think that is “cleaner”) copy it to your ruby-bin folder, in my
case “C:\ruby\bin”. Both works.