Error updating Mysql2 Gem on Windows 7 with Rails 3.1 and Ruby 1.9.2p290

When trying to upgrade to Rails 3.1 on my development system I ran into an error updating the MySQL2 gem.  After a bit of searching this turned out to be a known issue with win23 and the MySQL2 gem.   The error I received was:

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:/ruby/bin/ruby.exe extconf.rb
checking for rb_thread_blocking_region()... yes
checking for main() in -llibmysql... 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:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=C:/ruby/bin/ruby
        --with-mysql-dir
        --without-mysql-dir
        --with-mysql-include
        --without-mysql-include=${mysql-dir}/include
        --with-mysql-lib
        --without-mysql-lib=${mysql-dir}/lib
        --with-libmysqllib
        --without-libmysqllib


Gem files will remain installed in C:/ruby/lib/ruby/gems/1.9.1/gems/mysql2-0.3.7
 for inspection.
Results logged to C:/ruby/lib/ruby/gems/1.9.1/gems/mysql2-0.3.7/ext/mysql2/gem_m
ake.out

c:\>

FIRST – Be sure to install DevKit from RailsInstaller.org.  It is a set of .bat and .dll files you will need to place in your the bin directory of your ruby installation.  In my case my Ruby was installed at ‘c:\ruby’ so I dropped the files from DevKit in the ‘c:\ruby\bin’ directory.

Once you have DevKit installed go ahead and install an older version of the MySQL gem.  I am currently using version 0.2.6 which is working just nicely with Rails 3.1.

At the command prompt run the following command:

c:\>gem install mysql2 -v 0.2.6

The MySQL2 gem should install without a problem.

c:\>gem install mysql2 -v 0.2.6
Successfully installed mysql2-0.2.6-x86-mingw32
1 gem installed
Installing ri documentation for mysql2-0.2.6-x86-mingw32...
Enclosing class/module 'mMysql2' for class Client not known
Installing RDoc documentation for mysql2-0.2.6-x86-mingw32...
Enclosing class/module 'mMysql2' for class Client not known

c:\>

Now you should be all sent using Rails 3.1 and the MySQL2 gem.  Just an older version.  Once win32 is fully supported this work-around will no longer be necessary.

Note: There is another way to do this by specifying the library and includes.  That method did not work for me however if it works for you it may be worth a shot.