Recently I reinstalled by entire Windows installation due a SSD migration and wanted to take the opportunity to document the configuration process, specially in the light of the known issues with making it work with Ruby.
It is important to mention that most of these issues in Ruby land are generated due the lack of documentation associated on how to properly install MySQL and its related dependencies.
Most of us expect things to Just Work, and we often compare the installation process to installing to Linux or OSX which works out of the box.
What is not mentioned in the documentation installation is that development tools (a compiler) and specific headers and libraries are required. Unless you’ve already installed them, these development artifacts are usually missing from a Windows computer.
What I’m going to describe next is how I installed and configured my environment and how I made it work properly. YMMV if you decided to use different versions of the components I used for this.
Let’s get started:
Download the right version of MySQL
As I mentioned before, I’m using a 64bits version of Windows, so I think it will be best if I download a matching bits version.
So went ahead and visited MySQL download site:
http://dev.mysql.com/downloads/mysql
And selected MySQL Community Server 5.5.13.
From the versions offered, downloaded Windows (x86, 64-bit), MSI Installer
Install MySQL
Invoked the installer and presented with the normal installation wizard.
You can follow the next sequence of images to use my same settings.
Is good to always install your Data files outside the default program installation directory, that way, you can safely upgrade your installation and not to worry about an installer removing your data files.
Ok, so let’s move to the next thing…
Before installing MySQL/Ruby bindings
Update: mysql gem version `2.9.0` already fix the issues shown here. Install it normally and follow the on-screen instructions.
In order to use my brand new MySQL installation, now I need to install the MySQL bindings for it.
But, there is a small detail: Ruby is 32bits and my MySQL is 64bits, this means I can’t use MySQL provided libraries from Ruby.
Bummer! You told me to install the 64bits version!
Don’t despair! MySQL Connector to the rescue!
That is right, MySQL has something called Connector, the purpose of that library is to avoid a complete MySQL installation when you just need to connect to a remote one.
It comes in different flavors, we are interested in C language support, since that is the language Ruby uses for it’s extensions.
We are going to download a 32bits connector and use it!
So, at my web browser again, decided to visit the MySQL Connector/C download page:
http://dev.mysql.com/downloads/connector/c/
Since I’m not interested in installing this Connector and pollute my clean 64bits installation, I’m going to download the non-installer version.
I scrolled down the listing until I saw the noinstall 32bits version:
mysql-connector-c-noinstall-6.0.2-win32.zip
Decided to extract it to the root of my disk, so I ended with a folder named mysql-connector-c-noinstall-6.0.2-win32 in there.
Remember: extract into a folder without spaces. The same goes for your Ruby installation and the DevKit installation.
Time to install MySQL/Ruby bindings
So, now that all MySQL prerequisites are in place, will open a new command prompt and prepare to install the gem.
This time I’m going to use Ruby 1.9.2, properly installed and configured with the complementary Development Kit (DevKit) which is provided at RubyInstaller website (In case you haven’t installed yet, don’t forget to follow the installation instructions in the wiki)
OK, so in a Command Prompt, will type the gem installation command:
gem install mysql --platform=ruby -- --with-mysql-dir=C:/mysql-connector-c-noinstall-6.0.2-win32
Note the use of forward slashes for the directory where MySQL Connector/C was extracted.
The above command contains two special things:
First, we are telling RubyGems that we want the ruby platform of mysql gem. This particular platform is the one that contains the source code and this will allow us to skip the pre-compiled version of the gem.
The second part, which is added after two dashes, are the additional arguments that we are giving to the gem configuration process to locate our MySQL headers and libraries for successful compilation.
As result of this command, you will see something like this:
Fetching: mysql-2.8.1.gem (100%)
Temporarily enhancing PATH to include DevKit...
Building native extensions. This could take a while...
Successfully installed mysql-2.8.1
1 gem installed
Which indicates the gem installed successfully.
In case you obtained a different result, please refer to RubyInstaller Troubleshooting page:
https://github.com/oneclick/rubyinstaller/wiki/Troubleshooting
And try the proposed solutions there.
Using the bindings
Now that we installed the gem, we can remove the connector folder we first extracted. Before do that, first we need to take out a file from there: libmysql.dll. This file is required by the gem we compiled and needs to be available for it.
You can find it inside the lib directory of MySQL Connector.
I personally recommend you place it along your Ruby installation, inside the bin directory.
If you have multiple Ruby installations and you use Pik to change between them, you can place the library in the same directory Pik is installed. You need to remember that it is important the libmysql.dll file is on the PATH when you need to use it.
OK, after all that big red warning, let’s test this thing on a IRB console:
irb> require "rubygems"
irb> require "mysql"
irb> conn = Mysql.connect "localhost", "root", "abc123"
irb> result = conn.query "SELECT 1"
irb> result.num_rows
=> 1
irb> result.fetch_row
=> ["1"]
irb> result.free
irb> conn.close
irb> exit
Great!, now you have not just a working MySQL installation but also Ruby configured to talk to it!
Hope you enjoyed this post as I did enjoy creating it. Hope this ease your path on using MySQL with Ruby on Windows.


















