I was trying to install Ruby’s mysql2 gem on my Windows 7 computer, running MySQL 64-bits. It turns out that the mysql2 gem is not compatible with MySQL 64-bits libmysql.dll file. The solution is building the gem against the 32-bits version of the MySQL library, which thankfully turns out to be compatible.

When installing the Ruby mysql 2 gem with MySQL 64-bits installed on your system, you get something like:

	
c:>gem install mysql2 -- with-mysql-include=x:include
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:/Ruby192/bin/ruby.exe extconf.rb with-mysql-include=x:include
checking for rb_thread_blocking_region()... yes
checking for main() in -llibmysql... yes
checking for mysql.h... yes
checking for errmsg.h... yes
checking for mysqld_error.h... yes
creating Makefile
 
make
C:/Ruby192/bin/ruby -e "puts 'EXPORTS', 'Init_mysql2'"  > mysql2-i386-mingw32.def
gcc -I. -IC:/Ruby192/include/ruby-1.9.1/i386-mingw32 -I/C/Ruby192/include/ruby-1.9.1/ruby/backward -I/C/Ruby192/include/ruby-1.9.1 -I. -DHAVE_RB_THREAD_BLOCKING_REGION -DHAVE_MYSQL_H -DHAVE_ERRMSG_H -DHAVE_MYSQLD_ERROR_H -Ix:include -O3 -g -Wextra -Wno-unused-parameter -Wno-parentheses -Wpointer-arith -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wall -funroll-loops  -o client.o -c client.c
gcc -I. -IC:/Ruby192/include/ruby-1.9.1/i386-mingw32 -I/C/Ruby192/include/ruby-1.9.1/ruby/backward -I/C/Ruby192/include/ruby-1.9.1 -I. -DHAVE_RB_THREAD_BLOCKING_REGION -DHAVE_MYSQL_H -DHAVE_ERRMSG_H -DHAVE_MYSQLD_ERROR_H -Ix:include    -O3 -g -Wextra -Wno-unused-parameter -Wno-parentheses -Wpointer-arith -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wall -funroll-loops -o mysql2_ext.o -c mysql2_ext.c
gcc -I. -IC:/Ruby192/include/ruby-1.9.1/i386-mingw32 -I/C/Ruby192/include/ruby-1.9.1/ruby/backward -I/C/Ruby192/include/ruby-1.9.1 -I. -DHAVE_RB_THREAD_BLOCKING_REGION -DHAVE_MYSQL_H -DHAVE_ERRMSG_H -DHAVE_MYSQLD_ERROR_H -Ix:include    -O3 -g -Wextra -Wno-unused-parameter -Wno-parentheses -Wpointer-arith -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wall -funroll-loops  -o result.o -c result.c
result.c: In function 'rb_mysql_result_fetch_fields':
result.c:376:35: warning: comparison between signed and unsigned integer expressions
gcc -shared -s -o mysql2.so client.o mysql2_ext.o result.o -L. -LC:/Ruby192/lib -L. -LC:\Users\Luis\Projects\oss\oneclick\rubyinstaller\sandbox\tk\lib -LC:\Users\Luis\Projects\oss\oneclick\rubyinstaller\sandbox\tcl\lib -LC :\Users\Luis\Projects\oss\oneclick\rubyinstaller\sandbox\libyaml\lib -L
C:\Users\Luis\Projects\oss\oneclick\rubyinstaller\sandbox\pdcurses\lib -LC:\Users\Luis\Projects\oss\oneclick\rubyinstaller\sandbox\openssl\lib  -LC:\Users\Luis\Projects\oss\oneclick\rubyinstaller\sandbox\zlib\lib -LC:\Users\Luis\Projects\oss\oneclick\rubyinstaller\sandbox\iconv\lib -L
C:\Users\Luis\Projects\oss\oneclick\rubyinstaller\sandbox\gdbm\lib -LC:\Users\Luis\Projects\oss\oneclick\rubyinstaller\sandbox\libffi\lib  -Wl,--enable-auto-image-base,--enable-auto-import mysql2-i386-mingw32.def  -lmsvcrt -ruby191 -llibmysql  -lshell32 -lws2_32
client.o: In function `nogvl_connect':
C:Ruby192librubygems1.9.1gemsmysql2-0.3.6extmysql2/client.c:114: undefined reference to `mysql_real_connect@32'
client.o: In function `nogvl_init':
C:Ruby192librubygems1.9.1gemsmysql2-0.3.6extmysql2/client.c:105: undefined reference to `mysql_init@4'
client.o: In function `set_ssl_options':
C:Ruby192librubygems1.9.1gemsmysql2-0.3.6extmysql2/client.c:700: undefined reference to `mysql_ssl_set@24'
collect2: ld returned 1 exit status
make: *** [mysql2.so] Error 1
Gem files will remain installed in C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.3.6 for inspection.
Results logged to C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.3.6/ext/mysql2/gem_make.out

In other words, yet get a bunch of undefined references to functions that should exist in libmysql.dll. Except they don’t, because you’re running MySQL 64-bits and its functions have a different signature.

The solution is this: get the installation files for MySQL 32-bits (same version as yours), and copy the libmysql.dll file to your Ruby installation’s library directory (C:\Ruby192lib, for instance). Now linking will succeed.