3

我在本地安装了 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 上的位置吗?

应用程序只包含一个简单的模型“文章”

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 可能还没有正式为 Rails 4 做好准备——但是有没有人幸运地跨越了这个障碍?

4

1 回答 1

2

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

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

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

于 2014-01-15T12:44:28.310 回答