带有Rails 4的Mongoid - 无法连接到主节点... resolved_address = nil

时间:2014-01-15 03:28:22

标签: mongodb ruby-on-rails-4 mongoid moped

我在本地安装了MongoDB,在尝试在Rails 4 / Ruby 1.9.3 / Windows 7上使用Mongoid / Moped时遇到了这个错误:

"Could not connect to a primary node for replica set #<Moped::Cluster:27191916 @seeds=[<Moped::Node resolved_address=nil>]>"

可能是由于 resolved_address = nil - 其他论坛声称这是来自hosts文件中的拼写错误 - 这个文件位于Windows上的任何想法?

App只包含一个简单的模型'Article'

class Article
  include Mongoid::Document
  field :name, type: String
  field :content, type: String
end

,异常发生在Articles.all.each.do |article| ...

运行mongod和mongo shell在应用程序之外工作正常 - 所以我假设它是配置的东西...我的Rails服务器日志吐出以下内容:

Started GET "/articles" for 127.0.0.1 at 2014-01-14 19:04:18 -0800
Processing by ArticlesController#index as HTML
  MOPED: Could not resolve IP for: localhost:27017 runtime: n/a
  MOPED: Retrying connection attempt 1 more time(s). runtime: n/a
  MOPED: Could not resolve IP for: localhost:27017 runtime: n/a
  Rendered articles/index.html.erb within layouts/application (10294.6ms)
Completed 500 Internal Server Error in 10325ms

据我所知,我没有对副本集做任何事情。

Rails.env是development,我的配置文件(mongoid.yml)是:

development:
  sessions:
    default:
      database: mid_dev
      hosts:
        - localhost:27017
      options:
  options:

Mongoid可能尚未正式为轨道4做好准备 - 但有没有人有幸遇到过这个障碍?

1 个答案:

答案 0 :(得分:2)

查看hosts文件中的localhost映射或尝试:

development:
  sessions:
    default:
      database: mid_dev
      hosts:
        - 0.0.0.0:27017

有关主机文件的信息:http://en.wikipedia.org/wiki/Hosts_(file)