Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to Mongo in Replica set mode

Tags:

mongodb

I have a standalone Mongo instance running a replica set. I can't seem to connect and run any queries in the Mongo shell however. I get the following:

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

I set SlaveOk like so:

db.getMongo().setSlaveOk()

..but I still get an error:

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

I can't seem to find a straight answer on Google: how do I connect to my replica set using the mongo shell?

like image 359
James Avatar asked Mar 11 '13 18:03

James


3 Answers

I got the same problem and solved it using

rs.initiate()
like image 105
Andres Avatar answered Oct 16 '22 16:10

Andres


If you are connecting to a node in a replica set that is not the master, you need to explicitly tell the client that it is ok that your are not connected to a master..

You can do this by calling

rs.slaveOk()

You can then perform your query.

Note that you will only be able to perform queries, not make changes to the repository, when connected to a slave node.

like image 44
grillp Avatar answered Oct 16 '22 17:10

grillp


You are connected to a node that is neither in state secondary or primary. This node could be an arbiter or possibly a secondary in recovery mode. For example, if I had a replica set of 3 nodes, (where there is one primary, a secondary and an arbiter) I would get the same error if I had connected to the arbiter and issued a query even after I had set slaveOK true. The shell's command line prompt should indicate what state the node you are connected is in:

foo:ARBITER> db.test.find()
error: {
    "$err" : "not master or secondary; cannot currently read from this replSet member",
    "code" : 13436
}
like image 20
Bryan Reinero Avatar answered Oct 16 '22 16:10

Bryan Reinero