Adding a node to the replica set
Assuming you have the node from the previous recipe already running, open a new Terminal.
Start a new replica set node
mongod --dbpath /data/server2/db --replSet MyReplicaSet --port 27018
Connect to the primary server using mongo shell
mongo mongodb://localhost:27017
MyReplicaSet:PRIMARY> rs.status()['members']
[
{
"_id" : 0,
"name" : "localhost:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 890,
"optime" : {
"ts" : Timestamp(1527846941, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-06-01T09:55:41Z"),
"electionTime" : Timestamp(1527846189, 2),
"electionDate" : ISODate("2018-06-01T09:43:09Z"),
"configVersion" : 1,
"self" : true
}
]
Add the new node to the replica set
MyReplicaSet:PRIMARY> rs.add('localhost:27018')
{
"ok" : 1,
"operationTime" : Timestamp(1527846991, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1527846991, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
MyReplicaSet:PRIMARY> rs.status()['members']
[
{
"_id" : 0,
"name" : "localhost:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 960,
"optime" : {
"ts" : Timestamp(1527847011, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-06-01T09:56:51Z"),
"electionTime" : Timestamp(1527846189, 2),
"electionDate" : ISODate("2018-06-01T09:43:09Z"),
"configVersion" : 2,
"self" : true
},
{
"_id" : 1,
"name" : "localhost:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 30,
"optime" : {
"ts" : Timestamp(1527847011, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1527847011, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-06-01T09:56:51Z"),
"optimeDurableDate" : ISODate("2018-06-01T09:56:51Z"),
"lastHeartbeat" : ISODate("2018-06-01T09:57:01.043Z"),
"lastHeartbeatRecv" : ISODate("2018-06-01T09:57:00.150Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "localhost:27017",
"configVersion" : 2
}
]