Ruby libmysql.dll or mysql_api.so error

Installing Ruby MySQL module is pretty straight forward either on *nix or Windows by running

> gem  install  mysql

But on Windows when you try to import the module through “require mysql” you may get an error like

C:/Ruby/lib/ruby/gems/1.8/gems/mysql-2.8.1-x86-mswin32/lib/1.8/mysql_api.so: 126: The specified module could not be found.   - C:/Ruby/lib/ruby/gems/1.8/gems/mysql-2.8.1-x86-mswin32/lib/1.8/mysql_api.so (LoadError)
# from C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
...
...

In that scenario, you are missing “libmysql.dll” in the system32 directory.  In my case I had MySQL server copy and had the dll in folder C:/Program Files/MySQL/MySQL Server 5.4/lib/opt

From cygwin window do

> cp  /cygdrive/c/Program\ Files/MySQL/MySQL\ Server\ 5.4/lib/opt/libmysql.dll   /cygdrive/c/WINDOWS/system32/libmysql.dll

Now run your Ruby db test program

require "mysql"

begin
dbh = Mysql.real_connect("db_server_host", "user_name", "password", "database_name")
puts "Server version:" + dbh.get_server_info
rescue Mysql::Error => err
puts "Error code: #{err.errno}"
puts "Error: #{err.error}"
ensure
dbh.close if dbh
end

HTH,
Shiva

One thought on “Ruby libmysql.dll or mysql_api.so error

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s