user1002512
user1002512

Reputation:

mongoid connection issue

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..!!

Upvotes: 0

Views: 2162

Answers (1)

iblue
iblue

Reputation: 30404

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.

Upvotes: 1

Related Questions