mongoDB – replica set admin cheat sheet

Connect with

MongoDB
In replicaset there are various types of command which you have to run for maintenance and operation point of view.

Well, first of all, let us understand what is replicaset cluster, in mongoDB. There is concept of primary-secondary or master-slave, primary/master can accept write and read but ideally you sould configured readPrefered on secondary, it means ,langurages driver sends read on secondary and write on primary. In multi deployment cluster, you can confugured read from node which is sitting near to application server. Driver is smart enouhg which identify which mongoDB node running rear to application server. There are different question in your mind like , how to convert stand alone mongdb server to replicaset? how to add node in the replicas set cluster etc.

There are diffent cheat sheet of mongoDB on mysoftkey.com as follows:

How to graceful shutdown of mongoNode?

you can put the replicaset node under maintenance by following ways.

mongo
> use admin;
> db.shutdownServer();

how to add node under replica set

> rs.add("10.34.83.23:27017");

how to add Arbiter node under replica set?

> rs.addArb("10.34.83.2:27017");

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("10.34.83.10:27017");

how to initialize mongodb nodes on replica set?

{ "_id" : "rs2",
    "version" : 1,
    "members" : [
        {"_id" : 0,"host" : "10.72.179.1:27017","priority" : 2},
        {"_id" : 1,"host" : "10.72.179.2:27017", "priority" : 1},
	{"_id" : 1,"host" : "10.72.179.3:27017", "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.

How to add hidden node in cluster?

do this off hour , not preferable in business hour

cfg=rs.conf()
cfg.members[2].votes = 0
cfg.members[2].hidden = true;
rs.reconfig(cfg)

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


rs.reconfig(cfg,{force:true})

how to add with zero priority node

cfg=rs.conf()
cfg.members[2]={ _id: 2, host : "10.80.30.1: 27017", priority:0 }
rs.reconfig(cfg,{force:true})

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.


Connect with

Leave a Reply

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