65

When try to connect to the mysql server through my rails application, I get the following error

D:/Program_Files/Ruby192/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': 
Incorrect MySQL client library version! This gem was compiled for 6.0.0 but the client library is 5.0.27. (RuntimeError)

How can I rectify it?

8 Answers 8

103

Uninstalling and reinstalling the gem will often solve this issue with no need to download and move files around by hand. From your rails app directory:

> gem uninstall mysql2

You have requested to uninstall the gem:
    mysql2-0.3.11
database_cleaner-0.9.1 depends on [mysql2 (>= 0)]
If you remove this gems, one or more dependencies will not be met.
Continue with Uninstall? [Yn]  Y
Successfully uninstalled mysql2-0.3.11

> bundle install

Fetching gem metadata from http://rubygems.org/......
Fetching gem metadata from http://rubygems.org/..
Using rake (0.9.2)
Using i18n (0.6.1)
... <SNIP> ...
Installing mysql2 (0.3.11) with native extensions
... <SNIP> ...
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
5
  • 1
    This is the easiest way to fix this problem!
    – Benedikt
    Jul 19, 2013 at 3:12
  • this works great. I also have to do: sudo bundle install for mysql to compile.
    – Kien Pham
    Oct 15, 2013 at 19:21
  • This worked for me when I got a similar error on Mac OS Mavericks. Thanks! Dec 22, 2013 at 22:27
  • 1
    This doesn't work if you have to make MySQL2 behave with Windows. If it can install correctly through Bundle, then it won't need to be reinstalled.
    – glenatron
    Apr 10, 2014 at 10:42
  • @glenatron Understood. I just wanted to point out that this same error message may occur after upgrading the system mysql. In this case the (slightly counterintuitive) uninstall/reinstall will do the trick. Apr 15, 2014 at 20:27
88

I had the same problem as you, or at least the symptom was the same.

Background: I was using Rails 3, the mysql2 gem, and MySQL community server version 5.5.21 (32-bit) installed locally on my Windows machine. I grabbed the client library (libmysql.dll) from the MySQL installation and copied it to my ruby installation's bin folder.

When I ran bundle exec rake db:create, I got the same error message as you and I thought "Hey, how can the client library be outdated when I got it from the latest MySQL release?"

There's a helpful message that is shown when you gem install mysql2. Unfortunately, if you install the gem with Bundler, Bundler eats the message. Here it is:

=========================================================================
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

Following these instructions solved the problem for me.

Referenced link

4
  • This is important as it is a problem I have come across repeatedly, thanks for saving me a day's frustration. Mar 21, 2012 at 11:33
  • 6
    This worked for me, with the exception that the download location is broken. New download page dev.mysql.com/downloads/connector/c. Select "Windows (x86, 32-bit), ZIP Archive"
    – Spiralis
    Jul 10, 2012 at 12:07
  • This worked fore me as well. I ran gem install mysql2 and it gave me the latest url to download the libmysql.dll. Thx Aug 13, 2012 at 1:20
  • Also, make sure that the MySQL server, MySQL connector c, and mysql2 gem are on the same bit platform (32/64-bit)
    – konyak
    Dec 5, 2013 at 15:54
10

If you are using 64bit version of mysql and 32bit version of ruby, then check this solution on http://blog.mmediasys.com/2011/07/07/installing-mysql-on-windows-7-x64-and-using-ruby-with-it/

You basically have to download a single connector from mysql website, and compile mysql or mysql2 with connector you downloaded.

for Ruby 1.9.2:

gem install mysql --platform=ruby -- --with-mysql-dir=C:/mysql-connector-c-noinstall-6.0.2-win32

for Ruby 1.9.3: (showing mysql2 variant)

gem pristine mysql2 -- --with-mysql-config=C:\mysql-connector-c-noinstall-6.0.2-win32    

Note the use of forward slashes for the directory where MySQL Connector/C was extracted.

3

I had an issue just like this:

Incorrect MySQL client library version! This gem was compiled for 5.5.29 but the client library is 5.6.17.

The issue for me was that I had both versions, 5.5.29 and 5.6.17, installed on my machine. I have no idea how. When I bundled it automatically chose the 5.5.29 version. I uninstalled that one and then reinstalled my gem and that fixed the issue.

2

I discovered a completely different cause for this problem. I had been using the mysql gem. I built the mysql2 gem but I forgot to update my database.yml. With the mysql2 gem, it needs to say:

  development:
    adapter: mysql2

rather than

  development:
    adapter: mysql

The gem built, but I got the error when I next ran rake.

Obvious once you've seen it, but you get the same error message as discussed here!

By the way, the command to build the mysql2 gem on my machine was a bit more complicated than described above:

gem install mysql2 -- --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" --with-mysql-dir="c:\mysql-connector-c-noinstall-6.0.2-win32"
1

in my case on windows, incorrectly copied libmysql.dll from MySQL Server 5.5 directory to ruby200/bin. correct is to copy libmysql.dll from mysql-connector-c-noinstall-6.0.2-win32.

1
  • I think this is relevant, but I had the exact opposite experience. I had to use the version of libmysql.dll from the MySQL Server 5.6 directory and got this error from using the one out of MySQL Connector.C 6.1 (both installed by the mysql installer). The lesson is to use the right one, which ever your gem was built against.
    – Segfault
    Apr 3, 2015 at 23:17
1

To add to the existing answer. ( windows platform specifically )

In order to get this working, you need 2 things. The mysql2 gem and libmysql.dll and you need to match them up in terms of the version.

mysql2 gem: When you install it you need to specify the connector.

     gem install mysql2 --platform=ruby -- 
     --with-mysql-lib="d:\mysql\lib" --with-mysql-include="d:\mysql\include"

it does not need to be the connector downloaded from oracle. All you need is a mysql installation and the lib/include folder underneath it. then put the libmysql.dll under railsinstaller bin folder.

if it didn't work to make you install mysql2 gem succesfully => for my case it is because my mysql is too old. So I get the latest mysql from oracle. use the lib include libmysql.dll under it. you don't really need to upgrade your database, you can keep it somewhere and continue to use it after you generated the 2 required components

0

I had the same problem , and I have solved the follows:

1 :: Download the zip the following link: https://dev.mysql.com/downloads/connector/c/

2 :: decompress the file ( libmysql.dll ) in the " Provider" folder of project.

3 :: Volve one to run the command bundle install

and ready , solved !

https://dev.mysql.com/downloads/connector/c/

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.