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

2021-11-20 00:00:00 macos ruby-on-rails ruby mysql

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

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:ruby gems, using rvm. I have everything working well with sqlite.

现在我想尝试将内容转换为 MySQL,因为这是我进行大部分开发工作的内容.在我的 Gemfile 中,我用 mysql2 替换了 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:

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

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

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

$ 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

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

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.

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

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?

推荐答案

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

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

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

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

There are many blogs with install_name_tool, which won't work for me because I'm on 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

相关文章