mysql2 gem编译错误的MySQL客户端库

65

当我尝试通过我的Rails应用程序连接到MySQL服务器时,我遇到了以下错误

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)

我该如何纠正它?

8个回答

103
从您的Rails应用程序目录中,卸载并重新安装gem通常可以解决此问题,无需手动下载和移动文件。
> 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.

1
这是解决这个问题最简单的方法! - Benedikt
这个很好用。我还需要执行:sudo bundle install来编译mysql。 - Kien Pham
当我在Mac OS Mavericks上遇到类似的错误时,这对我很有用。谢谢! - Michael Giovanni Pumo
1
如果你需要让MySQL2在Windows上正常工作,这种方法是行不通的。如果它能够通过Bundle正确安装,那么就不需要重新安装了。 - glenatron
@glenatron 已经明白了。我只是想指出,在升级系统mysql后可能会出现相同的错误消息。在这种情况下,(稍微有些违反直觉的)卸载/重新安装将解决问题。 - chrislopresto

88

我遇到了和你一样的问题,或者说症状相同。

背景:我使用的是 Rails 3、mysql2 gem 和 MySQL 社区服务器版本 5.5.21(32 位),安装在我 Windows 机器上的本地环境中。我从 MySQL 安装中获取了客户端库(libmysql.dll),并将其复制到 ruby 安装的 bin 文件夹中。

当我运行 bundle exec rake db:create 时,我得到了和你一样的错误信息,我想:“嘿,我是从最新的 MySQL 发布中获取的客户端库,怎么会过时呢?”

当你使用gem install mysql2命令安装 gem 时,会显示一条有用的消息。不幸的是,如果你使用 Bundler 安装 gem,则 Bundler 会忽略该消息。以下是该消息:

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

按照这些说明,我解决了这个问题。

参考链接


这很重要,因为这是我反复遇到的问题,感谢您帮我省下了一天的挫败感。 - slotishtype
6
这对我起了作用,但下载位置是无效的。新的下载页面是http://dev.mysql.com/downloads/connector/c/。 选择“Windows(x86,32位),ZIP归档”。 - Spiralis
这对我也起作用了。我运行了gem install mysql2,它给了我最新的下载libmysql.dll的url。谢谢。 - Blake Mills
另外,请确保MySQL服务器、MySQL连接器c和mysql2 gem在相同的位平台上(32/64位)。 - konyak

10
如果你正在使用64位版本的MySQL和32位版本的Ruby,请查看这篇文章的解决方案:http://blog.mmediasys.com/2011/07/07/installing-mysql-on-windows-7-x64-and-using-ruby-with-it/。您需要从MySQL官网下载单个连接器,然后使用所下载的连接器编译MySQL或MySQL2。
对于Ruby 1.9.2:
gem install mysql --platform=ruby -- --with-mysql-dir=C:/mysql-connector-c-noinstall-6.0.2-win32

对于 Ruby 1.9.3:(显示 mysql2 变体)

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

请注意,此处使用正斜杠表示提取MySQL Connector/C的目录。


3

我曾经遇到过与此类似的问题:

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

我的问题是我在电脑上同时安装了5.5.29和5.6.17这两个版本,我也不知道为什么会这样。当我进行捆绑操作时,自动选择了5.5.29版本。我卸载了这个版本,然后重新安装了gem,问题就解决了。

2

我发现了这个问题完全不同的原因。我一直在使用mysql gem。我构建了mysql2 gem,但我忘记更新我的database.yml文件。使用mysql2 gem时,需要这样写:

  development:
    adapter: mysql2

而不是

  development:
    adapter: mysql

宝石已构建完成,但当我下一次运行rake时,我遇到了错误。

一旦你看到它,就显而易见了,但你会得到与这里讨论的相同的错误消息!

顺便说一下,在我的机器上构建mysql2 gem的命令比上面描述的要复杂一些:

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
在我的情况下,我在Windows上错误地将libmysql.dll从MySQL Server 5.5目录复制到ruby200/bin。正确的方法是从mysql-connector-c-noinstall-6.0.2-win32中复制libmysql.dll。

我认为这很相关,但我的经验却完全相反。我不得不使用MySQL Server 5.6目录中的libmysql.dll版本,并从使用MySQL Connector.C 6.1中的版本中获得了错误(两者都是由mysql安装程序安装的)。教训是要使用正确的版本,无论你的gem是建立在哪个版本上的。 - Segfault

1

补充现有答案(特别是Windows平台)。

为了使其正常工作,您需要两个东西。 mysql2 gemlibmysql.dll,并且您需要在版本方面将它们匹配起来。

mysql2 gem:安装时需要指定连接器。

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

它不需要从Oracle下载连接器。您只需要安装MySQL和其中的lib/include文件夹。然后将libmysql.dll放在railsinstaller bin文件夹下。

如果无法成功安装mysql2 gem => 对于我的情况,这是因为我的MySQL版本太旧了。所以我从Oracle获取了最新的MySQL。使用其中的lib include libmysql.dll您不需要升级数据库,可以将其保留在某个位置,并在生成了两个必需组件后继续使用它


0

1
嗨,Yulian,我建议你尝试用英语回答问题,这样可以获得更多的关注。http://meta.stackexchange.com/questions/13676/do-posts-have-to-be-in-english-on-stack-exchange - jperelli

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接