Ruby:mysql2-Gem无法正常工作(Mac OS X Snow Leopard,Ruby 1.9.2)

时间:2011-03-27 02:05:48

标签: mysql ruby-on-rails ruby gem

我刚刚编译了ruby并安装了rubygems,mysql2和rails,它运行得很好。但每当我尝试启动rails服务器时,我都会收到以下错误消息:

/usr/local/ruby/lib/ruby/gems/1.9.1/gems/mysql2-0.2.6/lib/mysql2.rb:7:in `require': dlopen(/usr/local/ruby/lib/ruby/gems/1.9.1/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dylib (LoadError)
  Referenced from: /usr/local/ruby/lib/ruby/gems/1.9.1/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle
  Reason: image not found - /usr/local/ruby/lib/ruby/gems/1.9.1/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle
  from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/mysql2-0.2.6/lib/mysql2.rb:7:in `'
  from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/bundler-1.0.10/lib/bundler/runtime.rb:68:in `require'
  from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/bundler-1.0.10/lib/bundler/runtime.rb:68:in `block (2 levels) in require'
  from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/bundler-1.0.10/lib/bundler/runtime.rb:66:in `each'
  from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/bundler-1.0.10/lib/bundler/runtime.rb:66:in `block in require'
  from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/bundler-1.0.10/lib/bundler/runtime.rb:55:in `each'
  from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/bundler-1.0.10/lib/bundler/runtime.rb:55:in `require'
  from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/bundler-1.0.10/lib/bundler.rb:120:in `require'
  from /Users/filzo/Documents/rails/blog/config/application.rb:7:in `'
  from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.0.5/lib/rails/commands.rb:28:in `require'
  from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.0.5/lib/rails/commands.rb:28:in `block in '
  from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.0.5/lib/rails/commands.rb:27:in `tap'
  from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.0.5/lib/rails/commands.rb:27:in `'
  from script/rails:6:in `require'
  from script/rails:6:in `'

我尝试了几种可能的解决方案(例如:http://railsforum.com/viewtopic.php?pid=23125#23125和libmysqlclient.18.dylib);但没有什么对我有用。我希望你能帮助我。

编辑: 这似乎解决了我的问题:

sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/ruby/lib/ruby/gems/1.9.1/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle 

反正;它是MySQL或ruby代码中的错误吗?

6 个答案:

答案 0 :(得分:32)

一种简单而最好的方法是通过命令创建软链接 -

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

http://rorguide.blogspot.com/2011/07/getting-error-library-not-loaded.html

答案 1 :(得分:15)

这似乎是已知的(并且不固定!)bug in the MySQL binary distribution

虽然在mysql2.bundle文件上运行install_name_tool会有效,但如果删除并重建它们,则会回到原点。此外,您必须将此修复程序应用于链接它的任何其他内容。更好的解决方案是将问题解决到库本身:

$ sudo install_name_tool -id \
  /usr/local/mysql/lib/libmysqlclient.18.dylib \
  /usr/local/mysql-5.5.12-osx10.6-x86_64/lib/libmysqlclient.18.dylib

删除并重建mysql2 gem:

$ gem uninstall mysql2
$ gem install mysql2

...将接受对库的更改,一切都应该正常工作,而不必修改任何环境变量。

答案 2 :(得分:13)

如果您在Mac OS X上使用RVM(一个很好的选择) - 这是最终适用于我的神奇命令。

sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib /Users/*USERNAME*/.rvm/gems/ruby-1.9.2-p180/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle

根据您当前的设置,在语法中替换 USERNAME 。 如果您的mysql gem版本不同,也会改变它。

答案 3 :(得分:4)

install_name工具的一个更好的替代方法是将mysql lib的路径添加到您的DYLD_LIBRARY_PATH环境变量中。在/ etc / profile中:

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib

或者,由于我也安装了Oracle Instant Client,请执行:

export ORACLE_HOME="/usr/local/oracle/instantclient_10_2"
export DYLD_LIBRARY_PATH=$ORACLE_HOME
export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:/usr/local/mysql/lib

答案 4 :(得分:3)

尝试创建这个符号链接,因此rake可以找到正确的MySQL客户端库。

ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

答案 5 :(得分:2)

我不确定这是否有帮助,但我使用的是OS X 10.7.1 Lion。在我的情况下,我打开了一个终端窗口,转到/Library/Ruby/Gems/1.8/gems/mysql2-0.3.7并且我一旦RVM咆哮说它遇到了新的或修改过的.rvmrc文件。该文件的内容是:

rvm use 1.9.2@mysql2 --create

它问我是否想要信任这个.rvmc文件。我说是的,它返回了以下内容:

Using /Users/dan/.rvm/gems/ruby-1.9.2-p290 with gemset mysql2

一旦完成,我运行了以下命令:

sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib /Library/Ruby/Gems/1.8/gems/mysql2-0.3.7/lib/mysql2/mysql2.bundle 

有一次,我准备出发了。希望这有帮助!