在 rails 上通过 bundle (Gemfile) 安装 mysql2 -v '0.3.19'

Install mysql2 -v '0.3.19' via bundle (Gemfile) on rails

我在 OSX Lion 上的 Gemfile 上列出 'mysql2' 时尝试 'bundle install' 时出错。我目前正在使用 Rails 构建一个应用程序。如果我 运行 'bundle install',它永远挂起。首先,我尝试通过将 gem 文件修改为 'gem 'mysql2', :git => 'git://github.com/sodabrew/mysql2.git', :ref => [=30 来解决这个问题=]',如此处建议: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 uninstall mysql' 那个问题就解决了,但现在我遇到了第一个问题。

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

可能是什么问题?

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

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

更新:1

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

`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)>'

那么你应该看看这个 answer and this 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

首先,找到你的套接字文件:

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