MongoDB tip: 4 ways to modify replica set configuration

This is just a quick guide on how to modify your replica set settings in 4 different ways.

This guide is assuming you have a functional replica set ready, it doesn't matter if it's a single node or multi-node replica set.

In each of the four method, I am looking to modify the priority of the _id=0 node


Method 1: Via Mongo Shell

Login to your Mongo shell, 
simply type in mongo, assuming default port is being used and authentication is not enabled

[in Mongo shell]
cfg = rs.conf()
cfg.members[0].priority=99
rs.reconfig(cfg)

# "ok":1 indicates the new configuration has been set 
MongoDB Enterprise minvera:PRIMARY> rs.reconfig(cfg)
{
        "ok" : 1,
        "operationTime" : Timestamp(1528464949, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1528464949, 1),
                "signature" : {
                        "hash" : BinData(0,"W/epTjISxIG99zs/7uGg4I8XA0Q="),
                        "keyId" : NumberLong("6563655092891811841")
                }
        }
}


Method 2: via Bash shell

# taking output of rs.conf() and save in a variable
conf=$(mongo --quiet --eval "printjson(rs.conf())")
# modifying the first occurrence of priority to 99 via regular expression 
conf=$(echo $conf | sed 's/\(\"priority\"\ : \)[^,]*/\199/')
# pass in the variable into mongo shell via "--eval "
mongo --eval "printjson(rs.reconfig(${conf}))"


Method 3: via Mongo --eval 

# pass in the new configuration via --eval directly
mongo --eval "cfg=rs.conf(); cfg.members[0].priority=99; rs.reconfig(cfg);"


Method 4: via javascript file

# create javascript file
echo "var conf=rs.conf();
conf.members[0].priority=999;
rs.reconfig(conf);" > load.js
# pass in the javascript as argument through mongo shell
mongo load.js

Comments

Popular posts from this blog

MongoDB Quick Note: BI Connector Issue

MongoDB Tips: Kill long running processes