【问题标题】:Install mysql2 -v '0.3.19' via bundle (Gemfile) on rails在 rails 上通过 bundle (Gemfile) 安装 mysql2 -v '0.3.19'
【发布时间】:2015-11-14 18:41:25
【问题描述】:

在 OSX Lion 上的 Gemfile 中列出“mysql2”时尝试“捆绑安装”时出错。我正在用 Rails 构建一个应用程序。如果我运行“捆绑安装”,它会永远挂起。首先,我尝试按照建议将 gem 文件修改为 'gem 'mysql2', :git => 'git://github.com/sodabrew/mysql2.git', :ref => 'a2800f'' 来解决这个问题在这里:https://github.com/brianmario/mysql2/pull/654。无限循环得到修复,我成功运行了 'bundle install' 但是当我运行 'rake db:create' 时,我收到了这个错误:

rake aborted!
LoadError: dlopen(/Users/macbook/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/bundler/gems/mysql2-a2800f86754b/lib/mysql2/mysql2.bundle, 9): Library not loaded: /usr/local/lib/libmysqlclient.18.dylib
Referenced from: /Users/macbook/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/bundler/gems/mysql2-a2800f86754b/lib/mysql2/mysql2.bundle
Reason: image not found - /Users/macbook/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/bundler/gems/mysql2-a2800f86754b/lib/mysql2/mysql2.bundle
/Users/macbook/Desktop/tutorial/billingleap2/moviestore/config/application.rb:7:in `<top (required)>'
/Users/macbook/Desktop/tutorial/billingleap2/moviestore/Rakefile:4:in `<top (required)>'

我也尝试 'brew install mysql' 并且它成功了,但是在 'rake db:create' 上,我收到了这个错误:

#<Mysql2::Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)>
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "pool"=>5, "username"=>"root", "password"=>nil, "host"=>"localhost", "database"=>"moviestore_development"}, {:charset=>"utf8", :collation=>"utf8_unicode_ci"}
(If you set the charset manually, make sure you have a matching collation)
#<Mysql2::Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)>
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8","pool"=>5, "username"=>"root", "password"=>nil, "host"=>"localhost", "database"=>"moviestore_test"}, {:charset=>"utf8", :collation=>"utf8_unicode_ci"}
(If you set the charset manually, make sure you have a matching collation)

然后我'brew卸载mysql'并且那个问题消失了,但现在我有了第一个。

尝试 'gem install mysql2 -v '0.3.19'' 也会挂起。

可能是什么问题?

【问题讨论】:

  • 请在此处粘贴您的 Gemfile 和 gemfile.lock
  • mysql 服务器进程是否正在运行并接受连接?错误消息表明没有进程正在接受连接。两种猜测:1) 没有 mysql 守护进程在运行 2) 有一个 mysql 守护进程在运行,但它不接受任何本地连接。我认为1)更有可能。安装完mysql是怎么启动的?
  • 这里是 Gemfile:pastie.org/10365361,这里是 gemfile.lock:pastie.org/10365363

标签: mysql ruby-on-rails gem mysql2


【解决方案1】:

当您使用捆绑器时,您必须解决第一个问题(为什么捆绑安装失败)并使用捆绑器安装 mysql2 gem。一定有一些 gem 依赖问题需要查看。

并且,您应该运行:bundle exec rake db:migrate,因为您正在使用 bundler 和 Gemfile 等,这将确保您的 rake 任务在当前项目的上下文中运行。

更新:1

如果您已经运行:bundle install 成功并成功安装了mysql2 gem,则尝试运行:

`bundle exec rake db:create`

如果这导致您出现指定的错误(这意味着您的 libmysqlclient 库由于某种原因未加载):

rake aborted!
LoadError: dlopen(/Users/macbook/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/bundler/gems/mysql2-a2800f86754b/lib/mysql2/mysql2.bundle, 9): Library not loaded: /usr/local/lib/libmysqlclient.18.dylib
Referenced from: /Users/macbook/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/bundler/gems/mysql2-a2800f86754b/lib/mysql2/mysql2.bundle
Reason: image not found - /Users/macbook/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/bundler/gems/mysql2-a2800f86754b/lib/mysql2/mysql2.bundle
/Users/macbook/Desktop/tutorial/billingleap2/moviestore/config/application.rb:7:in `<top (required)>'
/Users/macbook/Desktop/tutorial/billingleap2/moviestore/Rakefile:4:in `<top (required)>'

那么你应该看看这个answerthis one too,它们对类似问题有一些规定的解决方案。试试那些看看是否有效。

更新:2

所以,mysql gem 有一些开发依赖项,需要先安装在您的系统中,然后才能真正使用 mysql。执行以下操作:

sudo gem install mysql-server mysql-client
sudo gem install libmysql-ruby libmysqlclient-dev
sudo gem install mysql

然后再试一次:

bundle install

更新:3

首先,找到你的socket文件:

mysqladmin variables | grep socket

它会给你这样的东西:

| socket                                            | /tmp/mysql.sock

然后,在 config/database.yml 中添加一行:

development:
  adapter: mysql2
  host: localhost
  username: root
  password: xxxx
  database: xxxx
  socket: /tmp/mysql.sock

【讨论】:

  • 嗨,拉基布尔!非常感谢! ;) 这就是 bundle install: checking for mysql_query() in -lmysqlclient... no 失败的原因。我不再在我的机器上安装 mysql 了。也许问题是我安装了它并用 brew 卸载了它?
  • 你在github上有项目吗?
  • @Sofía 请尝试我的更新:2,看看是否能解决您的问题。告诉我!
  • 我还用 brew 安装了 mysql:brew install mysql,然后(在 Gemfile 中)gem 'mysql2', '~&gt; 0.3.11',然后是 bundle install。成功!
  • 是的!我现在感觉很傻……但我猜你是从错误中学习的。非常感谢!一切都很棒!
猜你喜欢
  • 2021-11-04
  • 2014-04-23
  • 1970-01-01
  • 2014-03-07
  • 2017-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-26
相关资源
最近更新 更多