Installing MySQL2 Gem on Windows
Asked Answered
D

3

6

I was having some issues getting the mysql2 gem to install on my Windows 8.1 machine. I followed the instructions in this post:

Ruby MYSQL2 gem installation on windows 7

to install the mysql2 gem, and I did not get any error messages.

I followed this with the

bundle install 

command, and confirmed that the following gems are installed (using bundle show), confirming that I have the following gems installed:

Using devise (3.2.2)
Using mysql2 (0.3.14)

Then I tried doing:

rails generate devise:install

and this is what I got:

C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/mysql2-0.3.14/lib/mysql2.rb:8:in `require': 126: The specified module could not be found.   
- C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/mysql2-0.3.14/lib/mysql2/mysql2.so (LoadError)
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/mysql2-0.3.14/lib/mysql2.rb:8:in `<top (required)>'
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/bundler-1.5.1/lib/bundler/runtime.rb:76:in `require'
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/bundler-1.5.1/lib/bundler/runtime.rb:76:in `block (2 levels) in require'
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/bundler-1.5.1/lib/bundler/runtime.rb:72:in `each'
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/bundler-1.5.1/lib/bundler/runtime.rb:72:in `block in require'
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/bundler-1.5.1/lib/bundler/runtime.rb:61:in `each'
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/bundler-1.5.1/lib/bundler/runtime.rb:61:in `require'
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/bundler-1.5.1/lib/bundler.rb:131:in `require'
    from C:/Users/Joseph/googledrive/projects/rails/test_new_devise/config/application.rb:7:in `<top (required)>'
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.0.2/lib/rails/commands.rb:43:in `require'
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.0.2/lib/rails/commands.rb:43:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

Any ideas?

Solution:

The answer is a combination of two answers from stackoverflow, plus modifications. The stackoverflow references are:

Ruby MYSQL2 gem installation on windows 7 ...mysql2/mysql2.so: [BUG] Segmentation fault ruby 2.0.0p247

Bottom line: to get mysql2 working in a 64 bit environment with Ruby 2 on Windows 8.1, you need to do the following:

  1. Clean up: the reason you're here is that you're probably been trying to install this gem, and it has failed, so you have some cleanup to do: gem uninstall mysql2

  2. Download Ruby 2.0 64 bit for Windows:

http://dl.bintray.com/oneclick/rubyinstaller/rubyinstaller-2.0.0-p353-x64.exe?direct

  1. Run the installer. Note the directory it installs to, and make sure it's in the User PATH. go to

    Control Panel > System and Security > System - Advanced System Settings > Environment Variables
    

and make sure the path to the bin directory of the ruby install directory is in the PATH environment variable for the LOCAL user (it's also OK if it's in the SYSTEM Path environment variable)

  1. Download the Ruby 2 64 bit Dev Kit:

http://cdn.rubyinstaller.org/archives/devkits/DevKit-mingw64-64-4.7.2-20130224-1432-sfx.exe

  1. Run the installer and note the location of the install directory. The instructions say that once you're done with the install, you need to run the devkitvars.bat file to set the environment variables. This DOES NOT work for Windows 8.1. You need to repeat the instructions to set the local path described in #2 above, to set the path environment variable for the local user to add the ruby dev kit bin directory, and the ruby dev kit mingw bin directory. For example, if the dev kit was installed to c:\ruby2devkit, and you installed ruby2 to the C:\Ruby200-x64 directory, then your edit your path statement to look like this:

    C:\Ruby200-x64\bin;C:\ruby2-devkit\bin;c:\ruby2-devkit\mingw\bin
    
  2. Next you need to install the mysql-connector. DO NOT download and run the self-installer. Instead, download the zip file and unpack it. Note the directory you unpacked it to (for the purposes of this post, let's assume you unpacked it to c:\mysql-connector:

http://dev.mysql.com/downloads/file.php?id=450612

  1. The libmysql.lib included in the MySQL Connector 64 bit is not compatible with the mingw64-gcc compiler. You need to generate mingw64 compatible libmysql.lib file.

  2. Download the tools you need

    https://structure-svm-map.googlecode.com/files/svm-map-win.zip

  3. Unzip this file to a local directory, let's assume that you unzipped it to c:\svm-map

  4. Edit the PATH environment path, as described earlier, to now look like this:

    C:\Ruby200-x64\bin;C:\ruby2-devkit\bin;c:\ruby2-devkit\mingw\bin;c:\svm-map;C:\svm-map\python-mingw-lib
    
  5. Generate the new mysql libraries:

    (make sure you're running as administrator)
    
    cd c:\mysql-connector\lib
    
    gendef.exe libmysql.dll
    
    dlltool -v --dllname libmysql.dll --def libmysql.def --output-lib libmysql.lib
    
    copy libmysql.dll C:\Ruby200-x64\bin
    
    copy libmysql.lib C:\Ruby200-x64\bin
    
  6. Install the gem as follows (note the use of forward slashes instead of backslashes, it will fail if you use backslashes):

    gem install mysql2 --platform=ruby -- '--with-mysql-dir="C:/mysql-connector/"'

Hope this helps, and hopefully someone will pay attention to the few Windows 8.1 users who want to do Ruby on Rails development.

Deviant answered 14/1, 2014 at 23:35 Comment(5)
try gem install mysql2Nereen
the mysql2 gem is already installed. Please read my question.Deviant
I run windows8.1 64-bit with 64-bit Ruby 2.0. I can confirm that this solution works. I had to take one additional set prior to #9. I had to delete the mysql2 gem's already installed by Bundle. Bundle failed to install them mysql gem when I attempted other peoples solutions, but it still created the gem directory and installed some files. Deleting them prior to step 9 made it work!Underplay
Thanks Peter Kirby. I will add this as step 0.Deviant
At step 10, I recommend perform the commands using administrator privileges. gendef.exe doesn't inform that the file libmysql.def was not generated.Shiri
S
7

When you installed the mysql2, did you download the lib files it needs?


I've not installed it on Windows 8, but Windows 7 needs to have this file to give mysql the dependencies requires to help it work. You can see various tutorials on how to do this, and I'll outline a simple overview here:

- Unzip the mysql connector file:

C:\mysql-connector-c-6.1.3-win32\ (or similar path WITHOUT any spaces)

- Run the mysql2 gem installer again:

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

Bottom line is I think your mysql2 gem isn't loading the files it requires to run, and probably doesn't show an error because it's Windows 8

Solubility answered 15/1, 2014 at 9:38 Comment(5)
I am using the 64 bit connector, not the 32 bit. Other than that, if you look at my original question, I said that I followed the instructions in the link I included, and that's a more expanded version of the suggestion you provide here. Only issue is that the example you provided does not work, because it's using Windows style directory separator, but the installer expects Linux style directory separators.Deviant
With windows 8 & connector 6.1.6 you must ONLY set mysql-dir: gem install mysql2 --platform=ruby -- '--with-mysql-dir="C:\mysql-connector-c-6.1.6-winx64"'Calciferous
Thanks bud - which version of Ruby are you running?Solubility
This works for me, widows 10 - ruby 3.0.2 / mysql 8Unsupportable
after this finally worked thanks to you @RichardPeck i ran into next issue related to tzinfo-data, and after commenting out the platforms part like this gem "tzinfo-data" # , platforms: %i[ mingw mswin x64_mingw jruby ] i finally got my app runing..now to switch to mssql :DKassey
T
3

I hope this will be of help when trying to install mysql2 using MariaDB on a Windows 10 machine.

I had a lot of issues trying the use the connector library for some reason. And in addition the libraries I could download were version 6 and the version of MariaDB I was using was using version 10 of the libraries.

I first downloaded and installed MariaDB.

I used the command

gem install mysql2 --platform=ruby -- --with-mysql-lib="path_to:\MariaDB 10.5\"

This compiled and installed the gem without any issues.

In the database.yml

default: &default adapter: mysql2 encoding: utf8 reconnect: false database: DBname pool: 5 username: DBuser password: DBpassword socket: tmp/mysql.sock

Tasset answered 5/2, 2021 at 14:6 Comment(0)
D
0

To people wants an more simple solution, worked for me:

Only install and unzip in a some dir that you remember (if you has this connector in a some place not is necessary to install):

http://dev.mysql.com/downloads/file.php?id=450612

And run:

gem install mysql2 --platform=ruby -- '--with-mysql-dir=C:\yourpath\mysql-connector-c-6.1.3-winx64'

For Rails:

When i try to install in ROR i got some different errors, the error was caused by the difference of versions in installation in the gemfile and version installed, when i ran the last code i getted this output:

Temporarily enhancing PATH for MSYS/MINGW...
Using msys2 packages: mingw-w64-ucrt-x86_64-libmariadbclient
Building native extensions with: '--with-mysql-dir=C:\MySQL\conn-c-6.1.3-win64'
This could take a while...
Successfully installed mysql2-0.5.6
Parsing documentation for mysql2-0.5.6
Installing ri documentation for mysql2-0.5.6
Done installing documentation for mysql2 after 1 seconds
1 gem installed

So i checked my gemfile and i will see the version i copied from

https://rubygems.org/gems/mysql2

For some reason was the 0.5.2

gem 'mysql2', '~> 0.5.2'

So i changed this version to installed 0.5.6 and with:

bundle install

All installed successfully.

The solution for rails was dumb, but it took me days to find this error because ruby don't say you what is the error in question.

Deepsea answered 13/4, 2024 at 3:45 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.