12

After updating MySQL to 5.5 using apt-get, the mysql2 gem stopped working.

Here's the error:

Incorrect MySQL client library version!
This gem was compiled for 5.5.17 but the client library is 5.1.58. (RuntimeError)

I tried reinstalling with mysql_config but it doesn't seem to make a difference..

gem install mysql2 -- --with-mysql-config=/usr/bin/mysql_config

Correspondingly, I tried telling bundle to compile mysql2 with mysql-config but still, the error remains..

bundle config build.mysql2 --with-mysql-config=/usr/bin/mysql_config
bundle install

When I tried an older version of the gem (v0.2.6) the rails console opens but crashes once I run any type of query.


Here's the full error stack when trying the most current version of the mysql2 gem:

[marco@linode:/www] 07:29:00 AM: rails c
/users/marco/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/mysql2-0.3.10/lib/mysql2.rb:9:in `require': Incorrect MySQL client library version! This gem was compiled for 5.5.17 but the client library is 5.1.58. (RuntimeError)
  from /users/marco/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/mysql2-0.3.10/lib/mysql2.rb:9:in `<top (required)>'
  from /users/marco/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:68:in `require'
  from /users/marco/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:68:in `block (2 levels) in require'
  from /users/marco/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:66:in `each'
  from /users/marco/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:66:in `block in require'
  from /users/marco/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:55:in `each'
  from /users/marco/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:55:in `require'
  from /users/marco/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler.rb:122:in `require'
  from /www/config/application.rb:7:in `<top (required)>'
  from /users/marco/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/railties-3.1.1/lib/rails/commands.rb:38:in `require'
  from /users/marco/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/railties-3.1.1/lib/rails/commands.rb:38:in `<top (required)>'
  from script/rails:6:in `require'
  from script/rails:6:in `<main>'

Edit: I removed all mysql2 gemspecs from cache, uninstalled all mysql-related packages via apt-get and reinstalled everything afresh, then tried again to install the gem with the mysql-config flag .. but it all proved futile. The error remains.

Edit 2: As suggested by John Bachir, I confirmed that mysql_config contains MySQL version 5.5.

Marco
  • 4,345
  • 6
  • 43
  • 77

15 Answers15

12

Late to the party, but I just did

gem uninstall mysql2
gem install mysql2

Gem path was

$ bundle show mysql2
/var/lib/gems/1.9.1/gems/mysql2-0.3.16
  • http://stackoverflow.com/questions/22518764/incorrect-mysql-client-library-version/23479503#23479503 – François Drolet Aug 02 '14 at 04:48
  • Thanks, it worked fine for me on ruby 2.0.0 and rails 4 application. Error produced probably due to updated version of mysql server. – Shahzad Tariq Apr 27 '15 at 07:47
  • Didn't work, as the Gem was not compiled again. After MySQL client is upgraded, the gem needs to be compiled against the new libraries. – Tiago Franco Sep 09 '16 at 10:20
8

I have encounter the same error when using Ubuntu Server 11.04 and Percona Server, what have I done was:

replace /usr/lib/libmysqlclient.so.16.0.0 with /usr/lib/libmysqlclient.so.18.0.0

ShiningRay
  • 1,008
  • 7
  • 12
  • Do you have a file called `libmysqlclient_r.so.18`? I replaced all the `libmysqlclient.so.16` files with the "18" version, but I don't have a corresponding "18" file for `/usr/lib/libmysqlclient_r.so.16` so maybe that's the culprit. – Marco Nov 24 '11 at 01:43
  • @Marco in my system, libmysqlclient_r is a symbol link to libmysqlclient – ShiningRay Nov 24 '11 at 01:44
  • @Marco The actual file is `libmysqlclient.so.16.0.0`, the other files like `libmysqlclient.so.16`, `libmysqlclient.so` are all symbol links. – ShiningRay Nov 24 '11 at 01:48
  • 1
    While this will fix this particular problem, I would be extremely weary solving it in this manner. This is probably going to break other bits of software that were actually compiled against version 16. I would suggest `apt-cache purge libmysqlclient16` if it doesn't remove anything you don't need. – johnf Nov 25 '11 at 08:13
  • @johnf can't do that, the mysql packages are dependent on it.. see pastie: http://pastie.org/2921551 – Marco Nov 25 '11 at 22:34
7

