Friday, June 3, 2011

Getting mysql2 gem to work with Ruby on Rails 3.0 and Windows 7 64bit

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:
Assuming you have all this already installed, here are the steps you need to follow:
  1. 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.
  2. Uninstall all previous version of the mysql2 gem just in case. e.g. gem uninstall mysql2 -a
  3. 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.
  4. 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
  5. 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
  6. 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).
  7. Then type: gem install mysql2-0.2.7.gem --platform=ruby -- --with-mysql-dir=X: --with-mysql-lib=X:\lib
  8. 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!

5 comments:

  1. Hi I'm new to Ruby on Rails and watching some the new Ruby on Rails 3.0 training and got stuff with installing mysql2. I've followed all your instructions but i don't now how to use the Git Bash on step 5. to the build the gem.

    ReplyDelete
  2. Hi Lanesa,

    You can download Git Bash here: http://git-scm.com/.

    Then when you open it up type: "cd C:\Ruby192\lib\ruby\gems\1.9.1\gems\mysql2-0.2.7" to go to that directory or wherever you put the folder. Then you should be able to type "gem build mysql2.gemspec".

    Let me know if you need any more help, and good luck!

    ReplyDelete
  3. The following changes will allow you to create the .gem file without having to rely on git-bash

    require File.expand_path('../lib/mysql2/version', __FILE__)

    Gem::Specification.new do |s|
    s.name = %q{mysql2}
    s.version = Mysql2::VERSION
    s.authors = ["Brian Lopez"]
    s.date = Time.now.utc.strftime("%Y-%m-%d")
    s.email = %q{seniorlopez@gmail.com}
    s.extensions = ["ext/mysql2/extconf.rb"]
    ignores = File.readlines(".gitignore").grep(/\S+/).map {|s| s.chomp }
    dotfiles = [".gitignore", ".rspec"]
    s.files = Dir["**/*"].reject {|f| File.directory?(f) || ignores.any? {|i| File.fnmatch(i, f) } } + dotfiles
    s.homepage = %q{http://github.com/brianmario/mysql2}
    s.rdoc_options = ["--charset=UTF-8"]
    s.require_paths = ["lib", "ext"]
    s.rubygems_version = %q{1.4.2}
    s.summary = %q{A simple, fast Mysql library for Ruby, binding to libmysql}
    s.test_files = s.files.grep(/^spec\//)

    # tests
    s.add_development_dependency 'eventmachine'
    s.add_development_dependency 'rake-compiler', "~> 0.7.7"
    s.add_development_dependency 'rspec'
    # benchmarks
    s.add_development_dependency 'activerecord'
    s.add_development_dependency 'mysql'
    s.add_development_dependency 'do_mysql'
    s.add_development_dependency 'sequel'
    s.add_development_dependency 'faker'
    end

    Thanks for your post, helped a bunch.

    ReplyDelete
  4. how to uninstall mysql2 gem on windows 32bits ?

    ReplyDelete