Edit: For Rails 3.1, these same steps should still work. I had some issues at first, but try uninstalling (gem uninstall mysql2 -a) all previous instances of the mysql2 gem before installing the latest one.
The mysql2 gem recently added support for Windows (https://github.com/brianmario/mysql2/issues/8), but it sure was a pain in the ass to install. Hopefully this blog post will help save you the time and swearing that this gem has caused me. I had a lot of issues just trying to get the gem to talk with my MySQL server, and it seemed like there was some issues with using the 64bit version of MySQL server, so I eventually downgraded to the 32bit version. Also, you cannot just do a bundle install or gem install mysql2 to get this gem installed. Lastly, once I finally got the dam gem installed, it would always hang when I tried to use it (e.g. rake db:create), and I found that there's an issue on github here: https://github.com/brianmario/mysql2/issues/142, but I was able to still make it work.
First off, here are the versions of my software:
- Ruby 1.9.2p180 (2011-02-18) [i386-mingw32] installed from RubyInstaller
- RubyInstaller's DevKit: DevKit-tdm-32-4.5.1-20101214-1400-sfx.exe
- Rails 3.0.7 or 3.0.8RC2
- MySQL Server 5.5 32 bit (It's really important that you install the 32 bit version of MySQL because the 64 bit version doesn't play nice with RubyInstaller. Refer to here for more information: https://github.com/oneclick/rubyinstaller/wiki/Troubleshooting#gems_mix_bits
Assuming you have all this already installed, here are the steps you need to follow:
- Copy C:\Program Files (x86)\MySQL\MySQL Server 5.5\lib\libmysql.dll to C:\Ruby192\bin. I think also if you just add C:\Program Files (x86)\MySQL\MySQL Server 5.5\lib\ to your environment variables it might work too.
- Uninstall all previous version of the mysql2 gem just in case. e.g. gem uninstall mysql2 -a
- Go to the mysql2 github page and select the 0.2.x branch, then click the Downloads button to download the gem (or you can go here: https://github.com/brianmario/mysql2/zipball/0.2.x). We're going to have to manually compile and install the gem. The latest mysql2 version 0.3.x only works with Rails 3.1 and not 3.0.
- Unzip in to a folder called mysql2-0.2.7 in your Ruby installation folder here: C:\Ruby192\lib\ruby\gems\1.9.1\gems, so that afterwards it looks like: C:\Ruby192\lib\ruby\gems\1.9.1\gems\mysql2-0.2.7
- Now load up Git Bash (I couldn't get this to work in regular Command Prompt) and go to C:\Ruby192\lib\ruby\gems\1.9.1\gems\mysql2-0.2.7 and type: gem build mysql2.gemspec
- Now you should have a file called mysql2-0.2.7.gem in your directory. Let's install the gem now. First type: subst X: "C:\Program Files (x86)\MySQL\MySQL Server 5.5" (or wherever your MySQL Server is installed).
- Then type: gem install mysql2-0.2.7.gem --platform=ruby -- --with-mysql-dir=X: --with-mysql-lib=X:\lib
- Congrats, you're done now! We can test it by typing rake db:create or rake db:migrate in one of your projects. If all went well you shouldn't have gotten any errors. Good luck!