How to properly shutdown the replica set in m103 vagrant box

What is the correct way to shutdown the replica set in the vagrant box?

If there is a command, or order of db.shutdownServer() to be executed.

There does not appear to be a single command to shutdown the whole replica set (cluster) at once. That’s kind of the point :slight_smile:

So yeah, shut them down one by one by connecting to each one individually.

Hi SweeGiap,

You have to shutdown the secondaries first and then primary. You can check members of replica set using rs.status() and by looking at that, run db.shutdownServer() on secondaries and then in last, shutdown Primary.

mongo admin --username m103-admin --password m103-pass --port 27017 --eval "rs.status().members"

Identify “SECONDARY” nodes in members key like:

  "name" : "m1.example.net:27017",
  "health" : 1,
  "state" : 2,
  "stateStr" : "PRIMARY",
  ......
  ],
  "name" : "m1.example.net:27027",
  "health" : 1,
  "state" : 2,
  "stateStr" : "SECONDARY",
  ......
  ],
  "name" : "m1.example.net:27037",
  "health" : 1,
  "state" : 2,
  "stateStr" : "SECONDARY",
  ......

In this case, nodes with port 27027 and 27037 are secondaries. So shutdown one by one like:

mongo admin --username m103-admin --password m103-pass --port 27027 --eval "db.shutdownServer()"

//output
MongoDB shell version v4.0.0-rc7
connecting to: mongodb://m1.example.net:27027/admin
MongoDB server version: 4.0.0-rc7

server should be down...

Do it for 27037 and then for PRIMARY (27017).

Hope it helps!

Kanika

2 Likes

SweeGiap just type exit and press enter or ctrl+c …

That only quits the Mongo shell. It does not shutdown any of the replica set members (MongoD).

kill -2 after exit from mongo

That’s a very generic thing to do. Remember, the original question was: “What is the correct way to shutdown the replica set in the vagrant box?

The answer is: there is no single command that shuts down the whole replica set. You have to shut them down individually. This can be done by either connecting to each one with the Mongo shell and issuing db.shutdownServer(), or by killing their specific process ID from the Linux commandline. Remember that many people on these forums are not Linux-save @Hakan_27544; they won’t know to add a PID after the “kill -2” like you and I know.

1 Like

Thanks for this.

I was hoping that there was a single command to shutdown the primary and it was going to shutdown the secondaries :slight_smile:

Might need to script this .

A lazy system administrator can be a very good system administrator! Automation is key to success when you’re in charge of large environments :slight_smile:

Hopefully I will never be in charge of a large farm, but it’s good to know, because you never know when the admin gets sick :slight_smile:

edit:

1 Like

Question 1. regarding a shutdown, if you shutdown the first of the secondaries, it shouldn’t force an election, and potentially change the primary of the remaining 2 nodes , which should be primary on 27001 and secondary on 27002 ?

Question 2. I know rs.status().members tells you in the members of the replica set, but is there a version of the command which tells you which members of the replica set are still running? I see we have a heartbeat, but you have to compare times and such forth.

Question 3. If you shutdown the replica set, incorrectly, can it recover gracefully? This is a answer I think I will be finding out tomorrow night :slight_smile:

Check the “Health” field :wink:

Thank you, at least I know there is a easy way to find out …

I was running through lesson, and something I did changed the primary. It taught me to check everytime rs.status and rs.isMaster :slight_smile:

1 Like