Mongodb rs.add() new member in STARTUP state

mongodbreplicationsharding

I have been running a shell script that starts mongod instances on different machines, initializing them (the first machines) and adding them to a replica set (the others). The following is a summary of what I ran:

Machine 1 – mongod and initiate as primary by running rs.initiate()

Machine 2 – mongod and contact primary to run rs.add()

However, when I add the second mongod instance, the response was ok, but running rs.status() on primary it shows Machine 2 in STARTUP state

{
"_id" : 1,
"name" : "Machine 2",
"health" : 1,
"state" : 0,
"stateStr" : "STARTUP",
"uptime" : 30,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2018-06-14T07:07:11.336Z"),
"lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
"pingMs" : NumberLong(0),
"configVersion" : -2

Further investigation using the db.runCommand({getLog:"rs"} reveals the following:

"2018-06-14T12:23:54.223+0800 I REPL [replexec-5] Member Machine 2 is now in state RS_DOWN"

Why is my machine on STARTUP when I first add it? What is this RS_DOWN? I'm using MONGO 3.64 for both machines. I always delete local db before trying out the scripts

Best Answer

I figured out what is wrong with my replica set clusters. You need to provide an argument to rs.initiate(), which is the configuration document. If you don't specify the document, it will read the configuration from the /etc/mongod.conf which will has no configuration whatsoever for the replica set. The configuration for rs.initiate() looks something like this:

rs.initiate( { _id: "myReplSet", version: 1, members: [ { _id: 0, host : "mongodb0.example.net:27017" }, { _id: 1, host : "mongodb1.example.net:27017" }, { _id: 2, host : "mongodb2.example.net:27017" } ] } )

But if you do not know before hand how many replica sets you want to add to the cluster it is ok, just specify the first one and the subsequent ones will be added without any problems:

rs.initiate( { _id: "myReplSet", version: 1, members: [ { _id: 0, host : "mongodb0.example.net:27017" } ] } )