Rails 3: Mongoid::Errors::NoSessionsConfig - ruby-on-rails

I'm unable to use mongoid in production mode in my rails application. I'm using Rails 3.2.14 and mongoid 3.0.23
Mongoid::Errors::NoSessionsConfig (
Problem:
No sessions configuration provided.
Summary:
Mongoid's configuration requires that you provide details about each session that can be connected to, and requires in the sessions config at least 1 default session to exist.
Resolution:
Double check your mongoid.yml to make sure that you have a top-level sessions key with at least 1 default session configuration for it. You can regenerate a new mongoid.yml for assistance via `rails g mongoid:config`.
Example:
  development:
    sessions:
      default:
        database: mongoid_dev
        hosts:
          - localhost:27017
):
Everything works fine in developement mode. Here's the mongoid.yml.
production:
sessions:
default:
database: myapp_production
hosts:
- localhost:27027
username: username
password: password
options:
raise_not_found_error: false
use_activesupport_time_zone: true
identity_map_enabled: true
development:
# Configure available database sessions. (required)
sessions:
# Defines the default session. (required)
default:
# Defines the name of the default database that Mongoid can connect to.
# (required).
database: myapp_development
# Provides the hosts the default session can connect to. Must be an array
# of host:port pairs. (required)
hosts:
- localhost:27017
test:
sessions:
default:
database: chanderi_test
hosts:
- localhost:27017
options:
consistency: :strong
# In the test environment we lower the retries and retry interval to
# low amounts for fast failures.
max_retries: 1
retry_interval: 0
How should I set up the database?

In your mongoid.yml in development section you have Mongoid specific options as empty, if you want to put some options then paste it after options: otherwise remove this line as due to that it is not able to detect test environment and giving error. Your mongoid.yml should be like this
production:
sessions:
default:
database: myapp_production
hosts:
- localhost:27027
username: username
password: password
options:
raise_not_found_error: false
use_activesupport_time_zone: true
identity_map_enabled: true
development:
# Configure available database sessions. (required)
sessions:
# Defines the default session. (required)
default:
# Defines the name of the default database that Mongoid can connect to.
# (required).
database: myapp_development
# Provides the hosts the default session can connect to. Must be an array
# of host:port pairs. (required)
hosts:
- localhost:27017
test:
sessions:
default:
database: chanderi_test
hosts:
- localhost:27017
options:
consistency: :strong
# In the test environment we lower the retries and retry interval to
# low amounts for fast failures.
max_retries: 1
retry_interval: 0

Related

Warning on updating Mongoid Gem to Version 5 Issue

