3

I installed MongoDB locally, and am stuck at this error when trying to use Mongoid/Moped on Rails 4 / Ruby 1.9.3 / Windows 7:

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

Probably caused from the resolved_address=nil - other forums stated this is from a typo in the hosts file - any idea where this file is located on Windows?

App contains just a simple model 'Article'

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

and the exception is caught at Articles.all.each.do |article| ...

Running mongod and mongo shell work fine outside of the app- so I'm assuming it's something with the configuration... My Rails server log spits out the following:

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

Far as I can tell, I didn't do anything with replica sets.

Rails.env is development and my config file (mongoid.yml) is:

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

Mongoid may not officially be ready for rails 4 yet - but has anybody had any luck crossing this hurdle?

corg
  • 428
  • 4
  • 14
  • I am facing same problem from `rails console` when doing `User.count` (doing any query to db). and `rails server` is working fine its weird problem error : "Moped::Node resolved_address=nil". – Raghvendra Parashar Jan 31 '15 at 05:22

1 Answers1

2

Review the localhost map in your hosts file or try it:

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

Information about Host file: http://en.wikipedia.org/wiki/Hosts_(file)

rjurado01
  • 5,337
  • 3
  • 36
  • 45
  • the entire hosts file (found at Windows/system32/drivers/etc) was commented out - i simply uncommented the line '127.0.0.1 localhost' and it all works perfectly, woo! – corg Jan 15 '14 at 16:36
  • I've remote host ip address instead of 0.0.0.0 when I place remote host IP it doest not resolve error. Any suggestions – Taimoor Changaiz May 28 '14 at 04:21
  • It can be tricky to edit the `hosts` file without breaking anything. Many editors may add some specific end of line characters or tabs/spaces, and this can break everything without you knowing. The safest solution is to use the plain old notepad to create a brand new host file, and add lines `xxx.xxx.xxx.xxx address` with only one space between the IP and the address. You can then test everything works well by opening a cmd (Win+R > cmd) and typing `ping address` – Cyril Duchon-Doris Apr 26 '15 at 22:28