MongoDB Script with rs.add() Not Executing As Expected

For some reason this very basic script does not run as expected:

rs.initiate();
rs.add("ip-10-0-1-11");
rs.add("ip-10-0-1-12");

When I call it (after a fresh spin-up of mongod) like so:

  • Requests to docker containers failing on chrome for OSX
  • IBM Bluemix erroring when creating volumes via docker-compose up
  • Alpine Linux Docker Container with jre-8 does not have Nashorn Engine
  • Deploy my application to cluster of multiple vms using orchestration tools
  • How can I pass the argument to docker-compose.yml using shell file
  • docker run from systemd service starts but exits immediately, and restarts, but docker run from shell works fine
  • docker exec mongodb mongo localhost:27017/test /script/path.js
    

    I only see the addition of the second host.

    rs0:PRIMARY> rs.status()
    {
            "set" : "rs0",
            "date" : ISODate("2015-12-14T20:27:10.124Z"),
            "myState" : 1,
            "term" : NumberLong(1),
            "heartbeatIntervalMillis" : NumberLong(2000),
            "members" : [
                    {
                            "_id" : 0,
                            "name" : "ip-10-0-1-10:27017",
                            "health" : 1,
                            "state" : 1,
                            "stateStr" : "PRIMARY",
                            "uptime" : 45,
                            "optime" : {
                                    "ts" : Timestamp(1450124790, 1),
                                    "t" : NumberLong(1)
                            },
                            "optimeDate" : ISODate("2015-12-14T20:26:30Z"),
                            "infoMessage" : "could not find member to sync from",
                            "electionTime" : Timestamp(1450124789, 2),
                            "electionDate" : ISODate("2015-12-14T20:26:29Z"),
                            "configVersion" : 2,
                            "self" : true
                    },
                    {
                            "_id" : 1,
                            "name" : "ip-10-0-1-12:27017",
                            "health" : 1,
                            "state" : 2,
                            "stateStr" : "SECONDARY",
                            "uptime" : 40,
                            "optime" : {
                                    "ts" : Timestamp(1450124790, 1),
                                    "t" : NumberLong(1)
                            },
                            "optimeDate" : ISODate("2015-12-14T20:26:30Z"),
                            "lastHeartbeat" : ISODate("2015-12-14T20:27:09.828Z"),
                            "lastHeartbeatRecv" : ISODate("2015-12-14T20:27:06.827Z"),
                            "pingMs" : NumberLong(0),
                            "configVersion" : 2
                    }
            ],
            "ok" : 1
    }
    

    However, if I jump back into the cli and add the missing host manually – it works no problem. Also – if I just run the script again, the second one gets added as normal.

    Checking the logs doesn’t give me back any useful information. I just see that the ip-10-0-1-12 host was added, and no references to ip-10-0-1-11. I’m 100% certain the other mongod instance is reachable by the time I run these commands.

    I have tried this a few ways – even like so:

    docker exec mongodb mongo localhost:27017/test --eval 'rs.initiate();rs.add("ip-10-0-1-11");rs.add("ip-10-0-1-12");'
    

    As well as jumping into the cli and running load("path/to/script.js")

    Any help would be much appreciated.

    Edit:

    Here is a chunk of logs while the script ran. Everything above is initialization info, and everything below is just accepted connections:

    2015-12-14T20:26:29.759+0000 I COMMAND  [conn1] initiate : no configuration specified. Using a default configuration for the set
    2015-12-14T20:26:29.759+0000 I COMMAND  [conn1] created this configuration for initiation : { _id: "rs0", version: 1, members: [ { _id: 0, host: "ip-10-0-1-10:27017" } ] }
    2015-12-14T20:26:29.759+0000 I REPL     [conn1] replSetInitiate admin command received from client
    2015-12-14T20:26:29.759+0000 I REPL     [conn1] replSetInitiate config object with 1 members parses ok
    2015-12-14T20:26:29.759+0000 I REPL     [conn1] ******
    2015-12-14T20:26:29.759+0000 I REPL     [conn1] creating replication oplog of size: 990MB...
    2015-12-14T20:26:29.765+0000 I STORAGE  [conn1] Starting WiredTigerRecordStoreThread local.oplog.rs
    2015-12-14T20:26:29.765+0000 I STORAGE  [conn1] The size storer reports that the oplog contains 0 records totaling to 0 bytes
    2015-12-14T20:26:29.765+0000 I STORAGE  [conn1] Scanning the oplog to determine where to place markers for truncation
    2015-12-14T20:26:29.784+0000 I REPL     [conn1] ******
    2015-12-14T20:26:29.793+0000 I REPL     [ReplicationExecutor] New replica set config in use: { _id: "rs0", version: 1, protocolVersion: 1, members: [ { _id: 0, host: "ip-10-0-1-10:27017", arbiterOnly: false, buildIndexes: true, hidden: false, priority: 1.0, tags: {}, slaveDelay: 0, votes: 1 } ], settings: { chainingAllowed: true, heartbeatIntervalMillis: 2000, heartbeatTimeoutSecs: 10, electionTimeoutMillis: 10000, getLastErrorModes: {}, getLastErrorDefaults: { w: 1, wtimeout: 0 } } }
    2015-12-14T20:26:29.793+0000 I REPL     [ReplicationExecutor] This node is ip-10-0-1-10:27017 in the config
    2015-12-14T20:26:29.793+0000 I REPL     [ReplicationExecutor] transition to STARTUP2
    2015-12-14T20:26:29.793+0000 I REPL     [conn1] Starting replication applier threads
    2015-12-14T20:26:29.794+0000 I REPL     [ReplicationExecutor] transition to RECOVERING
    2015-12-14T20:26:29.795+0000 I REPL     [conn1] replSetReconfig admin command received from client
    2015-12-14T20:26:29.795+0000 I REPL     [ReplicationExecutor] transition to SECONDARY
    2015-12-14T20:26:29.795+0000 I REPL     [ReplicationExecutor] conducting a dry run election to see if we could be elected
    2015-12-14T20:26:29.795+0000 I REPL     [ReplicationExecutor] dry election run succeeded, running for election
    2015-12-14T20:26:29.803+0000 I REPL     [ReplicationExecutor] election succeeded, assuming primary role in term 1
    2015-12-14T20:26:29.803+0000 I REPL     [ReplicationExecutor] transition to PRIMARY
    2015-12-14T20:26:29.804+0000 I REPL     [conn1] replSetReconfig admin command received from client
    2015-12-14T20:26:29.806+0000 I REPL     [conn1] replSetReconfig config object with 2 members parses ok
    2015-12-14T20:26:29.807+0000 I ASIO     [NetworkInterfaceASIO-Replication-0] Successfully connected to ip-10-0-1-12:27017
    2015-12-14T20:26:29.808+0000 I REPL     [ReplicationExecutor] New replica set config in use: { _id: "rs0", version: 2, protocolVersion: 1, members: [ { _id: 0, host: "ip-10-0-1-10:27017", arbiterOnly: false, buildIndexes: true, hidden: false, priority: 1.0, tags: {}, slaveDelay: 0, votes: 1 }, { _id: 1, host: "ip-10-0-1-12:27017", arbiterOnly: false, buildIndexes: true, hidden: false, priority: 1.0, tags: {}, slaveDelay: 0, votes: 1 } ], settings: { chainingAllowed: true, heartbeatIntervalMillis: 2000, heartbeatTimeoutSecs: 10, electionTimeoutMillis: 10000, getLastErrorModes: {}, getLastErrorDefaults: { w: 1, wtimeout: 0 } } }
    

  • docker info command doesn't show anything in ec2 Instance
  • how to change MTU inside docker container on ubuntu 14.4
  • Private Github repositories in dockerized rails application during build
  • Scaling Docker containers in Rancher with different but persistent volumes
  • No goals have been specified for this build.
  • Docker container name conflict conflict, but that container doesn't exist
  • One Solution collect form web for “MongoDB Script with rs.add() Not Executing As Expected”

    rs.initiate() requires a few seconds for the replica to change status. This might cause the first rs.add() not working properly. It’s strange that we did not get an error message for this.

    To avoid this problem happens, you can try to initiate and configure all the members in replica set in a single command:

    rs.initiate(
      {_id:'rs0',
       members:[ { _id:1, host:'ip-10-0-1-10:27017'},
                 { _id:2, host:'ip-10-0-1-11:27017'},
                 { _id:3, host:'ip-10-0-1-12:27017'}]
       });
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.