Thank you Claudio.
The instructions works the same for mysql2 gem, except the IRB session example.
You just need to change gem install mysql to gem install mysql2
All the other details are the same.
Cheers!
Thank you! This worked totally flawlessly the first time around. I thought this was going to be a major pain but turned out to be pretty easy. Totally new to rails and hate having to troubleshoot at the beginning
Love Your Work! After wasting hours on stackoverflow, your post was a savor! Indeed, the best solution is to compile it locally on my machine. Thanks!
How can I add the gem install command to my Gemfile instead ? I’m using the bundle install command to install gems locally.
I need to put this: gem install mysql –platform=ruby — –with-mysql-dir=C:/mysql-connector-c-noinstall-6.0.2-win32
into my Gemfile somehow. Any thoughts?
awesome. thank you!
Worked like a charm. After looking around everywhere, I was able to finally get latest MySQL 5.6 working with Ruby 1.9.3, Rails 3.2.2, and Windows 7 64-bit with 32-bit MySQL connector libmysql.dll. Thanks for the great post.
Perfect!!!
I had an existing Ruby install (1.8.7) and had some issues getting this to work, after adding Ruby 1.9.3. Below are some of the error messages I was receiving:
…undefined reference to `mysql_stmt_sqlstate…
ERROR: Failed to build gem native extension.
If you’re running into these issues try this:
1. Uninstall all Ruby installations
2. Delete the Ruby installation folders (the uninstall process didn’t delete mine)
3. Delete the Development Kit directory (This step may not be required)
4. Follow the steps from this article.
(NOTE: in my case I didn’t have to re-install MySQL, just the Ruby installer and Devkit)
Everything is working great now! Thanks for this article.
Hi Thanks a ton .. helped me to get through the mysql drive issue .. u rock man
Worked perfectly for me too. Thanks a million.
Nice one, 2 hours wasted elsewhere. Thanks worked first time.
Much thanks! greatly appreciate the clearly written article
[...] http://blog.mmediasys.com/2011/07/07/installing-mysql-on-windows-7-x64-and-using-ruby-with-it/ [...]
Thanks, some hours of hard work but I was good to go
I’m a Ruby n00b on Windows. Excellent tutorial! Thank you so much.
tks guy, it’s worth to me!
Thanks!!!!
Awesome information. After I completed your instruction I generated the rails app, but I noticed the gemfile is mapping to sqlite3 not mySql. Is it possible to map the gemfile to mysql?
Thanks
I resolved the sqlite3 issue, I did not generate the rails app with proper instruction in command line.. thanks for the instruction it works perfect
Worked for me. Thank you!
Thanks very much. Your article is very helpfull for me.
I started test of connection as shown in paragraph ‘Using the bindings’ and something is wrong in my installation.
> require “mysql” => true
> require “rubygems” => false
Why?
Please can anybody help me?
I reinstall RoR, MySQL, gem mysql2.
If I want to create db
> rake db:create
error is ‘I can’t connect to database’
could you tell me where may be mistake?
I try it second day and without your help I will give up.
thank’s for any help.
[...] blog here has a great step by step guide to set up your environment if you are not using xampp and a lot the [...]
Thanks dude.. I’ve been searching the solution… It worked…
Thanks again man
Thanks !!!
For those of you who are getting issues connecting.
For me: I used mysql2 with the above instructions.
Also make sure to point your database.yml file to the mysql2 adapter and use 127.0.0.1 instead of localhost.
Cheers dude, this worked an absolute treat, spent a few hours scratching my head now I can continue learning.
Peace
Super helpful! I killed days trying to fix one problem after another with MySQL 5.5 and Rails 3. Your post kept me from bailing on the whole mess and going back to PHP!
Beautiful, thanks!
Worked like charm. Thanks a ton
Worked really well, with one caveat. Can’t say I’m 100% sure why, but the gem would not install unless I did so locally — see below. I’m posting this in case anyone else encounters the same error.
When your instructions say to run
“gem install mysql –platform=ruby — –with-mysql-dir=C:/mysql-connector-c-noinstall-6.0.2-win32″
I had to download mysql 2.8.1-x86-mingw32 and install from my local directory.
“gem install C:/full/path/to/mysql-2.8.1-x86-mingw32.gem –platform=ruby — –with-mysql-dir=C:/mysql-connector-c-noinstall-6.0.2-win32″
Thank you!
@Sam:
You need to use the pure ruby version because the x86-mingw32 is
already compiled…
gem install mysql –platform=ruby — –with-mysq-dir=…
Perhaps you have problems installing gems, which is other problem.
Thanks, this solution allowed me to connect to WEBrick, and thanks Luis for confirming that it should be mysql2.
Thank you for this solution, it was exactly my situation!
Hey, I cannot make it work. I’m using the Aptana Studio 3 ( on windows 7 ultimate ). All the installs went perfectly, no error, but still get the same error…
Thank you for this solution. Work with Window Server 2008-R2 (Standard).
Awesome man awesome post…After wasting days i was about to quit learning rails…your post is a killer! I am so thrilled and happy. Thanks for the great work i will never forget your help..You saved it man!
Priceless post again!
Worked like clockwork! Everything was precise and hilarious and helped lighten up my day if not week
. Just as a recommendation: Add what someone is supposed to do next once all the configurations are behind his/her back. Again, THANK YOU SIR!!!!
Yes, Its working !! Tons of Thanks
thank you man.
i was searching for half a day
worlds of thanks buddy
wasted most of the day trying to get ruby working with mysql before i found your solution. it works great. many thanks.
worked!
Thanks!
One more thing.
Replace the test line
conn = Mysql.connect “localhost”, “root”, “abc123″ with
conn = Mysql.connect “127.0.0.1″, “root”, “abc123″
so you do not have issues when IPV6 is enabled in the server.
Yes ! it works perfectly, thank you for your awesome post………..
Thanks, I was not able to get it to work without specifying the lib and include separately instead of the just the mysql dir.
Using Windows 7 ×64 bit, ruby 2.0.0p0 (2013-02-24) [i386-mingw32], mysql 2.9.1
Ended up doing:
gem install mysql –platform=ruby — –with-mysql-lib=C:\mysql-connector-c-noinstall-6.0.2-win32\lib –with-mysql-include=C:\mysql-connector-c-noinstall-6.0.2-win32\include
Thanks a lot, great job!!!
Only
gem install myqlsl2 …………the same……….
In ……..\config\database.yml (Do not use any tabs for edit params):
host : 127.0.0.1 #instead localhost
Good luck everybody!!!!!!!!!!
[...] blog.mmediasys.com/2011/07/07/… [...]