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

1 comment:

  1. Sometime you can watch some kind of dll error in your computer. Don't be afraid, this problem is solved, you just need this tool : http://fix4dll.com/msvcp140_dll It make the way of solution much easier.

    ReplyDelete