Thursday, March 31, 2011

Installing mysql2 gem on Ruby 1.9.2 and Rails 3.0.5 on Windows


When running 'bundle install' or 'gem install mysql2', I got following error:
>gem install mysql2
ERROR:  Error installing mysql2:
        The 'mysql2' native gem requires installed build tools.

Please update your PATH to include build tools or download the DevKit
from 'http://rubyinstaller.org/downloads' and follow the instructions
at 'http://github.com/oneclick/rubyinstaller/wiki/Development-Kit'
 To solve this issue and to successfully install mysql2 gem, I took following steps:

1. Download Development Kit "http://github.com/downloads/oneclick/rubyinstaller/DevKit-tdm-32-4.5.1-20101214-1400-sfx.exe" from http://rubyinstaller.org/downloads/
2. Followed the instructions given on link https://github.com/oneclick/rubyinstaller/wiki/Development-Kit
3. Extracted the downloaded DevKit.
4. cd <DEVKIT_INSTALL_DIR>
5. run ruby dk.rb init.

C:\Users\ritesh\Downloads\DevKit-tdm-32-4.5.1-20101214-1400-sfx>ruby dk.rb init
[INFO] found RubyInstaller v1.9.2 at C:/Ruby192
[INFO] found RubyInstaller v1.8.7 at C:/Ruby187

Initialization complete! Please review and modify the auto-generated
'config.yml' file to ensure it contains the root directories to all
of the installed Rubies you want enhanced by the DevKit.

6. edit the generated config.yml file as per your system available Rubies version if required
7. [optional] ruby dk.rb review to verify the changes you made to it are correct.

8. finally, ruby dk.rb install to DevKit enhance your installed Rubies.


C:\Users\ritesh\Downloads\DevKit-tdm-32-4.5.1-20101214-1400-sfx>ruby dk.rb install
[INFO] Updating convenience notice gem override for 'C:/Ruby192'
[INFO] Installing 'C:/Ruby192/lib/ruby/site_ruby/devkit.rb'
[INFO] Skipping existing gem override for 'C:/Ruby187'
[WARN] Skipping existing DevKit helper library for 'C:/Ruby187'

9. You have successfully installed the DevKit, and now comes the step to install mysql2 gem, most important one for which we took so many steps. Make sure you are using Ruby 1.9.2 version.

10. Run gem install myql2 with parameters that I have passed, path can differ for you so make changes accordingly.

>gem install mysql2 -- '--with-mysql-lib="c:\Program Files\MySQL\MySQL Server 5.1\lib\opt" --with-mysql-include="c:\Program Files\MySQL\MySQL Server 5.1\include"'
Temporarily enhancing PATH to include DevKit...
Building native extensions.  This could take a while...
Successfully installed mysql2-0.2.7
1 gem installed
Installing ri documentation for mysql2-0.2.7...
Enclosing class/module 'mMysql2' for class Client not known
Installing RDoc documentation for mysql2-0.2.7...
Enclosing class/module 'mMysql2' for class Client not known

And, congrats, you are done with installation of Mysql2 gem on Ruby 1.9.2 and Rails 3.0.5 on Windows.

