James
James

Reputation: 6509

Connecting to Mongo in Replica set mode

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?

Upvotes: 13

Views: 23803

Answers (7)

Bryan Reinero
Bryan Reinero

Reputation: 141

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
}

Upvotes: 7

Rony Cherian
Rony Cherian

Reputation: 114

I got the same error while running aggregate() on staging server with two replica sets.I think you need to change the read preference to 'secondaryPreferred'.

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

Upvotes: 0

kakoma
kakoma

Reputation: 1203

This error is only displayed when you are running an instance that's part of a replica set in standalone mode without completely removing it from the replica set. e.g. You restart your instance on a different port but don't remove the --repSet option when starting it. This starts it but neither as a primary nor as a secondary, hence the error not master or secondary;

Depending on what you intended to do initially, either 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 start it on a different port WITHOUT the --repSet option

Upvotes: 0

Andres
Andres

Reputation: 4501

I got the same problem and solved it using

rs.initiate()

Upvotes: 13

Rajkamal Subramanian
Rajkamal Subramanian

Reputation: 6964

I also got the error. But when i tried connecting to the secondary node using the machine name instead of 'localhost' or 127.0.0.1 the error went away.

Upvotes: 0

grillp
grillp

Reputation: 1283

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.

Upvotes: 9

Martin Klosi
Martin Klosi

Reputation: 3296

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

Upvotes: 0

Related Questions