Thursday, September 11, 2014

Rails on Windows, Part 3/4: MySQL2 gem

Installing mysql2 gem on Windows 8 with Ruby2

This is what worked for me. Hopefully it’ll help someone else.
  • Uninstall the 64 bit version of Ruby 2 (apparently, there are lot of libraries that haven’t been tested against 64 bit Ruby on Windows and it can lead to errors) 
  • Download the latest 32 bit Ruby 2 version from here and install 
  • Download the 32 bit version of the DevKit from the same link as above and follow these instructions to have it enhance your previously installed Ruby version 
  • Download the 32 bit MySQL C connector (archive version) from here and unzip it to C:/mysql 
  • Open a command prompt and enter gem install mysql2 --platform=ruby -- '--with-mysql-dir="C:/mysql" 
  • Copy the file “libmysql.dll” from C:/mysql/lib to your Ruby bin folder 
  • Optional: delete C:/mysql
Reference: https://github.com/oneclick/rubyinstaller/issues/191

Note: This solution not always works depending on the environment bit version.
Some other issues are noted below that could help.

(http://stackoverflow.com/questions/19014117/ruby-mysql2-gem-installation-on-windows-7)

  1. Download the lastest MySQL Installer for windows 7 32 bits
  2. Installed the gem with the following command: gem install mysql2 --platform=ruby -- '--with-mysql-dir="C:/Program Files/MySQL/MySQL Connector C 6.1 6.1.2/"'
One pitfall to be aware of is that I changed the backslashes (\) to normal slashes (/). I've tried the same procedure with backslashes and it didn't work.
The installer already includes the C connectors for MySQL at MySQL Connector C 6.1 6.1.2 directory. Therefore, passing only the --with-mysql-dir parameter without the --with-mysql-lib or --with-mysql-include parameters, makes the gem to look at the same directory for the lib and includedirectories
Another source of problem could be using 64 bit connector instead of 32 bit.
This may be evident from log files, like here:

http://stackoverflow.com/questions/17866003/rails-installing-mysql-error-installing-mysql2-error-failed-to-build-gem-nat

This is lastily what helped me after tons of work trying to compile the gem:
Download 32 bit version connector and follow instruction like here:

https://medium.com/@frontlineutils/installing-the-mysql2-rubyonrails-gem-on-windows-7-8-a028f44d87f3

(Optional relevant link:
http://stackoverflow.com/questions/5732546/mysql2-gem-install-make-error-windows-7)

And the last solid solution is just:

gem install mysql2 -v '0.3.11'

In this case you will get the folowing message after successfull installation:


======================

  You've installed the binary version of mysql2.
  It was built using MySQL Connector/C version 6.0.2.
  It's recommended to use the exact same version to avoid potential issues.

  At the time of building this gem, the necessary DLL files where available
  in the following download:

  http://dev.mysql.com/get/Downloads/Connector-C/mysql-connector-c-noinstall-6.0
.2-win32.zip/from/pick

  And put lib\libmysql.dll file in your Ruby bin directory, for example C:\Ruby\
bin


Don't forget to pick up corresponding dll for that connector version.

Powered by Blogger.