I have updated the mongoid gem to 5 from mongoid-4 on rails 4 application. I am facing the following warning on app restart
W, [2017-02-15T13:59:49.356541 #14483] WARN -- : MONGODB Unsupported client option 'max_retries'. It will be ignored.
W, [2017-02-15T13:59:49.356739 #14483] WARN -- : MONGODB
Unsupported client option 'retry_interval'. It will be ignored.
W, [2017-02-15T13:59:49.356877 #14483] WARN -- : MONGODB Unsupported client option 'username'. It will be ignored.
How to update Mongoid Yml to remove the warning?
Also This is current YML file
staging:
clients:
default:
database: chillr_api
hosts:
- localhost:27017
options:
read:
mode: :nearest
# In the test environment we lower the retries and retry interval to
# low amounts for fast failures.
max_retries: 1
retry_interval: 0
username: 'username'
password: 'username'
Okey.Seems that max_retries and retry_interval are Moped configuration . Since Moped is taken away in Mogoid 5 This could be removed from configuration .Yml example

rails application connect to a mongoid replicaset

I'm having difficulty connecting to a mongoid replicaset from a rails app.
I have the following config:
dev1:
clients:
default:
database: opera_bounties_dev1
hosts:
- 10.0.0.1:27017
- 10.0.0.2:27017
# read: :secondary
# slave_ok: true
options:
# read: :secondary
replicaSet: operaeventrsX
consistency: :strong
pool_size: 200
# slave_ok: true
# connect: :replica_set
I can connect to the primary just fine, but if I shut down the primary, I cannot connect to the secondary. Now if I connect via mongo shell to the secondary and issue rs.slaveOk(), I can connect to the secondary for that one session. I need my rails app to connect to mongo with slaveOk always. setting slave_ok: true in the configuration (above) doesn't do it... What is the trick here?
The working config is:
dev1:
clients:
default:
database: dbname_dev1
hosts:
- 10.0.0.1:27017
- 10.0.0.2:27017
options:
read:
mode: :secondary_preferred
consistency: :strong
pool_size: 200
connect: :replica_set
replica_set: replicaSetName

What are all possible keys for database.yml

I've just discovered that the reconnect: true configuration option is possible in the database.yml file.
What other possible configuration options are there? Is there a complete reference for all options?
Known key examples:
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: foo
password: bar
reconnect: true
socket: /var/sock/thing.sock
development:
<<: *default
database: app_development
I don't think there is any place that just lists them but I checked the ActiveRecord's ConnectionAdapaters. Keep in mind that options change which database you use, but this was listed within the MySQL connection adapter.
List of Options for MySQL
:host - Defaults to "localhost".
:port - Defaults to 3306.
:socket - Defaults to "/tmp/mysql.sock".
:username - Defaults to "root"
:password - Defaults to nothing.
:database - The name of the database. No default, must be provided.
:encoding - (Optional) Sets the client encoding by executing "SET NAMES <encoding>" after connection.
:reconnect - Defaults to false (See MySQL documentation: http://dev.mysql.com/doc/refman/5.7/en/auto-reconnect.html).
:strict - Defaults to true. Enable STRICT_ALL_TABLES. (See MySQL documentation: http://dev.mysql.com/doc/refman/5.7/en/sql-mode.html)
:variables - (Optional) A hash session variables to send as SET ##SESSION.key = value on each database connection. Use the value +:default+ to set a variable to its DEFAULT value. (See MySQL documentation: http://dev.mysql.com/doc/refman/5.7/en/set-statement.html).
:sslca - Necessary to use MySQL with an SSL connection.
:sslkey - Necessary to use MySQL with an SSL connection.
:sslcert - Necessary to use MySQL with an SSL connection.
:sslcapath - Necessary to use MySQL with an SSL connection.
:sslcipher - Necessary to use MySQL with an SSL connection.
The github for Rails ActiveRecord adapters, https://github.com/rails/rails/tree/master/activerecord/lib/active_record/connection_adapters
edit:
Adding what #pjrebsch commented below. You can also see MySQL options on the Mysql2 gem's Readme

raise_not_found_error configuration to false not working

I am playing around with rails and mongodb and running into the issue where find_by returns an error when it does not find a result. I have already triple checked to make sure it's under options and not session/options.
Can anyone help? thanks
already checked to make sure it's set under options, not session options
Error
Mongoid::Errors::DocumentNotFound:
Problem:
Document not found for class VinDecode with attributes {:numOfDoors=>"3"}.
Summary:
When calling VinDecode.find_by with a hash of attributes, all attributes provided must match a document in the database or this error will be raised.
Resolution:
Search for attributes that are in the database or set the Mongoid.raise_not_found_error configuration option to false, which will cause a nil to be returned instead of raising this error.
Below is my mongoid.yml
development:
sessions:
default:
database: myapp_development
hosts:
- localhost:27017
options:
options:
raise_not_found_error: false
test:
sessions:
default:
database: myapp_test
hosts:
- localhost:27017
options:
read: primary
# In the test environment we lower the retries and retry interval to
# low amounts for fast failures.
max_retries: 1
retry_interval: 0
I think that you have two issues:
1) You need to un-indent your options
2) You should move your options below your test declaration
I ran into this same issue where it was working for my production code, but not working in my test environment and the solution was because the options were defined before the test declaration.
If you are planning on writing tests for your code which I highly recommend, you should move your options like below
development:
sessions:
default:
database: myapp_development
hosts:
- localhost:27017
options:
test:
sessions:
default:
database: myapp_test
hosts:
- localhost:27017
options:
read: primary
# In the test environment we lower the retries and retry interval to
# low amounts for fast failures.
max_retries: 1
retry_interval: 0
options:
raise_not_found_error: false

Mongoid only working in development. Throws error both is test and production

When I start my rails console as:
$ RAILS_ENV=development rails console
every thing seems to be working fine.
Mongoid is able to connect to mongodb and fetch records.
But with:
$ RAILS_ENV=test rails console
$ RAILS_ENV=production rails console
it's throwing up errors as:
Rack::File headers parameter replaces cache_control after Rack 1.5.
/usr/local/lib/ruby/gems/1.9.1/gems/mongoid-3.0.16/lib/mongoid/criteria.rb:585:in `check_for_missing_documents!': (Mongoid::Errors::DocumentNotFound)
Problem:
Document(s) not found for class Actor with id(s) 50e5259f53c205d815000001.
Summary:
When calling Actor.find with an id or array of ids, each parameter must match a document in the database or this error will be raised. The search was for the id(s): 50e5259f53c205d815000001 ... (1 total) and the following ids were not found: 50e5259f53c205d815000001.
Resolution:
Search for an id that is in the database or set the Mongoid.raise_not_found_error configuration option to false, which will cause a nil to be returned instead of raising this error when searching for a single id, or only the matched documents when searching for multiples.
My config/mongoid.yml has the exact same set of lines for all three environments.
I'm not able to figure out why it isn't able to connect in test and production.
Update:
Mongoid.yml
development:
sessions:
default:
database: tgmd
hosts:
- localhost:27017
test:
sessions:
default:
database: tgmd
hosts:
- localhost:27017
production:
sessions:
default:
uri: <%= ENV['MONGO_URL'] %>
I temporarily solved the issue by placing a:
options:
raise_not_found_error: false
in production:
Also moved out a few scripts from jobs/ folder. It worked then.
Can anyone enlighten me on this?
I think your ENV['MONGO_URL'] contains something that is not correct.
Try to use your development database in production:
production:
sessions:
default:
database: tgmd
hosts:
- localhost:27017
if it work. Check your ENV['MONGO_URL']

Resources