I had this problem and just needed to recompile the gem. I don't know if there's a more programmatic way with bundler, but I just removed the compiled gem and then re-ran bundler:

$ bundle show mysql2
/usr/local/rvm/gems/ruby-1.9.3-p385/bundler/gems/mysql2-32dd7e5bbeba
$ rm -rf /usr/local/rvm/gems/ruby-1.9.3-p385/bundler/gems/mysql2-32dd7e5bbeba
$ bundle
seb
  • 3,646
  • 26
  • 21
3

I came across a similar problem:

Incorrect MySQL client library version! This gem was compiled for 5.6.12 but the client library is 5.5.28.

And my MySQL version on Mac:

$ mysql --version
mysql  Ver 14.14 Distrib 5.6.12, for osx10.8 (x86_64) using  EditLine wrapper

and the mysql2 gem installed by bundle install is mysql2-0.3.13

I solved this problem by just setting the mysql2 gem to an older version in the Gemfile:

gem 'mysql2', '0.2.6'

If '0.2.6' does not work for you, you could try another old version number.

Ry-
  • 218,210
  • 55
  • 464
  • 476
Xiao Li
  • 51
  • 3
1

Rather than moving library files, you might also check the package list for your system -- if you upgraded to 5.5 using apt-get, it's likely that you still have 5.1 libraries lying around that you don't need anymore at all. For example, on a system where we encountered a similar issue, we still had the 5.1 library:

$ sudo dpkg -l | grep mysql
ii  libmysqlclient-dev               5.5.13-rel20.4-136.lucid                 Percona Server database development files
ii  libmysqlclient16                 5.1.61-rel13.2-431.lucid                 Percona Server database client library
ii  libmysqlclient18                 5.5.13-rel20.4-136.lucid                 Percona Server database client library

Rather than copying the 5.5 library over the 5.1 library, you should be able to remove it:

$ sudo apt-get remove libmysqlclient16

For us, this has been more reliable than the library renaming solution proposed earlier.

Austin Mills
  • 485
  • 3
  • 6
  • But "apt-get remove libmysqlclient16" will also remove MySQL server! Debian/Ubuntu has stupid dependencies here. I installed mariadb 5.5 server and client. Ubuntu 11.10 server installed both libmysqlclient16 (for MySQL 5.1) and libmysqlclient18 (for MySQL 5.5). That's because mariadb-client-5.5 depends on libdbd-mysql-perl. And libdbd-mysql-perl depends on libmysqlclient16. So finally: mysql client 5.5 depends on... mysql client 5.1 which is insane and confusing and it does not allow for installation of Ruby mysql2 gem. – hipertracker Apr 13 '12 at 23:39
1

I meet this problem again and I could not solve it with my previous method(use an older version mysql2 gem). So I try another method as these step:

1.find which mysql_config that your gem library depend on.

mysql_config situation is different in different people's machine, and one people machine can have several mysql_config, so there is no standard mysql_config path for everyone. As this question for example, the mysql_config version is "5.1.58", so you can search "mysql" file in root to find which has related with "5.1.58":

cd /

sudo find -name "mysql" ./

2.install mysql2 gem library again with specific mysql_config path find in step 1

gem install mysql2 -- --with-mysql-config=<%your_specific_mysql_config_path%>
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
Xiao Li
  • 51
  • 3
  • Installing with right config file worked for me. On OSX it is in /usr/local/mysql-version. Full path and command was sudo gem install mysql2 -- --with-mysql-config=/usr/local/mysql-5.5.27-osx10.6-x86_64/bin/mysql_config. As discussed also here: https://www.ruby-forum.com/topic/1440086 – drKreso Mar 01 '14 at 10:15
1

Other than making sure that only the libmysqlclient18 library was installed (and not 16), what made it work for me was installing libmysqlclient18-dev (the dev headers).

This is with Percona Server 5.5 on Ubuntu Precise.

Vaz
  • 678
  • 6
  • 16
1

I tried all the answers above and my final approach was fairly simple -- delete the entire bundled gem stack and reinstall everything. That finally got around the mysql client issue. i.e.

rm -rf /Users/sjohnson/.rvm/gems/ruby-1.9.3-p484@adp_rails3
bundle install

fuzzygroup
  • 1,109
  • 12
  • 12
1

