rails application connect to a mongoid replicaset - ruby-on-rails

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

Related

Rails CQL cannot connect to AWS Keyspaces (AWS Cassandra)

I am trying to connect from a Ruby on Rails application to AWS Keyspaces (AWS Cassandra), but I cannot manage to do it. I use the cequel gem and generated the config/cequel.yml which contains a similar thing to the following:
development:
host: "CONTACT_POINT"
username: "USER"
password: "PASS"
port: 9142
keyspace: key_development
max_retries: 3
retry_delay: 0.5
newrelic: true
ssl: true
server_cert: 'config/certs/AmazonRootCA1.pem'
replication:
class: NetworkTopologyStrategy
datacenter1: 3
datacenter2: 2
durable_writes: false
(Credentials where used in another app and they work which is working as expected.)
when I try to run:
rake cequel:keyspace:create
I get the following errors:
Cassandra::Errors::NoHostsAvailable: All attempted hosts failed: x.xxx.xxx.xxx (Cassandra::Errors::ServerError: Internal Server Error)
Set the dc to us-east-1 . drop the replication definition.

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

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

Rails 3: Mongoid::Errors::NoSessionsConfig

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

Resources