0

I've been trying to use Mongoid with Rails on Ubuntu. Mongodb was working fine for me with rails app 3.0 to 3.2,

today suddenly, i am getting error "/home/pravinmishra/.rvm/gems/ruby-1.9.2-p318/gems/mongo-1.5.2/lib/mongo/connection.rb:413:in `connect': Failed to connect to a master node at localhost:27017 (Mongo::ConnectionFailure)"

Many times i face this issues, and below commands works for me.

sudo rm /var/lib/mongodb/mongod.lock
sudo -u mongodb mongod -f /etc/mongodb.conf --repair
sudo start mongodb
sudo status mongodb 

but today i have not luck.

Previous couple of day back i changed my etc/hosts file and added 27.0.0.1 localhost.me

127.0.0.1 localhost.me:3000 localhost ubuntu
127.0.0.1 localhost
127.0.1.1 ubuntu
127.0.0.1 localhost.me locahost ubuntu

I thought, this creating problem. to over come i changed mongoid.config file localhost to localhost.me, but still getting error "/home/pravinmishra/.rvm/gems/ruby-1.9.2-p318/gems/mongo-1.5.2/lib/mongo/connection.rb:413:in `connect': Failed to connect to a master node at localhost.me:27017 (Mongo::ConnectionFailure)"

mongoid.yml file

defaults: &defaults
  host: localhost
  allow_dynamic_fields: false

development:
  <<: *defaults
  database: xxx_development

test:
  <<: *defaults
  database: xxx_test

# set these environment variables on your prod server
production:
 # host: <%= ENV['MONGOID_HOST'] %>
 # port: <%= ENV['MONGOID_PORT'] %>
 # username: <%= ENV['MONGOID_USERNAME'] %>
 # password: <%= ENV['MONGOID_PASSWORD'] %>
 # database: <%= ENV['MONGOID_DATABASE'] %>


 # set these environment variables on your prod server
production:
  uri: <%= ENV['MONGOHQ_URL'] %>

any suggestion would be appreciated, thanks in advance..!!

6
  • please post your mongoid.yml config file where you have defined the database connection configurations
    – abhas
    Jul 9, 2012 at 16:48
  • defaults: &defaults host: localhost allow_dynamic_fields: false development: <<: *defaults database: kidslink_development test: <<: *defaults database: kidslink_test # set these environment variables on your prod server production: # host: <%= ENV['MONGOID_HOST'] %> # port: <%= ENV['MONGOID_PORT'] %> # username: <%= ENV['MONGOID_USERNAME'] %> # password: <%= ENV['MONGOID_PASSWORD'] %> # database: <%= ENV['MONGOID_DATABASE'] %> # set these environment variables on your prod server production: uri: <%= ENV['MONGOHQ_URL'] %>
    – user1002512
    Jul 9, 2012 at 16:57
  • @ abhash, tks for quick reply. i edited question and added mongoid.yml file. Once take a look..!
    – user1002512
    Jul 9, 2012 at 17:02
  • From ubuntu, if you type in "mongo" does it connect to mongo's console shell? Jul 9, 2012 at 17:15
  • @ Jesse , Getting same error. MongoDB shell version: 2.0.1 connecting to: test Mon Jul 9 22:47:06 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84 exception: connect failed
    – user1002512
    Jul 9, 2012 at 17:18

1 Answer 1

1

Your /etc/hosts file is broken

27.0.0.1 localhost.me:3000 localhost ubuntu
  1. Your IP is incorrect. localhost is at 127.0.0.1, not 27.0.0.1
  2. You can't put port numbers into this file. Think of hosts as a local DNS override.

Because Mongoid looks at 27.0.0.1 for a running MongoDB, it can't find one. And fails to connect.

3
  • hey,it's 127.0.0.1 localhost.me:3000 localhost ubuntu only. i did mistake when post question. I edited. sorry for that.
    – user1002512
    Jul 9, 2012 at 17:13
  • Then your MongoDB is probably not starting correctly. Please take a look at your /var/log/mongodb/mongodb.log
    – iblue
    Jul 9, 2012 at 18:09
  • and there is a typo locahost next to the line in question
    – Vao Tsun
    Feb 1, 2017 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.