Mongo - Ruby Connection Problem

mongo - ruby connection problem

This is definitely due to your mongo server not running. Since you're on Ubuntu, try doing a sudo /etc/init.d/mongodb start and then see if your code works.

Ruby mongo driver: Catch MongoDB connection errors after a few seconds?

You're on the right track. server_selection_timeout is the correct option in this case -- it tells the driver how long to wait to find a suitable server before timing out. You can set that option on a new client in the Mongoid configuration file (config/mongoid.yml).

You want your configuration file to look something like this:

development: # (or production or whatever environment you're using)
clients:
default:
# your default client here
new_client: # the name of your new client with different options
database: mongoid
hosts:
- localhost:27017
options:
server_selection_timeout: 6
...

Read the Mongoid Configuration documentation to learn more about setting up config files.

Then, you want to use the new client you defined to perform your query, and rescue any Mongo::Error::NoServerAvailable errors.

begin
User.with(client: 'new_client').collection.find({}).count()
rescue Mongo::Error::NoServerAvailable => e
# perform error handling here
end

Note that this starts up a new Mongo::Client instance, which is an expensive operation if done repeatedly. I would recommend that you close the extra client once you are done using it, like so:

Mongoid::Clients.with_name('new_client').close

MongoDB connection error when I try to install any gem

Your config file says to connect to localhost:28017, but that's the web console. You probably want localhost:27017.

mongoid connection issue

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.

Mongo Connection Failure

CloudFoundry.com upgraded from MongoDB 1.8 to 2.0 last week, and that's likely affecting your app. I will be checking with the CF.com engineering team and filing a bug if necessary.

However, since you're manually specifying connection information in mongo.yml, have you disabled auto-config of services? To do so, create the file config/cloudfoundry.yml and add:

autoconfig: false

For more information on auto-config of services in Ruby apps, see http://blog.cloudfoundry.com/2012/03/12/using-cloud-foundry-services-with-ruby-part-1-auto-reconfiguration/



Related Topics



Leave a reply



Submit