So, I got MySQL to work for my Ruby on Rails development environment. Let’s be clear about what I actually wanted:

  • Ruby 1.9.1
  • Rails 2.3.2
  • Any version of MySQL whatsoever
  • Any way whatsoever to make my up-to-date Ruby on Rails talk to that MySQL

I don’t care what version of MySQL. I don’t care if I have to force platforms for Gems or recompile or whatever. I never did care, and I’ll happily even dig through code to make manual changes before compiling or what-have-you. What I do care about is actually getting this to work, so I can finally stop tinkering and start doing something productive.

Well, I learned a lot on my way there. I had a glimpse at how extconf.rb works (because I had to figure out correct parameters), I dug through rails sources to find errors that you don’t even want to know about, had another good look at makefiles, and generally tried to understand what exactly didn’t work.

Also, there is a more efficient way to do this, as pointed out by Luis in the comments. Thanks!

So, in the end, it now works

This tutorial blog post on Code Elite was very, very helpful. So were Luis comments here and elsewhere (although me being quite the newbie when it comes to gcc and the likes, they went a bit over my head).

The Code Elite post is not the best at being copy-paste-able though, the formatting kinda ruins that. It does point out the correct steps to getting MySQL to work in Ruby (and consequently, Rails).

Long story short, these are the steps that lead to a working MySQL integration:

  1. Download Ruby 1.9.1 MinGW32 and the corresponding developer kit from the RubyInstaller download page. I prefer the zipped version to the one with installer, to be honest, but that’s just me.
  2. Get the 32-bit version (not 64-bit, or linking won’t work) of MySQL 5.0 (not 5.1, or nothing will work) from the MySQL download page. Again, I prefer the zipped version.
  3. Get the current mysql-ruby source (2.8.1, currently) from its FTP.
  4. Put Ruby into F:\Ruby or your path of preference. Add Ruby’s \bin path to your path variable. Close and re-open the command window to update the path.
  5. Install Rails – this is easy.
    gem install rails
  6. Put MySQL into F:\mysql (or your path, again). No adding to the path yet here.
  7. Put the contents of the mysql-ruby package into your Ruby path (I didn’t try other paths) and point your command line there, then run this:
    ruby extconf.rb --with-mysql-lib=f:/mysql/lib/opt --with-mysql-include=f:/mysql/include
  8. Run make, then make install. Here is where 64-bit MySQL fails.
  9. Add the following to your path variable (substituting your MySQL location): F:\mysql\lib\opt;F:\mysql\bin. Again, close and re-open the command window to update the path.
  10. Run this to let Ruby know MySQL:
    ruby -e 'require "mysql"'
  11. Get into a command prompt as admin (you might have to right-click a command prompt and “run as admin” to get to this) to install and start the MySQL service. To do that, run these in the mysql\bin directory:
    mysqld-nt --install
    net start mysql
  12. Now that the MySQL server is actually running, you can connect to it to change your root password:
    mysqladmin -u root password topsecretpassword

Steps 1-3 are just downloads. Steps 4 and 5 already installs Ruby on Rails completely. Steps 6-12 then are for installing mysql (which is necessary first for the DLLs to be available to the compiler later) and then compiling the mysql-ruby package (driver?) from scratch into Ruby.

Interestingly, step 7 builds the makefile through Ruby (mostly, with some automated C mixed in), while step 8 actually compiles the C source through make and step 10 plugs this into Ruby. Steps 11 and 12 merely configure the MySQL server.

This should be it. And this time, I hope I did no forbidden or ugly things :) For they know not what they do and stuff.