0

I am trying to capture mongo replica set state from mongo shell. E.g.

[root@server ~]$ mongo
MongoDB shell version v3.6.8
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.8
rs0:SECONDARY>

I'd like to capture that SECONDARY keyword, and to do it in one command. I've tried something like mongo > output, however it requires a manual ctrl+c to terminate the process.

I have tried parse rs.status() output to display one secondary node for mongodump destination

But the solution doesn't always work. Different mongo version can output different message. ARBITER node does not have authentication data either.

1

The output you are trying to capture is the default prompt for the mongo shell in interactive mode.

As at MongoDB 4.0 there isn't a built-in helper to return replica set member status as a string, but you can derive the same status in JavaScript.

For example, create the following as getstate.js:

    var mystate = 'UNKNOWN';

    // Note: isMaster command does not require authentication
    var isMaster = db.isMaster();

    if (isMaster.msg == "isdbgrid") {
        mystate = "mongos";
    } else if (isMaster.ismaster && !isMaster.setName) {
        mystate = 'STANDALONE';
    }

    if (isMaster.setName) {
        if (isMaster.secondary) {
            mystate = 'SECONDARY';
        } else if (isMaster.arbiterOnly) {
            mystate = "ARBITER";
        } else if (isMaster.primary === isMaster.me) {
            mystate = 'PRIMARY';
        }
    }

    print(mystate);

Now invoke mongo using --quiet to suppress any startup messages or warnings:

$ mongo localhost:27017 getstate.js --quiet
PRIMARY

$ mongo localhost:27018 getstate.js --quiet
SECONDARY

$ mongo localhost:27019 getstate.js --quiet
ARBITER
  • This is a nice script, I will implement it. However I still have problem with arbiter when authentication enabled. Since arbiter does not have auth data, this method will return Error: Authentication failed. Is there a way to determine whether mongo is arbiter or not, before running this script? – Howard Lee Nov 16 '18 at 18:08
  • @HowardLee The isMaster command (as used by drivers and clients) intentionally does not require authentication. I simplified the script example so you should now be able to run against any replica set member without auth. – Stennie Nov 20 '18 at 12:54
  • NOTE: in general I would advise against using arbiters for production deployments where possible. An arbiter provides election availability without contributing to write availability so there are important caveats if a data bearing member is unavailable. For example, it is no longer possible to acknowledge majority writes in a Primary-Secondary-Arbiter config with one data member down (which in turn can impact cache pressure and usage of some newer features like Change Streams in 3.6+). A replica set with only a healthy Primary and Arbiter also does not have any active replication. – Stennie Nov 20 '18 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.