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
- Your IP is incorrect.
localhost
is at 127.0.0.1, not 27.0.0.1 - 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
Rails with Paypal Permissions and Paypal Express Checkout
Invalid Multibyte Char (Utf-8) Error, Ruby
Could Not Find a Valid Gem 'Rails' (>= 0) in Any Repository
How to Call Java API from Ruby 1.8 or 1.9
How to Authorize a Service Account for Google Calendar API in Ruby
What Is the &: of &:Afunction Doing
How to Find Where Gem Files Are Installed
How to Step Out of a Loop with Ruby Pry
Rails: Convert Utc Datetime to Another Time Zone
What Is Mattr_Accessor in a Rails Module
Mailer Unable to Access Reset_Token in User Model
How to Use Ruby's Readlines.Grep for Utf-16 Files
Tzinfo-Data Present But Not Seen
Having Trouble Installing Any Ruby 1.9.X (With Rbenv) on MAC Osx Due to Psych Yaml Parse Errors
Open_Id_Authentication - "Openidauthentication.Store Is Nil. Using In-Memory Store." Problem
Ruby SASS, Unable to Resolve Dependancies
Generate All Possibles Combinations of an Array with a Length Within a Given Range