mongoDB – replica set admin cheat sheet

Connect with

MongoDB replica set admin cheat sheetCheatsheet of MongoDB replica set admin. MongoDB admin uses this frequently for managing MongoDB replica set. In replica set, there are various types of commands which you have to run for a maintenance and operation point of view.

1. Overview of MongoDB replica set admin

Well, first of all, let us understand what is a replica set cluster, in MongoDB. There is a concept of primary-secondary or master-slave, primary/master can accept write and read but ideally, you should configure readPrefered on secondary, it means, languages driver sends read on secondary and write on the primary. In multi deployment cluster, you can configure read from the node which is sitting near to application server. The driver is smart enough which identify which MongoDB node running rear to the application server. There are different questions in your mind like, how to convert stand-alone MongoDB server to replica set? how to add a node in the replicas set cluster etc.

There are diffent cheat sheet of mongoDB on you can visit each one of them as follows:

2. How to graceful shutdown of mongoNode?

you can gracefully shutdown a MongoDB node for maintenance.

> use admin;
> db.shutdownServer();

3. how to add node under replica set

Following command is used to add a node in MongoDB replica set cluster. There are two part of argument, fist part will be IP/domain/host/machineName and second part will be port number in which MongoDB running. Here, I have provided IP and port with colon separated.

> rs.add("");

4. how to add Arbiter node under replica set?

Following command is used to add arbiter in MongoDB replica set cluster. there are two part of argument , fist part will be IP/domain/host/machineName and second part will be port number in which MongoDB running.

> rs.addArb("");

In argument , paass hostname, subdomain name, or ip of the server . Arbiter node will take participation in election to vote any one node to become primary. No data replication will be on Arbiter node.

how to remove node from replica set?

logged-in into mongodb primary node and run following.

> rs.remove("");

5. how to initialize mongodb nodes on replica set?

{ "_id" : "rs2",
    "version" : 1,
    "members" : [
        {"_id" : 0,"host" : "","priority" : 2},
        {"_id" : 1,"host" : "", "priority" : 1},
	{"_id" : 1,"host" : "", "priority" : 1}

here, priority is optional parameter , add this if you want to push value other than 1 , 1 is by default.
rs2 is the name of replicaset which need to set in client seed connection.

6. How to add hidden node in cluster?

do this off hour , not preferable in business hour

cfg.members[2].votes = 0
cfg.members[2].hidden = true;

if above not worked then do forecfully, by using folloing but don’t do this peak hour.


7. how to add with zero priority node

Following command is used to node in a MongoDB cluster with zero priority.

cfg.members[2]={ _id: 2, host : " 27017", priority:0 }

Here, _id should be incremental so , first check existing and then increment one for new node.

how to check long running task

> db.currentOp()

kill long running task

> db.killOp(1194053549);

Here, in argument pass process id of running job to kill.

Happy learning for MongoDB replica set admin cheat sheet.

Connect with

Leave a Comment

Your email address will not be published. Required fields are marked *