if you know c program,you can remove this message. omit it.

  1. open this mysql.gem mysql-2.9.1.gem\data.tar.gz\data.tar\ext\mysql_api\mysql.c

  2. find "This gem was compiled for" ,delete this if sentence .

    if (lib[i] != MYSQL_SERVER_VERSION[i]) { Line 1897: rb_raise(rb_eRuntimeError, "Incorrect MySQL client library version! This gem was compiled for %s but the client library is %s.", MYSQL_SERVER_VERSION, lib); }
    

you need set sys-variable for local gemfile install on windows system . SET HOMEDRIVE=c: ERROR: While executing gem ... (Errno::EINVAL) Invalid argument @ rb_file_s_stat - U:/

install local gem file . C:\Rails>gem install --local mysql-2.9.1.gem --install-dir=c:/tmp --platform= ruby -- --with-mysql-dir=C:/Rails/mysql-connector-c mysql-connector-c visit mysql http://dev.mysql.com/downloads/connector/c/ . you don't care version.

try again. require 'mysql'

thor
  • 21,418
  • 31
  • 87
  • 173
Gao.YD
  • 11
  • 2
1

On Mac using Brew verify the versions installed on your machine:

ls /usr/local/Cellar/mysql*

Them remove the wrong version such as:

brew uninstall mysql-client

Remove the gem:

gem uninstall mysql2

Run bundle again:

bundle install
monteirobrena
  • 2,562
  • 1
  • 33
  • 45
0

In my case, I had various files from libmysqlclient15 (dated 2009) and libmysqlclient16 (dated 20012) left in /usr/lib, possibly from a botched Ubuntu upgrade.

Removing the libmysqlclient15 files (which were linked to libmysqlclient15.so) solved my problem.

Paul Schreiber
  • 12,531
  • 4
  • 41
  • 63
0

On my machine I had to uninstall the gem, then re-direct the symlink /usr/local/mysql to the correct Homebrew version:

ln -s /usr/local/bin/mysql /usr/local/mysql

Then run bundle install.

Steve Lorek
  • 111
  • 1
  • 1
  • 7
0

Reinstall libmysqlclient-dev with apt remove libmysqlclient-dev && apt install libmysqlclient-dev fixed for me

Fangxing
  • 5,716
  • 2
  • 49
  • 53
0

This is for anyone that stumble upons this question after 11 years.

Finally figured out that an update on mariadb-connector-c caused my rails fail to start up with a Bundler::GemRequireError.

It was complaining with the following:

Gem Load Error is: Incorrect MySQL client library version! This gem was compiled for 10.6.8-MariaDB but the client library is 3.2.7.

The version numbers that are suggested in the error are widely different, which felt strange. This wasn't a new install, my dev env was working for months.

After going through all sorts of hoops, depleting SO resources and even getting some AI involved, I finally tried downgrading the package. (My next step was going to be removing MariaDB altogether, but fortunately we didn't come to that)

sudo dnf downgrade mariadb-connector-c-3.1.13-2.fc35 --allowerasing

I verified that the mysql cc_version went down

/usr/bin/mysql_config

Then I removed and installed the gem

gem uninstall mysql2

gem install mysql2 -v '0.3.21' -- --with-mysql-config=/usr/bin/mysql_config

That's it. rails was working again.

FYI: I am on Fedora 35; I use RVM; and the rails version was 4.2

A couple of things that may fix some other people's issues:

Reinstalling Gem:

gem uninstall mysql2

gem pristine mysql2 or bundle install or gem install mysql2 -v '0.3.21' --source 'https://rubygems.org/'

Checking Package History:

dnf history provides a list of recent dnf changes... some have empty fields in "Command Line" column... those still have updates, etc in them... you can refer to them with dnf history info <ID>

Can Toraman
  • 1
  • 1
  • 2
0

Make sure that /usr/bin/mysql_config is indeed the one that came with 5.5.

John Bachir
  • 22,495
  • 29
  • 154
  • 227
  • Good suggestion. Unfortunately I checked and it does indeed have the correct version number in it. – Marco Nov 16 '11 at 05:06
  • I have, as well as the cached gemspec files and vendor/cache and anything else I could find remotely related to the gem. I even uninstalled the libmysqlclient files, as well as the gem, before reinstalling everything but to no avail. – Marco Nov 16 '11 at 05:13
  • 1
    Switch to Postgres. (just kidding) – John Bachir Nov 16 '11 at 05:27