rails中的Mongodb:无法连接到副本的主节点

时间:2014-03-03 05:32:32

标签: ruby-on-rails mongodb mongoid

我是rails和mongodb的新手。我已经在我的rails项目名称中生成了mongod模型作为论坛 当我尝试使用rails控制台中的以下命令添加记录时

f = Forum.new 
f.topic_name = "my_topic"
f.save

我收到以下错误

Moped::Errors::ConnectionFailure: Could not connect to a primary node for replic
a set #<Moped::Cluster:25470936 @seeds=[<Moped::Node resolved_address=nil>]>

我在端口27017上运行mongod服务器 以下是我的mongoid.yml文件

development:
  # Configure available database sessions. (required)
  sessions:
    # Defines the default session. (required)
    default:
      # Defines the name of the default database that Mongoid can connect to.
      # (required).
      database: local
      # Provides the hosts the default session can connect to. Must be an array
      # of host:port pairs. (required)
      hosts:
        - localhost:27017
      options:
        # Change the default write concern. (default = { w: 1 })
        # write:
        # w: 1

        # Change the default consistency model to primary, secondary.
        # 'secondary' will send reads to secondaries, 'primary' sends everything
        # to master. (default: primary)
        # read: secondary_preferred

        # How many times Moped should attempt to retry an operation after
        # failure. (default: 30)
        # max_retries: 30

        # The time in seconds that Moped should wait before retrying an
        # operation on failure. (default: 1)
        # retry_interval: 1
  # Configure Mongoid specific options. (optional)
  options:
    # Includes the root model name in json serialization. (default: false)
    # include_root_in_json: false

    # Include the _type field in serializaion. (default: false)
    # include_type_for_serialization: false

    # Preload all models in development, needed when models use
    # inheritance. (default: false)
    # preload_models: false

    # Protect id and type from mass assignment. (default: true)
    # protect_sensitive_fields: true

    # Raise an error when performing a #find and the document is not found.
    # (default: true)
    # raise_not_found_error: true

    # Raise an error when defining a scope with the same name as an
    # existing method. (default: false)
    # scope_overwrite_exception: false

    # Use Active Support's time zone in conversions. (default: true)
    # use_activesupport_time_zone: true

    # Ensure all times are UTC in the app side. (default: false)
    # use_utc: false
test:
  sessions:
    default:
      database: local
      hosts:
        - localhost:27017
      options:
        read: primary
        # In the test environment we lower the retries and retry interval to
        # low amounts for fast failures.
        max_retries: 1
        retry_interval: 0

怎么做??

1 个答案:

答案 0 :(得分:0)

查看您的hosts文件中的localhost映射,或尝试使用0.0.0.0:27017作为主机。

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