MongoDB – How to Permanently Enable Read Access for Secondary Node

mongodb

Upon trying to connect mongodb secondary node from application :

08:11:2018 16:01:00.552 – error: A hook (orm) failed to load!
08:11:2018 16:01:00.558 – error: Error: Failed to connect to MongoDB. Are you sure your configured Mongo instance is running?
Error details:
MongoError: no valid seed servers in list

I can access the secondary DB by manually entering the command rs.slaveOk(); , but the same wont suffice as I need the application to have dynamic read access everytime the DB is bieng queried

Best Answer

08:11:2018 16:01:00.552 - error: A hook (orm) failed to load! 08:11:2018 16:01:00.558 - error: Error: Failed to connect to MongoDB. Are you sure your configured Mongo instance is running? Error details: MongoError: no valid seed servers in list

As per your it seems like that your mongod server has not started.

As per MongoDB documentation here All read preference modes except primary may return stale data because secondaries replicate operations from the primary with some delay. [1] Ensure that your application can tolerate stale data if you choose to use a non-primary mode.

MongoDB drivers support five read preference modes.

Read Preference Mode    Description

primary                 Default mode. All operations read from the current replica set primary.                                                       
                        Multi-document transactions that contain read operations must use read preference primary.

                        All operations in a given transaction must route to the same member.

primaryPreferred        In most situations, operations read from the primary but if it is unavailable, operations read from secondary members.

secondary               All operations read from the secondary members of the replica set.

secondaryPreferred      In most situations, operations read from secondary members but if no secondary members are available, operations read from the primary.

nearest                Operations read from member of the replica set with the least network latency, irrespective of the member’s type.

As MongoDB documented setSlaveOk() This allows the current connection to allow read operations to run on secondary members. See the readPref() method for more fine-grained control over read preference in the mongo shell.

db.getMongo().setSlaveOk()

For further your ref here, here and here