122 comments:

  1. Hi,
    First of all, thanks for the article. :)

    Well I have followed all the steps that you mentioned above but i am getting the following error :

    Could not find gem 'mysql2 (>= 0, runtime)' in any of the gem sources listed in your Gemfile.

    I am using Ruby 1.9.2 and Rails 3.0.5 with IDE as RubyMine 3.1 and SQLYog Community for MySql5.1.

    Any Suggestions ?

    ReplyDelete
  2. Hi Rashmi, Thanks for your encouragement and appreciation.
    When you are getting this error? What command you are executing? Have u added the line
    (gem 'mysql2') in Gemfile. Also please post your Gemfile details because this is something related Gemfile and gems version.

    ReplyDelete
  3. Hi,
    I am getting the error when I try to create db - 'rake db:create' from cmd prompt as well as from IDE. Yes I have added (gem 'mysql2') in Gemfile.Also, i have added the 'libmySQL.dll' in Ruby192's bin dir.

    Contents of Gemfile are :

    source 'http://rubygems.org'

    gem 'rails', '3.0.5'

    # Bundle edge Rails instead:
    # gem 'rails', :git => 'git://github.com/rails/rails.git'

    gem 'mysql2'

    # Use unicorn as the web server
    # gem 'unicorn'

    # Deploy with Capistrano
    # gem 'capistrano'

    # To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
    # gem 'ruby-debug'
    # gem 'ruby-debug19', :require => 'ruby-debug'

    # Bundle the extra gems:
    # gem 'bj'
    # gem 'nokogiri'
    # gem 'sqlite3-ruby', :require => 'sqlite3'
    # gem 'aws-s3', :require => 'aws/s3'

    # Bundle gems for the local environment. Make sure to
    # put test-only gems in this group so their generators
    # and rake tasks are available in development mode:
    # group :development, :test do
    # gem 'webrat'
    # end

    ReplyDelete
  4. P.S : For the time-being, I am using the Ruby192 dir [with rails 3.0.4] and it's working fine.

    But still I am eager to know the reason behind the error that I faced earlier for newer version of rails i.e 3.0.5 !!

    Thanks in advance :)

    ReplyDelete
  5. Hi,

    Have u noticed any change in gem version when you are running rails 3.0.4 or rails 3.0.5?

    Try to run "gem update --system" to update the gems on rails 3.0.5 and then run 'bundle install'. Then 'rake db:create'.

    Meanwhile, I am trying to reproduce the issue at my end, also trying to figure out if there is any difference in behavior of rails 3.0.4 and rails 3.0.5 with respect to mysql or mysql2 gem.

    Thanks

    ReplyDelete
  6. Hi,
    Well as a newbie to rails, I am not able to notice any change in gem versions. Any idea about them?

    Thanks..

    ReplyDelete
  7. hi...

    i'm new in rails i follow all the steps, before i found this post, i made the 9 step correctly, but when i tried to make the 10 step the shell give me erros

    gem install mysql2 -- '--with-mysql-lib="c:\Program Files\MySQL\MySQL Server 5.5\lib\opt" --with-mysql-include="c:\Program Files\MySQL\MySQL Server 5.5\include"'

    I dont know if the version of mysql can make something diferent. Do you have any idea?

    ReplyDelete
  8. Hi ppillo,

    Please post the error that you are getting along with ruby version, rails version.

    Also, check post "http://rorguide.blogspot.com/2011/03/getting-error-specified-module-could.html" if that can help.

    Have you copied "libmySQL.dll" file from 'C:\Program Files\MySQL\MySQL Server 5.5\bin' to 'C:\Ruby192\bin'

    Thanks

    ReplyDelete
    Replies
    1. hi Ritesh,

      myself priya, actually am new to ruby on rails but still using ruby on rails tutorial i tried to create a simple project 'hello rails' but i got lot of errors all most i googled and fixed it but still not able to run the project please suggest me some good tutorial to go with.

      here are few details of software am using
      os : win8,32 bit
      ruby :193
      rails :4
      mysql :5.5
      gem :2.0.7
      aptana studio 3

      and also am trying to use mysql instead of sqlite3 wich is default in aptana studio 3
      please give the solution to go through....

      Delete
    2. Hi Priya,

      I have followed 'http://guides.rubyonrails.org' site. Its one of the site which has explanation in simplest language. You can start looking for page - 'http://guides.rubyonrails.org/getting_started.html'.

      Let me know about the error if you not able to run.

      Delete
    3. Hi ritesh,
      thanks for ur reply :)myself priya again :)
      and actually i created my simple ruby on rails project and was able to run, later i was asked to work with fedena project so its requirements was like ruby 189
      and rails 2.3.5 so wat i did was along with previously installed rails -v4 i installed rails -v 2.3.4 and it also got installed but am not able to run rails server dono wats wrong with it......

      i also uninstall rails -v2.3.5 and tried but i couldn't rn my project please help :(

      Delete
  9. Hi Rashmi,

    I am unable to reproduce any such error at my end.
    Which ruby version patch u r using? Check it with
    "ruby -v" command and give complete ruby version with patch details.

    Also, Rails 3.0.6 has been released, so once try with rails 3.0.6 version to test if that error occurs in 3.0.6 also.

    Thanks

    ReplyDelete
  10. Hi,

    I have followed all the steps in the article but in step 10 i get the following error:

    Temporarily enhancing PATH to include DevKit...
    Building native extensions. This could take a while...
    ERROR: Error installing mysql2:
    ERROR: Failed to build gem native extension.

    C:/dev/Ruby192/bin/ruby.exe extconf.rb --with-mysql-lib="c:\Program File
    s\MySQL\MySQL Server 5.1\lib\opt" --with-mysql-include="c:\Program Files\MySQL\M
    ySQL Server 5.1\include"
    checking for rb_thread_blocking_region()... yes
    checking for main() in -llibmysql... no
    *** extconf.rb failed ***
    Could not create Makefile due to some reason, probably lack of
    necessary libraries and/or headers. Check the mkmf.log file for more
    details.

    I am using Ruby 1.9.2 and Rails 3.0.5 and MySql5.1, also i added libmysql.dll to Ruby/bin.

    Any Suggestions?

    ReplyDelete
  11. Ok, problem solved:

    subst X: "C:\Program Files\MySQL\MySQL Server 5.1"
    gem install mysql2 -v 0.2.6 --platform=ruby -- --with-mysql-dir=X: --with-mysql-lib=X:\lib\opt
    subst X: /D

    Version 0.2.7 did not work for me. Hope it helps :)

    ReplyDelete
  12. Hi Ritesh,
    I am using SQLServer 5.5 and not 5.1. When i try to execute Step 10 as mentioned by you , i am getting the following error :


    Temporarily enhancing PATH to include DevKit...
    Building native extensions. This could take a while...
    ERROR: Error installing mysql2:
    ERROR: Failed to build gem native extension.

    C:/Ruby192/bin/ruby.exe extconf.rb --with-mysql-lib="c:\Program Files\MySQL\MySQL Server 5.5\lib\opt" --with-mysql-include="c:\Program Files\MySQL\MySQL Server 5.5\inc
    lude"
    checking for rb_thread_blocking_region()... *** extconf.rb failed ***
    Could not create Makefile due to some reason, probably lack of
    necessary libraries and/or headers. Check the mkmf.log file for more
    details. You may need configuration options.


    What i doubt is that in Step 10 there is no folder as opt in my c:\Program Files\MySQL\MySQL Server 5.5\lib\.

    Please help.

    ReplyDelete
  13. Got it working as soon as I changed "c:\Program Files\MySQL\MySQL Server 5.5\lib\opt" to "c:\Program Files\MySQL\MySQL Server 5.5\lib".

    Many thanks for putting this guide together Ritesh!

    ReplyDelete
    Replies
    1. Thank you so much, I was having the same problem (although using MySQL Server 5.6) and changing it to /lib worked for me too.

      Delete
  14. Hi,

    Thanks M.G, thanks Sebastian for sharing your solutions with everyone.

    Jasmit, Ppillo hope this will solve your problem too.

    Thanks everybody :)

    ReplyDelete
  15. Hi Ritesh,
    Even after following what Sebastian mentioned, i am still facing the same issue. Any other Suggestions ?

    ReplyDelete
  16. Hi Guys, I'm getting the following error
    D:\RAILS-APPS>gem install mysql2 -v 0.2.3 --platform=ruby -- '--with-mysql-dir="c:\Program Files\MySQL\MySQL Server 5.5"
    --with-mysql-lib="c:\Program Files\MySQL\MySQL Server 5.5\lib" --with-mysql-include="c:\Program Files\MySQL\MySQL Serve
    r 5.5\include"'
    Temporarily enhancing PATH to include DevKit...
    Building native extensions. This could take a while...
    ERROR: Error installing mysql2:
    ERROR: Failed to build gem native extension.

    C:/Ruby192/bin/ruby.exe extconf.rb --with-mysql-dir="c:\Program Files\MySQL\MySQL Server 5.5" --with-mysql-lib="
    c:\Program Files\MySQL\MySQL Server 5.5\lib" --with-mysql-include="c:\Program Files\MySQL\MySQL Server 5.5\include"
    checking for rb_thread_blocking_region()... yes
    checking for main() in -llibmysql... yes
    checking for mysql.h... yes
    checking for errmsg.h... yes
    checking for mysqld_error.h... yes
    creating Makefile

    make
    gcc -I. -IC:/Ruby192/include/ruby-1.9.1/i386-mingw32 -I/C/Ruby192/include/ruby-1.9.1/ruby/backward -I/C/Ruby192/include/
    ruby-1.9.1 -I. -DHAVE_RB_THREAD_BLOCKING_REGION -DHAVE_MYSQL_H -DHAVE_ERRMSG_H -DHAVE_MYSQLD_ERROR_H "-Ic:\Program Files
    \MySQL\MySQL Server 5.5\include" "-Ic:\Program Files\MySQL\MySQL Server 5.5/include" -O3 -g -Wextra -Wno-unused-param
    eter -Wno-parentheses -Wpointer-arith -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wall -funroll-loop
    s -o client.o -c client.c
    client.c: In function 'rb_mysql_client_free':
    client.c:127:23: error: 'F_GETFL' undeclared (first use in this function)
    client.c:127:23: note: each undeclared identifier is reported only once for each function it appears in
    make: *** [client.o] Error 1


    Gem files will remain installed in C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.2.3 for inspection.
    Results logged to C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.2.3/ext/mysql2/gem_make.out

    I've applied all the recommendations from this thread and can't make it work. Any suggestion will be very appreciated.

    ReplyDelete
  17. hi!

    thanks Sebastian&Ritesh it works!

    ReplyDelete
  18. Great to hear that Ppillo.

    Thanks :)

    ReplyDelete
  19. Hi Cristian,

    Try this command to install mysql2 with Mysql Server version 5.5:

    gem install mysql2 -- '--with-mysql-lib="c:\Program Files\MySQL\MySQL Server 5.5\lib" --with-mysql-include="c:\Program Files\MySQL\MySQL Server 5.5\include"'

    Thanks

    ReplyDelete
  20. I don't have an include directory in my MySQL Server 5.1 directory...

    How do I get it? Do I really need to build mysql from the source code on Windows!? :/

    It's not there. MySQL installer did not install it.

    ReplyDelete
  21. Hi Egervari,

    Try M.G. Solution, see if that can help

    subst X: "C:\Program Files\MySQL\MySQL Server 5.1"

    gem install mysql2 -v 0.2.6 --platform=ruby -- --with-mysql-dir=X: --with-mysql-lib=X:\lib\opt

    Thanks

    ReplyDelete
  22. Hi everyone,
    Rails 3.0.6, Ruby 192, MySQL 5.5.11.
    Got all forementioned problems solved with (installed devkit):
    subst x: "C:\Program Files\MySQL\MySQL Server 5.5."
    gem install mysql2 -- with-mysql-dir=X: --with-mysql-lib=X:\lib
    Thanks

    ReplyDelete
    Replies
    1. I confirm that works.
      Thank you very much.

      Delete
  23. Hi everybody,

    DevKit installed (bson_ext correct)
    ruby 1.9.2
    mysql 5.5 (fail) and i try to uninstall and install 5.1 but i get same error...

    This is the output of the command (with command call):
    https://gist.github.com/921486

    i tried with subst, with full_path... But nothing...

    Thanks.

    ReplyDelete
  24. Hi,

    I have been able to install the mysql2 gem, but my rake commands hang. Any ideas?

    I am running Win7 x64 with MySQL 5.6, Ruby 1.9.2p180, Rails 3.0.6, Rake 0.8.7, and DevKit-tdm-32-4.5.1-20101214-1400-sfx.exe.

    I was able to install the mysql2 gem with the following command:

    gem install mysql2 -- --with-mysql-include="\include" --with-mysql-lib="\lib" --with-mysql-config="\bin\mysql_config"

    I have also tried MySQL 5.1 and MySQL 5.5 with no success.

    Thanks

    ReplyDelete
  25. Hey Ritesh,

    Am a newbie to RoR. While installing the gems for mysql2, I received the following error: (shrinked the redundant error)

    Temporarily enhancing PATH to include DevKit...
    Building native extensions. This could take a while...
    ERROR: Error installing mysql2:
    ERROR: Failed to build gem native extension.

    ..
    checking for rb_thread_blocking_region()... yes
    checking for main() in -llibmysql... yes
    checking for mysql.h... yes
    checking for errmsg.h... yes
    checking for mysqld_error.h... yes
    creating Makefile

    make
    C:/Ruby192/bin/ruby -e "puts 'EXPORTS', 'Init_mysql2'" > mysql2-i386-mingw32.def
    ...
    ...
    client.o: In function `nogvl_connect':
    C:\Ruby192\lib\ruby\gems\1.9.1\gems\mysql2-0.2.7\ext\mysql2/client.c:111: undefined reference to `mysql_real_connect@32'
    client.o: In function `nogvl_init':
    C:\Ruby192\lib\ruby\gems\1.9.1\gems\mysql2-0.2.7\ext\mysql2/client.c:102: undefined reference to `mysql_init@4'
    client.o: In function `set_ssl_options':
    C:\Ruby192\lib\ruby\gems\1.9.1\gems\mysql2-0.2.7\ext\mysql2/client.c:612: undefined reference to `mysql_ssl_set@24'
    collect2: ld returned 1 exit status
    make: *** [mysql2.so] Error 1

    Gem files will remain installed in C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.2.7 for inspection.
    Results logged to C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.2.7/ext/mysql2/gem_make.out

    Command used:
    gem install mysql2 -- '--with-mysql-lib="C:\Program Files\MySQL\MySQL Server 5.5\lib" --with-mysql-include="C:\Program Files\MySQL\MySQL Server 5.5\include"'

    OS : Windows 7 , 64 bit

    Plz Help Ritesh ! :)

    Thanks,
    Ankush

    ReplyDelete
  26. Hi Ankush,

    Similar problem others also faced and they were able to solve it by command:
    gem install mysql2 -- with-mysql-dir=X: --with-mysql-lib=X:\lib
    subst x: "C:\Program Files\MySQL\MySQL Server 5.5."
    Versions used: Rails 3.0.6, Ruby 192, MySQL 5.5.11.

    Thanks

    ReplyDelete
  27. Same as Bob complained... rake db:create hangs...

    I am running Win7 x32 with MySQL 5.5, Ruby 1.9.2p180, Rails 3.0.6, Rake 0.8.7, and DevKit-tdm-32-4.5.1-20101214-1400-sfx.exe.

    I was able to install the mysql2 gem with the following command:

    gem install mysql2 -- --with-mysql-dir=x: --with-mysql-lib=x:\lib

    I got warning message Enclosing class/module 'mMysql2' for class Client not known.

    I have also tried MySQL 5.1 and MySQL 5.5 with no success.

    Thanks

    ReplyDelete
  28. I reinstalled my computer with my lovely Gentoo but...
    Does everybody try on Windows 64bit this idea?

    And.. Did you try to force start at 32bit the gem/ruby?
    The whole ruby (+rails) run as 32bit application... ^_^ I haven't got Windows now but if i meet then i try it... if you do it pls let me know the result.

    ReplyDelete
  29. Read this thread for the contemplation:

    https://gist.github.com/921486#comments

    ReplyDelete
  30. rake db:create --trace

    ** Invoke db:craete (first_time)
    ** Invoke db:load_config (first_time)
    ** Invoke rails_env (first_time)
    ** Execute rails_env
    ** Execute db:load_config
    ** Execute db:create
    rake aborted!

    c:/ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.2.7/lib/active_record/connection_adapters/mysql2_adapter.rb:312:in 'query'
    c:/ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.2.7/lib/active_record/connection_adapters/mysql2_adapter.rb:312:in 'execute'
    ...

    ReplyDelete
  31. Thanks for this article.

    Nicholas

    ReplyDelete
  32. Thanks for not helping.

    Damir

    ReplyDelete
  33. I'm having the same issues as Bob and Anonymous above. The task rake db:create hangs on

    C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.2.7/lib/active_record/connection_adapters/mysql2_adapter.rb:312

    Just after this query: SET SQL_AUTO_IS_NULL=0, @@wait_timeout = 2147483

    I'm using
    - Windows 7 64bits
    - ruby 1.92
    - Mysql Server 5.1 (thought I tried 5.5 too)
    - mysql2-0.2.7

    I'm going to fallback into mysql adapter for now.
    For now I'm

    ReplyDelete
  34. Hi all,

    Rails server hanging issue with mysql2 is an open issue, for details refer to "https://github.com/brianmario/mysql2/issues/51"

    Thanks

    ReplyDelete
  35. Other similar issues are - "https://github.com/brianmario/mysql2/issues/142" and "https://github.com/brianmario/mysql2/issues/129"

    ReplyDelete
  36. I use WAMP 2.1 as MySQL installation. What paths should I supply in the last step (gem install mysql2...) ?

    ReplyDelete
  37. I mean I can't find "lib" and "include" MySQL directories in WAMP installation.

    ReplyDelete
  38. I got it to work by using Ruby 1.8.7 and the x86 version of MySQL

    ReplyDelete
  39. Misha: You problably have installed a "lite" version or some such, which doesn't include the mysql headers.
    Try downloading a full MySQL install from mysql.com (in my case, it had to be x86). Or, if you want everything in a single package, you can try XAMPP from apachefriends.org, but be sure to download the full version.

    P.S. Sorry for the double post

    ReplyDelete
  40. Mikelis: I use WAMP rather than XAMPP. Do you think installing XAMPP instead will solve the problem ?

    ReplyDelete
  41. Thanks Mikelis.
    Misha, when we install XAMPP, MySQL gets installed with "lib", "bin", "include" and other MySQL directories, and that I have verified on one of the system, so you can give it a try.

    Thanks

    ReplyDelete
  42. Saved me a headache, thanks a lot!

    ReplyDelete
  43. I've tried a few of you guys suggestions, but I still get an error. The only different about my mySQL installation is that I didn't use the windows installer, I installed it manually and I don't have an opt directory. This is what I tried to do last:

    X:\Program Files\RubyDevKit>gem install mysql2 -v 0.2.6 --platform=ruby -- --with-mysql-dir="x:\Prog
    ram Files\mysql-5.5.11-winx64" --with-mysql-lib="x:\Program Files\mysql-5.5.11-winx64\lib" --with-my
    sql-include="x:\Program Files\mysql-5.5.11-winx64\include" --without-opt-dir
    Building native extensions. This could take a while...
    ERROR: Error installing mysql2:
    ERROR: Failed to build gem native extension.

    x:/Ruby192/bin/ruby.exe extconf.rb --with-mysql-dir=x:\Program Files\mysql-5.5.11-winx64 --w
    ith-mysql-lib=x:\Program Files\mysql-5.5.11-winx64\lib --with-mysql-include=x:\Program Files\mysql-5
    .5.11-winx64\include --without-opt-dir
    checking for rb_thread_blocking_region()... yes
    checking for main() in -llibmysql... no
    *** extconf.rb failed ***
    Could not create Makefile due to some reason, probably lack of
    necessary libraries and/or headers. Check the mkmf.log file for more
    details. You may need configuration options.

    Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=x:/Ruby192/bin/ruby
    --with-mysql-dir
    --with-mysql-include=${mysql-dir}/include
    --with-mysql-lib=${mysql-dir}/lib
    --with-libmysqllib
    --without-libmysqllib


    Gem files will remain installed in x:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.2.6 for inspection.
    Results logged to x:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.2.6/ext/mysql2/gem_make.out

    Any help will be appreciated!

    ReplyDelete
  44. @Eric : I had exactly same issue. I've realized viewing other forums that mysql2 gem is not compatible with MySQL 64 bits. So I uninstalled MySQL and reinstalled x32 version (with msi installer). And now mysql2 is finally installed.

    Regards

    ReplyDelete
  45. hi. i've solved all problems on my machine in the following way.
    first of all, my system Win7 professional x64
    1. do first 9 steps from the article.
    2. download and install the version of MySQL that correspondes to your machine, my was MySQL v5.1.56 for x64 Windows, typical installation, do not forget to add your "bin" directory to the "path" variable(can do it through the installation).
    3. i've downloaded the "essential" version of MySQL, so there was some folders missing, such as "lib\opt","include" etc. Then i downloaded the full MySQL server in the zip and extracted "bin" "lib" and "include" folders into the installation folder of MySQL Server(mine: C:\Program Files\MySQL\MySQL Server 5.1\)
    4.Then i took the "libmysql.dll" from the 32bit version of MySQL and copied it to "bin" folder of Ruby
    5. in the "cmd":
    Subst X: C:\Program Files\MySQL\MySQL Server 5.1
    gem install mysql2 -v 0.2.6 -- --with-mysql-dir=X: --with-mysql-lib=X:\lib\opt --with-mysql-include=X:\include

    after these manipulations the gem was installed and when i've tested the rake utility everything was ok

    hope that will help you.

    ReplyDelete
  46. Thanks igkuz for your detailed manipulated and strange fix :)
    Hope that will fix everyones problem.

    ReplyDelete
  47. Hi the above post by iguz fixed the hanging issue, however it did not fix the migration. I am using Windows 7

    C:\nuhype>ruby -v
    ruby 1.9.2p180 (2011-02-18) [i386-mingw32]

    C:\nuhype>rails -v
    Rails 3.0.6

    C:\nuhype>gem install mysql2 -v 0.2.6 -- '--with-mysql-lib="c:\Program Files\MySQL\MySQL Server 5.1\lib\opt" --with-mysql-include="c:\Program Files\MySQL\MySQL Server 5.1\include"'

    Successfully installed mysql2-0.2.6-x86-mingw32
    1 gem installed
    Installing ri documentation for mysql2-0.2.6-x86-mingw32...
    Enclosing class/module 'mMysql2' for class Client not known
    Installing RDoc documentation for mysql2-0.2.6-x86-mingw32...
    Enclosing class/module 'mMysql2' for class Client not known

    C:\nuhype>rake db:migrate
    (in C:/nuhype)
    rake aborted!
    Please install the mysql2 adapter: `gem install activerecord-mysql2-adapter` (no such file
    to load -- active_record/connection_adapters/mysql2_adapter)
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.6/lib/active_record/connection_adapters/
    abstract/connection_specification.rb:71:in `rescue in establish_connection'
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.6/lib/active_record/connection_adapters/
    abstract/connection_specification.rb:68:in `establish_connection'

    I have copied over libmySQL.dll to the c:/Ruby/bin directory and also attempted to install: gem install activerecord-mysql2-adapter however, the activerecord gem does not exist. Someone please explain why this is happening?

    Nicholas

    ReplyDelete
  48. Anonymous' solution didn't work for me, it might work for somebody else, though.

    @igkuz Your solution worked perfectly. I didn't have to change my current configuration for my MySQL x64 installation.
    Thanks for the help!

    ReplyDelete
  49. Hey this worked for me.

    Only one thing to add.

    Sometimes windows doesn't add the devkit to your path. So worth running
    devkitvars.bat

    Thanks for help.

    Cheers..!!

    ReplyDelete
  50. Thanks for the great article. My environment is Win7x64 + RoR 3.0.7. I was having problems with the hanging issue when running 'rake db:migrate'. So I downgraded my mysql2 to 0.2.6 and this appears to have fixed my hanging issues. Here is how I built my 0.2.6 mysql2 :

    subst X: "C:\Program Files (x86)\MySQL\MySQL Server 5.1"
    gem install mysql2 -v 0.2.6 --platform=ruby -- --with-mysql-dir=X: --with-mysql-lib=X:\lib\opt


    Hope this helps with the others' hanging issues.

    ReplyDelete
  51. Thanks Mahesh for helping with your comments & solutions.

    ReplyDelete
  52. This is a great article, but is it possible that an update would be in order? The steps in the original article will you partway to getting MySQL/RAILS to work on Win7, but not all the way. I got further with suggestions from the comments, but I'm still not running. Is there an actual How-To-Do-It article on this topic that works for Win7/32bit, please?
    THANKS!

    ReplyDelete
  53. Well, i had this problem for a while, n i was pulling my hair over it, then i tried this:

    gem install mysql2 -v 0.2.6

    has been working flawlessly for me.

    Am running:
    Rails Version 3.0.7
    Win 7/64-bit

    ReplyDelete
  54. Hi Ankush,

    I really appreciate you taking the time to write up this blog. It is going on a week of this frustration that MySQL just will not work...

    I followed everyone of your steps religiously. I also followed the steps on this page:
    http://www.keenertech.com/articles/2011/06/05/mysql-and-rails-3-on-a-pc

    But I am not having any luck what so ever.

    I am running windows 7, rails 1.9.2, rails 3.0.9 and trying to run MySQL... When I get to step 10, like others, this is the error message that I receive:

    C:\ruby-dev-kit>gem install mysql2 -- '--with-mysql-lib="c:\Program Files\MySQL\
    MySQL Server 5.5\lib\opt" --with-mysql-include="c:\Program Files\MySQL\MySQL Ser
    ver 5.5\include"'
    Temporarily enhancing PATH to include DevKit...
    Building native extensions. This could take a while...
    ERROR: Error installing mysql2:
    ERROR: Failed to build gem native extension.

    C:/Ruby192/bin/ruby.exe extconf.rb --with-mysql-lib="c:\Program Files\My
    SQL\MySQL Server 5.5\lib\opt" --with-mysql-include="c:\Program Files\MySQL\MySQL
    Server 5.5\include"
    checking for rb_thread_blocking_region()... yes
    checking for main() in -llibmysql... no


    Gem files will remain installed in C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.
    3.5 for inspection.
    Results logged to C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.3.5/ext/mysql2/ge
    m_make.out

    I am sure you're sick of seeing this over and over again but I really don't what to do. I really don't want to give up on RoR but I can't make any progress unless I figure this out.

    side note: If I just type 'Rails Server' in the cmd prompt, I receive the following message, "The program can't start because sqlite3.dll is missing from your computer. Try reinstalling the program to fix this problem."

    Please help!


    David

    ReplyDelete
  55. Hi,

    I am also experiencing this problem. My environment is Windows 7, 64 bit, ruby 1.9.2p180, rails 3.0.7, MySql 5.5.

    I get exactly the same error as above.

    ReplyDelete
  56. Hi, I was facing the same problem for days, finally

    gem install mysql2 -v 0.2.6

    worked for me as well, it was also necessary to copy the libmySQL.dll (32bit version!!!) from {MYSQL_server_5.5_32bit_installation_dir}/lib to {ruby_folder}/bin

    ReplyDelete
  57. when you try to install the mysql2 gem with rails on xampp (using xampp's mysql installation) there is a problem. Mysql on xampp does not have the "library" and "includes" fodlers.... only "bin".

    Is there any workaround? (except ofc installing mysql for windows)

    ReplyDelete
  58. Hi,

    When we install xampp, Mysql on xampp gets installed with "lib", "bin", "include" and other MySQL directories. If you don't find it try to re-install xampp.

    ReplyDelete
  59. The trick is to copy the "libmySQL.dll" file from 'C:\Program Files\MySQL\MySQL Server 5.5\library' to 'C:\Ruby192\bin'

    ReplyDelete
  60. win7 x64, rails 3.1.0.rc4, ruby 1.9.2, mysql 5.5

    checking for rb_thread_blocking_region()... yes
    checking for main() in -llibmysql... yes
    checking for mysql.h... yes
    checking for errmsg.h... yes
    checking for mysqld_error.h... yes
    creating Makefile

    make

    and many lines with errors like this:
    C:\Ruby192\lib\ruby\gems\1.9.1\gems\mysql2-0.3.6\ext\mysql2/client.c:114: undefined reference to `mysql_real_connect@32'
    client.o: In function `nogvl_init':
    ...
    C:\Ruby192\lib\ruby\gems\1.9.1\gems\mysql2-0.3.6\ext\mysql2/result.c:73: undefined reference to `mysql_free_result@4'
    collect2: ld returned 1 exit status
    make: *** [mysql2.so] Error 1

    What can I do?

    ReplyDelete
  61. Ok I've done all of this to no avail. I have installed MySQL 5.5 64 bit so I downloaded the connector/c 32 bit into a separate directory and used it to install mysql2. I then copied the dll to the ruby lib folder.

    I thought everything worked so I tried rails server and I get this not a valid Win32 application. I'm stumped at this point since I'm already using my MySQL database for my Java projects.

    I have this running on my Ubuntu 64 machine, so could I dual develop even with different databases? Sometimes if you develop right in Java, you can abstract the database from the application and it'll never know the difference. Is SQLite3 too different?

    ReplyDelete
  62. No glory for me with Windows 7x64, MySQL 5.5x64, Ruby 1.9.2:

    C:\>gem install mysql2 -- '--with-mysql-lib="C:\Program Files\MySQL\MySQL Server 5.5\lib" --with-mysql-include="C:\Program Files\MySQL\MySQL Server 5.5\include"'

    Temporarily enhancing PATH to include DevKit...
    Building native extensions. This could take a while...
    ERROR: Error installing mysql2:
    ERROR: Failed to build gem native extension.

    C:/Ruby/bin/ruby.exe extconf.rb --with-mysql-lib="C:\Program Files\MySQL\MySQL Server 5.5\lib" --with-mysql-include="C:\Program Files\MySQL\MySQL Server 5.5\include"
    checking for rb_thread_blocking_region()... yes
    checking for main() in -llibmysql... yes
    checking for mysql.h... yes
    checking for errmsg.h... yes
    checking for mysqld_error.h... yes
    creating Makefile

    make
    gcc -I. -IC:/Ruby/include/ruby-1.9.1/i386-mingw32 -I/C/Ruby/include/ruby-1.9.1/ruby/backward -I/C/Ruby/include/ruby-1.9.1 -I. -DHAVE_RB_THREAD_BLOCKING_REGION -DHAVE_MYSQL_H -DHAVE_ERRMSG_H -DHAVE_MYSQLD_ERROR_H "-IC:\Program Files\MySQL\MySQL Server 5.5\include" -O3 -g -Wextra -Wno-unused-parameter -Wno-parentheses -Wpointer-arith -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wall -funroll-loops -o client.o -c client.c
    gcc -I. -IC:/Ruby/include/ruby-1.9.1/i386-mingw32 -I/C/Ruby/include/ruby-1.9.1/ruby/backward -I/C/Ruby/include/ruby-1.9.1 -I. -DHAVE_RB_THREAD_BLOCKING_REGION -DHAVE_MYSQL_H -DHAVE_ERRMSG_H -DHAVE_MYSQLD_ERROR_H "-IC:\Program Files\MySQL\MySQL Server 5.5\include" -O3 -g -Wextra -Wno-unused-parameter -Wno-parentheses -Wpointer-arith -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wall -funroll-loops -o mysql2_ext.o -c mysql2_ext.c
    gcc -I. -IC:/Ruby/include/ruby-1.9.1/i386-mingw32 -I/C/Ruby/include/ruby-1.9.1/ruby/backward -I/C/Ruby/include/ruby-1.9.1 -I. -DHAVE_RB_THREAD_BLOCKING_REGION -DHAVE_MYSQL_H -DHAVE_ERRMSG_H -DHAVE_MYSQLD_ERROR_H "-IC:\Program Files\MySQL\MySQL Server 5.5\include" -O3 -g -Wextra -Wno-unused-parameter -Wno-parentheses -Wpointer-arith -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wall -funroll-loops -o result.o -c result.c
    result.c: In function 'rb_mysql_result_fetch_fields':
    result.c:376:35: warning: comparison between signed and unsigned integer expressions
    gcc -shared -s -o mysql2.so client.o mysql2_ext.o result.o -L. -LC:/Ruby/lib -L"C:\Program Files\MySQL\MySQL Server 5.5\lib" -L. -Wl,--enable-auto-image-base,--enable-auto-import mysql2-i386-mingw32.def -lmsvcrt-ruby191 -llibmysql -lshell32 -lws2_32
    client.o: In function `nogvl_connect':
    C:\Ruby\lib\ruby\gems\1.9.1\gems\mysql2-0.3.6\ext\mysql2/client.c:114: undefined reference to `mysql_real_connect@32'
    client.o: In function `nogvl_init':
    C:\Ruby\lib\ruby\gems\1.9.1\gems\mysql2-0.3.6\ext\mysql2/client.c:105: undefined reference to `mysql_init@4'
    client.o: In function `set_ssl_options':
    C:\Ruby\lib\ruby\gems\1.9.1\gems\mysql2-0.3.6\ext\mysql2/client.c:700: undefined reference to `mysql_ssl_set@24'
    client.o: In function `set_charset_name':
    C:\Ruby\lib\ruby\gems\1.9.1\gems\mysql2-0.3.6\ext\mysql2/client.c:688: undefined reference to `mysql_options@12'
    C:\Ruby\lib\ruby\gems\1.9.1\gems\mysql2-0.3.6\ext\mysql2/client.c:690: undefined reference to `mysql_error@4'
    client.o: In function `set_connect_timeout':
    C:\Ruby\lib\ruby\gems\1.9.1\gems\mysql2-0.3.6\ext\mysql2/client.c:659: undefined reference to `mysql_options@12'
    C:\Ruby\lib\ruby\gems\1.9.1\gems\mysql2-0.3.6\ext\mysql2/client.c:661: undefined reference to `mysql_error@4'
    [comment truncated]

    ReplyDelete
  63. YES!! Ritesh Kumar is my HERO!! FTW! (worked like a charm)!

    ReplyDelete
  64. This worked with MySQL Server 5.5 on Windows 7:

    $ gem install mysql2 -- '--with-mysql-lib="c:\Program Files\MySQL\MySQL Server 5.5\lib" --with-mysql-include="c:\Program Files
    \MySQL\MySQL Server 5.5\include"'
    Temporarily enhancing PATH to include DevKit...
    Building native extensions. This could take a while...
    Successfully installed mysql2-0.3.6
    1 gem installed
    Installing ri documentation for mysql2-0.3.6...
    Enclosing class/module 'mMysql2' for class Client not known
    Installing RDoc documentation for mysql2-0.3.6...
    Enclosing class/module 'mMysql2' for class Client not known

    ReplyDelete
  65. Hey Greg, thanks for your compliment :)

    Hatem, glad that mysql2 installed successfully.
    Can you post the environments in which it got installed.
    In previous comment, Phrogz facing problem with installation on Windows 7x64, MySQL 5.5x64, Ruby 1.9.2.

    Thanks

    ReplyDelete
  66. Try the following if you continue to face problems. This worked well for me.


    gem install mysql2 -v 0.2.6

    ReplyDelete
  67. I had the same issue with installing mysql2 gem on Windows 7 x64 and I solved this. Please read this: https://github.com/oneclick/rubyinstaller/wiki/Troubleshooting#gems_mix_bits

    solution:
    1. Download mysql-5.5.14-winx64.msi from http://dev.mysql.com/downloads/mysql/ and install
    2. Download mysql-5.5.14-win32.msi, enter package by Total Commander by Ctrl+PgDn hotkey and copy contents of SourceDir/Mysql to D:/Temp
    3. Copy file d:\Temp\MySQL Server 5.5\lib\libmysql.dll to c:\Ruby192\bin\
    4. Install mysql2 gem by command:
    gem install mysql2 -- '--with-mysql-lib="d:\Temp\MySQL Server 5.5\lib" --with-mysql-include="d:\Temp\MySQL Server 5.5\include"'

    That`s all.

    ReplyDelete
  68. if you are using 32bit mysql on Win7 then use:
    gem install mysql2 -- '--with-mysql-lib="C:\Program Files (x86)\MySQL\
    MySQL Server 5.5\lib" --with-mysql-include="C:\Program Files (x86)\MySQL\MySQL S
    erver 5.5\include"'

    ReplyDelete
  69. milton here some info that got me running on xp after i did the above and it still did not work

    your gemfile should state following:

    gem 'rails', '3.0.9'

    gem 'mysql', '~> 2.8.1'

    furthermore your database.yml should state following:

    adapter: mysql

    for all 3 instances !

    now you can do a succesful bundle install and mysql2 will install accordingly and work !

    have fun regards milton

    ReplyDelete
  70. gem install mysql2 -v 0.2.6

    worked for me...

    ReplyDelete
  71. Wow, I've been trying to install mysql2 for almost a day and still can't figure out what is still missing after following all the steps you guys suggested.
    Installing Rails with InstantRails is a no brainer..but for guys like me who wants to be productive and produce an application, this is such a hassle..
    This is just counter-productive...
    Anybody who got the right steps to take should voice out now, before I go back to PHP..(still ROCKS!, Sorry RoR peeps, I see too much configuration and convention!)
    BTW, Am installing it in WinXP sp3 with MySQL 5.5

    ReplyDelete
  72. I'm able to get the gem installed successfully using the suggestions above, but when I try to run my server, I continue to get the following error:

    ←[31mCould not find gem 'mysql2 (= 0.2.11)' in any of the gem sources listed in
    your Gemfile.←[0m
    ←[33mRun `bundle install` to install missing gems.←[0m

    subsequent bundle install attempts to install the mysql2 gem again, and promptly fails because I'm not giving it the lib and include path parameters. Any idea what is causing this discrepancy between what gem install does and what bundle install does? Or, alternatively, is there a way to pass those same parameters to bundle?

    I'm running on Win7 (32-bit) machine, having just upgraded to Ruby 1.9.2. Had the mysql2 installation issue on 1.8.7, which I had been using previously, but gem install was able to keep bundle happy... Did something change for 1.9 or is there a way to check if bundle is getting my gems between the two Ruby version on my machine confused?

    ReplyDelete
  73. Hi Alex,

    First, some clarification on your issues:
    1. Discrepancy between gem install and bundle install seems to be of mysql2 version issue (Or can be a case of installation of gem between two Ruby versions)

    2. In Gemfile, we don't require to pass any lib or include path for mysql2, normally it works with version and source parameters(In my case -
    gem 'mysql2', '< 0.3')

    3. Which version of Ruby you are using? You can manage between Ruby multiple versions on Windows by 'Pik' to avoid versions confusion. (http://rorguide.blogspot.com/search/label/Pik)

    Now, some queries and steps that can fix your issue:
    1. Which version of mysql2 gem you installed?

    2. Delete 'Gemfile.lock' and then run 'bundle install'

    3. After you installed gem, did you close that terminal and open a new one and then tried to run bundle install.

    4. Make sure, you are running 'bundle install' on the same Ruby version on which you have installed the 'mysql2' gem.

    Hope this will clarify few of your doubts. Let me know about the installation results.

    Thanks

    ReplyDelete
  74. Hi Ron Tuibeo,

    Sorry to hear that you are facing installation issue :(
    Can you post the error messages, gemfile details about the mysql2 versions, ruby and rails version to help us in analyzing the issue.

    And and.... 'too much configuration and convention' is one of the beauty that RoR provides to make work easier and faster, it doesn't force you to follow them and your installation issue has nothing to do with it.

    ReplyDelete
  75. Hey Ritesh, thanks for the help! After spending a few more hours digging through it, I did find that my issue was that the gem versions were out of sync. Getting everything lined back up seems to have made things work, but I'll stick with 1.8.7 for now, and will try an upgrade later. On the bright side, I feel far more familiar with gem and bundler at this point...

    ReplyDelete
  76. Hey Ritesh & others,

    I'm facing some installation issues as well with mysql2.

    ruby -v => 1.9.2p290

    *** FAILURE ***
    > bundle install
    ...
    ...
    Installing mysql2 (0.2.13) .....
    Failed to build gem native extension
    Installing mysql2 (0.2.13) with native extensions C:/Ruby192/lib/ruby/site_ruby/
    1.9.1/rubygems/installer.rb:533:in `rescue in block in build_extensions': ERROR:
    Failed to build gem native extension. (Gem::Installer::ExtensionBuildError)

    C:/Ruby192/bin/ruby.exe extconf.rb
    checking for rb_thread_blocking_region()... yes
    checking for main() in -llibmysql... no
    *** extconf.rb failed ***
    Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers. Check the mkmf.log file for more details. You may need configuration options.

    So, then I did this ..
    *** SUCCESS ****
    > gem install mysql2 -v 0.2.6

    and that's successful

    BUT,
    > rails server

    ←[31mCould not find gem 'mysql2 (< 0.3)' in any of the gem sources listed in your Gemfile.←[0m
    ←[33mRun `bundle install` to install missing gems.←[0m

    But 'bundle install fails!'

    Thanks for any help
    Scott

    ReplyDelete
  77. Hi Scott,

    Some steps which can help you in solving this issue:

    Which version of 'MySQL Server' you are using?
    Which mysql2 version you are mentioning in 'GemFile'?

    Copy "libmySQL.dll" file from 'C:\Program Files\MySQL\MySQL Server 5.5\library' to 'C:\Ruby192\bin' and then try 'bundle install'


    Use below command to install mysql2:
    gem install mysql2 -- '--with-mysql-lib="c:\Program Files\MySQL\MySQL Server 5.5\lib" --with-mysql-include="c:\Program Files\MySQL\MySQL Server 5.5\include"'

    Thanks
    Ritesh

    ReplyDelete
  78. Hi,

    I have the same problem, I can't get mysql2 to run. I use Win 7 64 Bit with:

    >ruby -v
    ruby 1.9.2p290 (2011-07-09) [i386-mingw32]

    >rails -v
    Rails 3.0.10

    >gem -v
    1.8.10

    I use mysql 32 Bit and copied the libmysql.dll to ruby192/bin

    gem install mysql2 does not work, but I tried with
    >gem install mysql2 -v 0.2.6
    as mentioned above, and it seams to work:

    Successfully installed mysql2-0.2.6-x86-mingw32
    1 gem installed
    Installing ri documentation for mysql2-0.2.6-x86-mingw32...
    Enclosing class/module 'mMysql2' for class Client not known
    Installing RDoc documentation for mysql2-0.2.6-x86-mingw32...
    Enclosing class/module 'mMysql2' for class Client not known

    But when I start my rails server I get:

    E:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.2.6-x86-mingw32/lib/mysql2/mysql2.rb:2:in `require': 193: %1 ist keine zulõssige Win32-Anwendung. - E:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.2.6-x86-mingw32/lib/mysql2/1.9/mysql2.so (LoadError)

    The german words mean: %1 is no valid Win32 application.

    Does somebody knows something about that? Thank you!

    ReplyDelete
  79. Someone facing this issue, taken from my email.

    Hi Ritesh,
    I found your post and tried your solution:
    http://rorguide.blogspot.com/2011/03/installing-mysql2-gem-on-ruby-192-and.html

    I am running: ruby 1.9.2p290
    rails 3.0.10
    and mysql2 0.2.7 with mysql 5.5

    with mysql gem everythings works fine, but i cannot get mysql2 running on windows (windows server 2003). On MacOS everything works fine.

    The problem with the normal mysql gem is, that i get ascii/utf8 problems.

    When i try to run the server with your setup, it starts, but i cannot connect to it via browser.

    Do you have any suggestions?

    Would be very nice, if you can help me with that.

    ReplyDelete
  80. Hi,
    after (too) many tries I finally got my solution.

    Windows 7 x64
    Ruby 1.9.2p290
    Rails 3.0.10
    MySql 5.5 x64

    Ruby is 32bit so I guess the problem is working with the 64bit dll provided with the MySql install.
    I've read somewhere (sorry I don't remember where but thanks) that it's useful to use the 32bit connector downloadable from the MySql site.
    I've downloaded "mysql-connector-c-noinstall-6.0.2-win32.zip" and unzipped it under C:\ and copied the libmysql.dll in my C:\ruby192\bin folder.

    Rails wants version 0.2.13 of the mysql2 gem so I've built that version using this command that points to the connector files:
    gem install mysql2 -v 0.2.13 -- '--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"'

    After that I've been able to execute "bundle install" and "rails server" on my project without further problems...

    I do hope that this helps and thanks to all for the suggestions!

    ReplyDelete
  81. Thanks Stefano Falda,

    Steps that you have mentioned is really going to help a lot to everyone including me :)

    Thanks once again for your great effort

    Ritesh

    ReplyDelete
  82. Thanks Stefano Falda, works perfect,after hours of trying to get this working with xampps mysql folder....

    ReplyDelete
  83. thank u vry much ritesh and jasmit

    ReplyDelete
  84. Windows 7 x64
    Ruby 1.9.2p290
    Rails 3.1.0
    MySql 5.5 x64

    I am getting the error below.
    I followed Riteshs way and also stefanos way.
    I think I am not installing the right version of mysql.
    I am really confused with so many replies.
    can someone please help me with step by step installation right from which version of mysql to install.
    I can easily install ruby, rails, dev kit.
    But just cannot install the mysql gem after trillion tries.

    [code]
    C:\DevKit>gem install mysql2 -v 0.2.13 -- '--with-mysql-lib="C:\mysql-connector-
    c-noinstall-6.0.2-win32\lib" --with-mysql-include="C:\mysql-connector-c-noinstal
    l-6.0.2-win32\include"'
    Temporarily enhancing PATH to include DevKit...
    Building native extensions. This could take a while...
    ERROR: Error installing mysql2:
    ERROR: Failed to build gem native extension.

    C:/Ruby192/bin/ruby.exe extconf.rb --with-mysql-lib="C:\mysql-connector-
    c-noinstall-6.0.2-win32\lib" --with-mysql-include="C:\mysql-connector-c-noinstal
    l-6.0.2-win32\include"
    checking for rb_thread_blocking_region()... yes
    checking for main() in -llibmysql... no
    *** extconf.rb failed ***
    Could not create Makefile due to some reason, probably lack of
    necessary libraries and/or headers. Check the mkmf.log file for more
    details. You may need configuration options.

    Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=C:/Ruby192/bin/ruby
    --with-mysql-dir
    --without-mysql-dir
    --with-mysql-include=${mysql-dir}/include
    --with-mysql-lib=${mysql-dir}/lib
    --with-libmysqllib
    --without-libmysqllib


    Gem files will remain installed in C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.
    2.13 for inspection.
    Results logged to C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.2.13/ext/mysql2/g
    em_make.out
    [/code]

    ReplyDelete
  85. Thanks a lot.. Helped me big time.

    ReplyDelete
  86. gem install mysql2 -- '--with-mysql-lib="C:\Program File
    s\MySQL\MySQL Server 5.5\lib" --with-mysql-include="C:\Program Files\MySQL\MySQL
    Server 5.5\include"

    I have no idea what is going on :( i've read all articles / comments / tips and still have a message

    Temporarily enhancing PATH to include DevKit...
    Building native extensions. This could take a while...
    ERROR: Error installing mysql2:
    ERROR: Failed to build gem native extension.

    C:/Ruby192/bin/ruby.exe extconf.rb --with-mysql-lib="C:\Program Files\My
    SQL\MySQL Server 5.5\lib" --with-mysql-include="C:\Program Files\MySQL\MySQL Ser
    ver 5.5\include"
    checking for rb_thread_blocking_region()... *** extconf.rb failed ***
    Could not create Makefile due to some reason, probably lack of
    necessary libraries and/or headers. Check the mkmf.log file for more
    details. You may need configuration options.

    Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=C:/Ruby192/bin/ruby
    C:/Ruby192/lib/ruby/1.9.1/mkmf.rb:368:in `try_do': The complier failed to genera
    te an executable file. (RuntimeError)
    You have to install development tools first.
    from C:/Ruby192/lib/ruby/1.9.1/mkmf.rb:435:in `try_link0'
    from C:/Ruby192/lib/ruby/1.9.1/mkmf.rb:440:in `try_link'
    from C:/Ruby192/lib/ruby/1.9.1/mkmf.rb:552:in `try_func'
    from C:/Ruby192/lib/ruby/1.9.1/mkmf.rb:797:in `block in have_func'
    from C:/Ruby192/lib/ruby/1.9.1/mkmf.rb:693:in `block in checking_for'
    from C:/Ruby192/lib/ruby/1.9.1/mkmf.rb:280:in `block (2 levels) in postp
    one'
    from C:/Ruby192/lib/ruby/1.9.1/mkmf.rb:254:in `open'
    from C:/Ruby192/lib/ruby/1.9.1/mkmf.rb:280:in `block in postpone'
    from C:/Ruby192/lib/ruby/1.9.1/mkmf.rb:254:in `open'
    from C:/Ruby192/lib/ruby/1.9.1/mkmf.rb:276:in `postpone'
    from C:/Ruby192/lib/ruby/1.9.1/mkmf.rb:692:in `checking_for'
    from C:/Ruby192/lib/ruby/1.9.1/mkmf.rb:796:in `have_func'
    from extconf.rb:9:in `'


    Gem files will remain installed in C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.
    3.7 for inspection.
    Results logged to C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.3.7/ext/mysql2/ge
    m_make.out

    ReplyDelete
  87. Hi Jacobson,

    At first sight, it seems to be a Devkit installation problem.
    Have you installed DevKit properly with correct version?
    "https://github.com/oneclick/rubyinstaller/wiki/Development-Kit" link will help you in installation of Devkit.

    Once it has installed, you can install mysql2 with the above command.

    Thanks
    Ritesh Kumar

    ReplyDelete
  88. I have my Devkit already installed http://screenshotuploader.com/s/6snA6LR2wLF this is screenshot made after my second installation. I installed this version : DevKit-tdm-32-4.5.2-20110712-1620-sfx.exe from https://github.com/oneclick/rubyinstaller/downloads/

    ReplyDelete
  89. Hello All,

    First of all thanks for making such kind of solutions for us.
    All things are fine but here several kind of dependencies are going on, like rails version, mysql2 version, and ruby version.for complete installation use these steps.
    step 1. Install dev kit for windows.
    and follow instruction from above.
    And make one change
    Use this command for installation: Here related to mysql path is path of local installation directory.
    gem install mysql2 –v=0.2.6 -- '--with-mysql-lib="c:\Program Files\MySQL\MySQL Server 5.1\lib\" --with-mysql-include="c:\Program Files\MySQL\MySQL Server 5.1\include"'

    step 2: After installing use your version mysql2 –v 0.2.x, because –v0. 3.x don’t work on window or Rails 3 or Ruby 1.9.

    step 3: update in your gem file form gem 'mysql2' to gem 'mysql', '0.2.x' # here x=6..10.

    One point to note here if you use 0.3.x version for rails 3.1.x then it will not support.
    and

    if You use mysql2 -v=0.3.x then it will require mysql2-adapter for active record so use mysql2-v0.2.x.

    Enjoy with mysql2 on ruby 1.9 and rails 3.0.x


    Cheers.

    ReplyDelete
  90. Bravo!
    It is 5:37AM here in San Diego, CA. I can go sleep now.

    ReplyDelete
  91. Dear All,

    I had a problem installing mysql2 gem. Below is the error. (Platform win 7 x64, mysql 5.5 x64, rails 3.1, ruby 1.9.2)

    THANKS.

    gem install mysql2 -- '--with-mysql-lib
    ="c:\Program Files\MySQL\MySQL Server 5.5\lib" --with-mysql-include="c:\Program
    Files\MySQL\MySQL Server 5.5\include"'

    Temporarily enhancing PATH to include DevKit...
    Building native extensions. This could take a while...
    ERROR: Error installing mysql2:
    ERROR: Failed to build gem native extension.

    C:/Ruby192/bin/ruby.exe extconf.rb --with-mysql-lib="c:\Program Files\MySQL\MySQ
    L Server 5.5\lib" --with-mysql-include="c:\Program Files\MySQL\MySQL Server 5.5\
    include"
    checking for rb_thread_blocking_region()... yes
    checking for main() in -llibmysql... yes
    checking for mysql.h... yes
    checking for errmsg.h... yes
    checking for mysqld_error.h... yes
    creating Makefile

    make: *** [mysql2.so] Error 1

    ReplyDelete
  92. I fix my problem by uninstall mysql 64 bit and change it with mysql 32.

    ReplyDelete
  93. Hi Ritesh!

    I tried this, and works for me!

    -------------------------------------------------
    Versions
    -Windows: XP SP3
    -Ruby: 1.9.2p290 (2011-07-09) [i386-mingw32]
    -Rails: 3.1.1
    -MySQL: MySQL 5.5
    -------------------------------------------------

    FOLLOW STEPS 1 ~ 8

    [MySQL] : (MySQL Folder)
    (i used "/" and not "\" ~> e.g "C:/Programs Files/MySQL/MySQL 5.5")

    gem install mysql2 -- '--with-mysql-lib="[MySQL]/lib" --with-mysql-include="[MySQL]/include"'

    gem list
    .
    .
    mysql (2.8.1 x86-mingw32)
    mysql2 (0.3.7)
    rails (3.1.1)
    .
    .

    Thanks! \o

    ReplyDelete
  94. Great thanks! I had the same problem when installing rails and this solved it.

    ReplyDelete
  95. Hi all, first of all thank you for the post and the useful comments. I've followed all the steps but when executing rake db:drop or rake db:create the execution never ends after the warning message:
    C:\>rake db:drop
    WARNING: Global access to Rake DSL methods is deprecated. Please include
    ... Rake::DSL into classes and modules which use the Rake DSL methods.

    When executing rails s the messages seems correct but when trying to connect from a browser it says waiting for localhost and it never ends neither.

    C:\>rails s
    => Booting WEBrick
    => Rails 3.0.5 application starting in development on http://0.0.0.0:3000
    => Call with -d to detach
    => Ctrl-C to shutdown server
    [2011-11-08 14:54:25] INFO WEBrick 1.3.1
    [2011-11-08 14:54:25] INFO ruby 1.9.2 (2011-02-18) [i386-mingw32]
    [2011-11-08 14:54:25] INFO WEBrick::HTTPServer#start: pid=904 port=3000


    Have you seen that behavior before?

    I'm using ruby 1.9.2 / rails 3.0.5 / mysql 5.5 64 bits (using the 32 connector) in a Windows 7 64 bits

    Thank you very much for your help!

    ReplyDelete
  96. Hi again, it seems that the problems with rake db:drop and rails s are solved after installing ruby 1.8.7 so the problem was caused by Ruby 1.9.2 and 1.9.3 (just in case it helps). Thanks!

    ReplyDelete
  97. Best solution is here: http://blog.mmediasys.com/2011/07/07/installing-mysql-on-windows-7-x64-and-using-ruby-with-it/

    ReplyDelete
  98. Hello,, help me in this,, when i install bundle in command prompt it said could not locate gemfile... hOw can you fix it....i had even install the bundler and it works, but the bundle did not work,

    ReplyDelete
  99. Hi, make sure that there is a Gemfile located at that path from where you are running bundle install because Bundle install checks Gemfile and then install gems present in Gemfile

    Thanks
    Ritesh Kumar

    ReplyDelete
  100. I am regular ѵisіtor, hοw are yοu everybоdy?
    Thіs artiсlе pоѕtеd аt
    this web sitе is aсtuаlly nice.
    Feel free to surf my web blog ; Jade Crystal

    ReplyDelete
  101. I followed your guide and only thing I had to do extra was copy a libmysql.dll to the bin folder of ruby192. Thanks! Very helpful!

    ReplyDelete
  102. I am getting this error on my local machine after deploying project to remote server(which the app works fine on)

    C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql-2.9.0-x86-mingw32/lib/mysql.rb:4:in `require': Incorrect MySQL client library version! This gem was compiled for 6.0.0 but the client library is 5.0.27. (RuntimeError)

    Any Help


    ReplyDelete
    Replies
    1. Hi Michele,

      Error tells that there is a mismatch in MySQL client library installed and lib used in ruby. Try this solution - Copy "libmySQL.dll" file from 'C:\Program Files\MySQL\MySQL Server 5.1\bin' to 'C:\Ruby192\bin' and then start the server.

      Let me know incase if it does not work.

      Thanks
      Ritesh Kumar

      Delete
  103. I am getting an error like this

    $ rails generate exam index
    c:/Ruby193/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11-x86-mingw32/lib/mysql2/mysql2.rb:2:in `require': 193: %1 is not a valid Win32 application. - c:/Ruby193
    /lib/ruby/gems/1.9.1/gems/mysql2-0.3.11-x86-mingw32/lib/mysql2/1.9/mysql2.so (LoadError)
    from c:/Ruby193/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11-x86-mingw32/lib/mysql2/mysql2.rb:2:in `'
    from c:/Ruby193/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11-x86-mingw32/lib/mysql2.rb:9:in `require'
    from c:/Ruby193/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11-x86-mingw32/lib/mysql2.rb:9:in `'
    from c:/Ruby193/lib/ruby/gems/1.9.1/gems/bundler-1.2.3/lib/bundler/runtime.rb:68:in `require'
    from c:/Ruby193/lib/ruby/gems/1.9.1/gems/bundler-1.2.3/lib/bundler/runtime.rb:68:in `block (2 levels) in require'
    from c:/Ruby193/lib/ruby/gems/1.9.1/gems/bundler-1.2.3/lib/bundler/runtime.rb:66:in `each'
    from c:/Ruby193/lib/ruby/gems/1.9.1/gems/bundler-1.2.3/lib/bundler/runtime.rb:66:in `block in require'
    from c:/Ruby193/lib/ruby/gems/1.9.1/gems/bundler-1.2.3/lib/bundler/runtime.rb:55:in `each'
    from c:/Ruby193/lib/ruby/gems/1.9.1/gems/bundler-1.2.3/lib/bundler/runtime.rb:55:in `require'
    from c:/Ruby193/lib/ruby/gems/1.9.1/gems/bundler-1.2.3/lib/bundler.rb:128:in `require'
    from C:/Aptana3/exa/config/application.rb:7:in `'
    from c:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/commands.rb:24:in `require'
    from c:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/commands.rb:24:in `'
    from script/rails:6:in `require'
    from script/rails:6:in `'

    ReplyDelete
    Replies
    1. Hi Anil,

      Please try the solution given on post:
      http://rorguide.blogspot.in/2011/03/getting-error-specified-module-could.html

      Let me know if it not work.

      Thanks
      Ritesh

      Delete
  104. Thank you very much for this post. I'd been trying for almost an hour to get this gem installed but kept getting errors about missing headers or libraries when I tried to run gem install. Your solution worked perfectly on the first try.

    ReplyDelete
  105. Thanks alotttttttttt.... this site is really very much helpful...!!:)

    ReplyDelete
  106. I had this error :

    C:\Ruby192\lib\ruby\gems\1.9.1\gems\mysql2-0.2.7\ext\mysql2/client.c:111: undefined reference to `mysql_real_connect@32'
    client.o: In function `nogvl_init':
    C:\Ruby192\lib\ruby\gems\1.9.1\gems\mysql2-0.2.7\ext\mysql2/client.c:102: undefined reference to `mysql_init@4'
    client.o: In function `set_ssl_options':
    C:\Ruby192\lib\ruby\gems\1.9.1\gems\mysql2-0.2.7\ext\mysql2/client.c:612: undefined reference to `mysql_ssl_set@24'
    collect2: ld returned 1 exit status
    make: *** [mysql2.so] Error 1

    The solution :

    Download last MySQL connector from http://cdn.mysql.com/Downloads/Connector-C/mysql-connector-c-noinstall-6.0.2-win32.zip (32 bits because 64 bits causes the problem "undefined reference" uninstall 64 bits if needed)

    Extract it to C:\connector-6.0.2

    gem install mysql2 --platform=ruby -- '--with-mysql-lib="C:\connector-6.0.2\lib" --with-mysql-include="C:\connector-6.0.2\include" --with-mysql-dir="C:\connector-6.0.2"'

    ReplyDelete
    Replies
    1. When rake db:migrate, an other problem appears :

      mysql2.so not found

      The solution : copy libmysql.dll from C:\connector-6.0.2\lib and replace into C:\Ruby\bin where Ruby is installed

      Delete
    2. Thank you I solved the problem. You ara great.

      Delete
  107. Thanks buddy it solved my problem.I was trying to solve dis from last 1 month.But at last ur Solution worked for me.

    ReplyDelete
  108. THANK SATAN! Finally, something that works!

    ReplyDelete
  109. Thank you very much!
    I WampServer and MySQL from my version:

    C:\DevKit>gem install mysql2 -- '--with-mysql-lib="C:\wamp\bin\mysql\mysql5.6.12
    \lib" --with-mysql-include="C:\wamp\bin\mysql\mysql5.6.12\include"
    Temporarily enhancing PATH to include DevKit...
    Building native extensions. This could take a while...
    Successfully installed mysql2-0.3.15
    1 gem installed
    Installing ri documentation for mysql2-0.3.15...
    Installing RDoc documentation for mysql2-0.3.15...

    ReplyDelete