OSX 上的 rails + MySQL:库未加载:libmysqlclient.18.dylib

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10557507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:20:36  来源:igfitidea点击:

rails + MySQL on OSX: Library not loaded: libmysqlclient.18.dylib

mysqlruby-on-railsrubymacos

提问by George Armhold

I'm just starting out with Ruby (and rails). I did the setup according to http://ruby.railstutorial.org/ruby-on-rails-tutorial-book#sec:rubygems, using rvm. I have everything working well with sqlite.

我刚刚开始使用 Ruby(和 Rails)。我根据http://ruby.railstutorial.org/ruby-on-rails-tutorial-book#sec:rubygems进行了设置,使用rvm. 我的一切都与 sqlite 配合良好。

Now I'd like to try converting things over to MySQL, since that's what I do most of my development with. In my Gemfile I've replaced sqlite with mysql2:

现在我想尝试将事物转换为 MySQL,因为这是我进行的大部分开发工作。在我的 Gemfile 中,我用 mysql2 替换了 sqlite:

group :development, :test do
#  gem 'sqlite3', '1.3.5'
  gem 'mysql2'
  gem 'rspec-rails', '2.9.0'
end

But when I try to create the DB for rails in MySQL I get:

但是当我尝试在 MySQL 中为 rails 创建数据库时,我得到:

$ rake db:create --trace
rake aborted!
dlopen(/Users/username/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dylib
  Referenced from: /Users/username/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle
  Reason: image not found - /Users/username/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle

I've seen other postings recommending re-installing MySQL via homebrew (mine was installed via a downloadable DMG), but I'd prefer not to do that as I have several other databases in there already for other non-ruby projects.

我已经看到其他帖子建议通过自制软件重新安装 MySQL(我的是通过可下载的 DMG 安装的),但我不想这样做,因为我已经有其他几个数据库用于其他非 ruby​​ 项目。

I do in fact have the file that Rails is looking for; it's installed in /usr/local/mysql/lib/libmysqlclient.18.dylib. What's the best way to tell Rails how to locate it?

我确实有 Rails 正在寻找的文件;它安装在/usr/local/mysql/lib/libmysqlclient.18.dylib. 告诉 Rails 如何定位它的最佳方式是什么?

回答by atejeda

The solution is pretty easy; Add the library path in your ~/.bash_profile or ~/.profile file:

解决方案非常简单;在 ~/.bash_profile 或 ~/.profile 文件中添加库路径:

MYSQL=/usr/local/mysql/bin
export PATH=$PATH:$MYSQL
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH

If it is still not working (this work for me):

如果它仍然不起作用(这对我有用):

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

There are many blogs with install_name_tool, which won't work for me because I'm on OSX Lion:

有很多博客install_name_tool,这对我不起作用,因为我使用的是 OSX Lion:

sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/bin/indexer
sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/bin/search

回答by TinMonkey

In El Capitan I got ln: /usr/lib/libmysqlclient.18.dylib: Operation not permitted

在 El Capitan 我得到了 ln: /usr/lib/libmysqlclient.18.dylib: Operation not permitted

In El Capitan /usr/lib/now has a restricted flag and can't be written to without disabling security so I just put the link in /usr/local/libinstead.

在 El Capitan/usr/lib/现在有一个受限制的标志,如果不禁用安全就不能写入,所以我只是把链接放在里面/usr/local/lib

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

Rails server is running fine again.

Rails 服务器再次运行良好。

回答by Giuseppe

While the title of this question describes precisely the problem I encountered, the circumstances are different from those described in the previous answers, and so was the solution.

虽然这个问题的标题准确地描述了我遇到的问题,但情况与之前答案中描述的情况不同,解决方案也是如此。

In my case (El Capitan, mysql installed via homebrew), a brew update && brew upgradecaused the mysql package to be upgraded to 5.7.10 (from 5.6.x).

在我的情况下(El Capitan,mysql 通过自制软件安装),brew update && brew upgrade导致 mysql 包升级到 5.7.10(从 5.6.x)。

The upgrade caused libmysqlclient.18.dylibto be replaced with libmysqlclient.20.dylib, but the mysql2gem was still relying on the former.

升级导致libmysqlclient.18.dylib替换为libmysqlclient.20.dylib,但mysql2gem 仍然依赖于前者。

To fix the problem I did: gem uninstall mysql2 && gem install mysql2

为了解决我所做的问题: gem uninstall mysql2 && gem install mysql2

Please note that similar problems can occur with different homebrew-managed libraries (see my own answer to this, for example)

请注意,不同的自制软件管理的库可能会出现类似的问题(例如,请参阅我自己的回答

回答by Joseph

sudo ln -s /usr/local/mysql-5.5.25-osx10.6-x86_64/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

That worked for me. I installed MySQL from a dmg file.

那对我有用。我从 dmg 文件安装了 MySQL。

回答by Greg Benner

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

Worked for me. All the similar ones didn't.

为我工作。所有类似的都没有。

回答by sealocal

I ran into this problem after a complete removal and then fresh install of MySQL. Specifically:

在完全删除然后全新安装 MySQL 后,我遇到了这个问题。具体来说:

Library not loaded: /usr/local/opt/mysql/lib/libmysqlclient.20.dylib

I had not even touched my Rails app.

我什至没有接触过我的 Rails 应用程序。

Reinstalling the mysql2gem solved this problem.

重新安装mysql2gem 解决了这个问题。

$ gem uninstall mysql2
$ gem install mysql2 -v 0.3.18 # (specifying the version found in my Gemfile.lock)

[MySQL 5.7.10, Rails 4.0.0, Ruby 2.0.0, Mac OS X Yosemite 10.10]

[MySQL 5.7.10、Rails 4.0.0、Ruby 2.0.0、Mac OS X Yosemite 10.10]

回答by arjunswaj

If you are using MySQL installed from HomeBrew in El Capitan, then you should link it as follows:

如果您使用的是从 El Capitan 的 HomeBrew 安装的 MySQL,那么您应该将其链接如下:

sudo ln -sf /usr/local/Cellar/mysql/5.6.27/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib

回答by Abhishek Pande

For MySql 5.6 installed from DMG on Mavericks

对于在 Mavericks 上从 DMG 安装的 MySql 5.6

sudo ln -s /usr/local/mysql-5.6.14-osx10.7-x86_64/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

回答by ingconti

I confirm patch from Abhishek doeswork.

我确认来自 Abhishek 的补丁确实有效。

it work for Yosemite, too.

它也适用于优胜美地。

note: instead of linking to a particular version of mysql, use the fact mysql already built symlink:

注意:不要链接到特定版本的 mysql,而是使用 mysql 已经构建的符号链接:

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

this solution does work for Xcode and C API.

此解决方案适用于 Xcode 和 C API。

回答by tagaism

For those who are using brew. Just link you mysql version with "--force" option.

对于那些使用 brew 的人。只需将您的 mysql 版本与“--force”选项联系起来。

brew link mysql56 --force