MySQL/Ruby for Windows

MySQL/Ruby is a native MySQL database adapter for Ruby by Tomita Masahiro, delivering:

What I provide here is a distribution for Windows. It was included by default in Instant Rails 1.2 but was taken out in 1.3a because of recurring segfaults on Windows 2000. After having fixed these segfaults, Instant Rails has included it once again.

Background

Getting MySQL/Ruby up and running on Windows used to be a bit of a hassle. Several projects presented solutions but many of them went stale and didn’t provide concise building instructions, so I figured I started maintaining a project of my own. And, unlike some other distributions, this one doesn’t require any additional runtime libraries.

Solving the Segfaults on Windows 2000

Windows MySQL/Ruby was plagued by recurring segfaults on Windows 2000 in the spring of 2006. After much debugging and conversation with the Ruby community, reader Tobias Grimm provided a patch that finally solved the issue. It has been included in the 2.7.1 release. Many thanks, Tobias!

Fixing the garbage creation issue

Stefan Kaes discovered that MySQL/Ruby creates a lot more garbage than it is supposed to, and this distribution is the first to incorporate that fix in an optional performance version of the adapter. Kudos to Stefan for his continuing and amazing quest to improve Ruby on Rails performance.

Installation

Installation is easy. Just grab a small download and follow three simple steps.

Download

These packages contain libraries for use with Ruby 1.8.5 and MySQL 4.1 and up (earlier versions may work but are untested and unsupported). They have been compiled using Visual Studio C++ 6.0, optimizing for speed and modern CPUs. Follow the instructions below if you need to compile a custom version.

Setup

  1. Select and copy the .so file of your choice to %RUBYARCHDIR% and rename it to mysql.so (e.g. C:\ruby\lib\ruby\site_ruby\1.8\i386-msvcrt\mysql.so).

    • The 4.1 and 5.0 directories correspond to your MySQL server version. (For all you tech savvies: they are generally intercheangable, but differ slightly on byte-level in terms of argument types.)

    • The ‘vanilla’ library is the stock MySQL/Ruby 2.7.3 we all know and love. The ‘performance’ library has Stefan Kaes’ garbage correction patch applied that can increase performance anywhere from 5% to 30% depending on the size of the data set you’re accessing. The flip-side is this: it is less tested, so take your pick!

  2. MySQL/Ruby needs a DLL dependency satisfied in order to run. You can do this by either:

    • (Recommended) Including the MySQL bindir in your path (e.g. C:\Program Files\MySQL\MySQL Server 5.0\bin).

    • Copying libmysql.dll from your MySQL bindir or source package to your Ruby bindir (e.g. C:\ruby\bin).

    Instant Rails users: You will find that the libmysql.dll from the included PHP distribution won’t work. Instead, grab a libmysql.dll from a real MySQL distribution. InstantRails 1.3a comes with MySQL 4.1.9, which you can download here.

  3. To verify, try to run the following code:

    require 'mysql'
    puts Mysql::VERSION

    If it outputs 20703, then you’re all set up! Just be sure that you’re MySQL server has TCP/IP connections enabled.

Donations

If this solution helps you in your business or your professional life, please consider getting me an item off of my Amazon wish list. Donations ensure you that I can continue support MySQL/Ruby for Windows and cut me a fair share of your profits. And at about $20 and below, that’s a bargain.

Rolling Your Own Release

If you’d like to roll a Windows release of your own, perhaps compiled against a different version of MySQL or Ruby or with debug symbols included, here’s how to.

Prerequisites

You’ll need to have the following set up:

  • Microsoft Visual C++ 6.0 with Service Pack 6. Using newer versions will result in compatibility issues and recurring segfaults. There have been talks in the Windows Ruby community about upgrading to Visual Studio 2005, but that will certainly not happen before the fall of 2006 at the very earliest and possibly much later.

  • MySQL (be sure to install the development libraries and headers)

  • Ruby for Windows

  • MySQL/Ruby

  • Optionally, Stefan Kaes’ garbage correction patch. You will need to perform the patch manually as Stefan’s patch is against an earlier version of MySQL/Ruby.

Unpack the MySQL/Ruby tarball to a location of your choosing and install the rest.

Prepping and Building

With everything set up to go, we will need to set a few things up for a build to succeed. Assuming you installed Ruby to C:\ruby and are compiling against MySQL 5.0:

  1. Fire up Visual C++ and create an empty Win32 Dynamic-Link Library project.

  2. Rename mysql.c.in to mysql.c and add it to your Visual C++ project.

  3. Open up the Visual C++ project settings, and set the following configuration properties in the appropriate configuration (I recommend setting “All Configurations”):

    • C/C++ > Processor: Pick the one you’re targeting lest you’ll find yourself with a woefully underoptimized library. You will usually want to select “Pentium Pro” as that represents anything newer than a plain old Pentium.

    • C/C++ > Code Generation > Use run-time library: Multithreaded DLL

    • C/C++ > Optimizations > Optimizations: choose as you see fit, but you must ensure that “Frame-Pointer Omission” is disabled. Ruby’s garbage collector needs frame pointers or random segfaults will occur.

      My distribution is optimized as follows: Global Optimizations, Favor Fast Code. Inline function expansion: Only __inline

    • C/C++ > Preprocessor > Preprocessor definitions: add HAVE_MYSQL_H to the list (comma-separated).

    • C/C++ > Preprocessor > Additional include directories: C:\ruby\lib\ruby\1.8\i386-mswin32,C:\Program Files\MySQL\MySQL Server 5.0\include

    • Link > Input > Object/library modules: add msvcrt-ruby18.lib libmysql.lib to the list (space-separated).

    • Link > Input > Additional library path: C:\ruby\lib,C:\Program Files\MySQL\MySQL Server 5.0\lib\opt

    • Link > Project Options: add /EXPORT:Init_mysql to the command line. Be sure to explicitly pick a configuration or this textfield will be greyed out.

  4. If compiling against MySQL 5.0.27: to the top of mysql.c add

    #define ulong unsigned long

  5. Now build the project.

If all went well, you should end up with a DLL file in the output folder you configured. Follow the regular installation instructions, and you’re all set with your home-built MySQL/Ruby!

Feedback

Words of praise? Doesn’t work for you? All of that and more is welcome at my address. Please leave your feedback at a suitable newsitem on my blog. And if this helps you in your business or professional life, please also consider getting me an item off of my Amazon wish list. Donations ensure you that I can continue support MySQL/Ruby for Windows and cut me a fair share of your profits.