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

时间:2015-08-20 22:59:31

标签: mysql ruby-on-rails gem mysql2

我在尝试捆绑安装时遇到错误。当列出' mysql2'在OSX Lion上的Gemfile上。我目前正在使用Rails构建应用程序。如果我运行'捆绑安装',它会永远挂起。首先,我尝试通过将gem文件修改为' gem' mysql2',:git =>来解决问题。 ' git://github.com/sodabrew/mysql2.git',:ref => ' a2800f'',如此处所示:https://github.com/brianmario/mysql2/pull/654。无限循环得到修复,我跑了“捆绑安装”。成功,但是当我跑去耙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)>'

我也尝试“安装mysql”#39;它工作,但然后在rake db:create&#39;,我收到了这个错误:

#<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)

那么我就赶紧卸载mysql&#39;而那个问题已经消失,但现在我有了第一个问题。

尝试&#39; gem install mysql2 -v&#39; 0.3.19&#39;&#39;也挂了。

可能是什么问题?

1 个答案:

答案 0 :(得分:3)

当您使用bundler时,您必须解决第一个问题(捆绑安装失败的原因)并使用bundler安装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

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

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