Connect to Mongo in replica configuration mode

I have a standalone Mongo instance that has a set of replicas installed. However, it seems that I cannot connect and run any requests in the Mongo shell. I get the following:

error: { "$err" : "not master and slaveOk=false", "code" : 13435 }

I install SlaveOk as follows:

db.getMongo().setSlaveOk()

.. but I still get the error message:

 error: { "$err" : "not master or secondary; cannot currently read from this replSet member", "code" : 13436 } 

I cannot find a direct answer on Google: how do I connect to my replica installed using the mongo shell?

+8
mongodb
source share
7 answers

If you connect to a node in a replica set that is not leading, you need to explicitly tell the client that it is normal, that you are not connected to the wizard.

You can do this by calling

rs.slaveOk ()

Then you can fulfill your request.

Please note that you will be able to fulfill requests, and not make changes to the repository when connecting to the slave node.

+8
source share

I have the same problem and I decided to use it with

 rs.initiate() 
+8
source share

You are connected to a node that is not in a secondary or primary state. This node can be an arbiter or perhaps secondary in recovery mode. For example, if I had a set of replicas of 3 nodes (where there is one primary, secondary, and arbiter), I would get the same error if I connected to the arbiter and issued a request even after I set slaveOK true. At the command line The command line should indicate in which state the node is connected:

 foo:ARBITER> db.test.find() error: { "$err" : "not master or secondary; cannot currently read from this replSet member", "code" : 13436 } 
+6
source share

Have you tried: db.getMongo (). setSlaveOk (true)

0
source share

I also got an error. But when I tried to connect to the secondary node using the machine name instead of "localhost" or 127.0.0.1, the error disappeared.

0
source share

This error is displayed only when starting an instance of the part of the replica that is installed offline, without completely removing it from the replica set. for example, you restart the instance on a different port, but do not remove the -repSet parameter when it starts. This begins it, but not as primary, nor secondary, therefore, the error is not leading or secondary;

Depending on what you intended to do first, restart the instance on the correct port and with the correct --repSet option. This adds it to the replica set and gets rid of this error.

If you intended to run the instance as standalone for some time (say, to create an index), then run it on another port WITHOUT the -repSet option

0
source share

I got the same error when running aggregate () on an intermediate server with two sets of replicas. I think you need to change the reading preference to "secondaryPreferred".

Just put .read ('secondaryPreferred') after the request function.

0
source share

All Articles