How to setup a valid mongo replicaset

I would like to study a scenario that there are several MongoDB in replica mode but in a special case, there is only one working. My configuration is like following.

I have a MongoDB container on an EC2 instance, my command is

  • How to build Docker Image with Fake
  • Docker registry and index
  • Docker search command always lists empty result
  • docker toolbox windows 7: stops working when disconnecting from LAN
  • marking origin of logs in logstash
  • Docker - adding DNS by editing “docker” file
  • sudo docker run \
    --name mongo \
    -v /home/core/mongo-files/data:/data/db \
    -p 27018:27017 -d mongo:3.2.1 \
    --smallfiles \
    --replSet "rs0"
    

    then I have 2 applications developing by Nodejs that use this database. They connect with this connection string:

    uri: ‘mongodb://192.168.0.100:27018/testmongo?replicaSet=rs0′

    Unfortunately, one of my applications works well but other doesn’t. Error message when it tried to connect database is

    MongoDB connection error: MongoError: no valid replicaset members
    found

    I have check status by running this commands rs.slaveOk(), rs.status() then I have this

    rs0:PRIMARY> rs.status()
    {
        "set" : "rs0",
        "date" : ISODate("2016-09-19T11:50:59.947Z"),
        "myState" : 1,
        "term" : NumberLong(2),
        "heartbeatIntervalMillis" : NumberLong(2000),
        "members" : [
            {
                "_id" : 0,
                "name" : "02aaebd39d4b:27017",
                "health" : 1,
                "state" : 1,
                "stateStr" : "PRIMARY",
                "uptime" : 1194663,
                "optime" : {
                    "ts" : Timestamp(1474285564, 1),
                    "t" : NumberLong(2)
                },
                "optimeDate" : ISODate("2016-09-19T11:46:04Z"),
                "electionTime" : Timestamp(1473091196, 1),
                "electionDate" : ISODate("2016-09-05T15:59:56Z"),
                "configVersion" : 1,
                "self" : true
            }
        ],
        "ok" : 1
    }
    

    My question if this setup for my scenario is correct or not? If it is correct, could you please help me fix this. Thank you in advance

  • Docker Networking: Auto-discovering host names in a bridge network
  • Sonatype Nexus 3 - Scripting Docker Image Cleanup?
  • Docker compose error: compose.cli.main.main: Container command could not be invoked
  • Writing python log files to logstash
  • Sending email using smtplib in ubuntu docker throws “421 4.3.2 Connection rate limit exceeded”
  • How can I use Docker to run my all selenium test cases on all instance of my application using a single machine?
  • One Solution collect form web for “How to setup a valid mongo replicaset”

    Edited

    Maybe start again, there are 3 stages to starting the replica

    1) starting mongod instances as replicas

    mongod --port 27018 --replSet rs0 --dbpath /home/core/mongo-files/data:/data/db1 --logpath your/log/path --smallfiles --fork --logappend
    mongod --port 27017 --replSet rs0 --dbpath /home/core/mongo-files/data:/data/db2 --logpath your/log/path --smallfiles --fork --logappend
    

    2) Connect to mongodb and initiate the replica and define the mongods included as replicas

    rs.initiate(
       {
          _id: "rs0",
    members: [
             { _id: 0, host : "Your.Machine's.LocalHost:27017" },
             { _id: 1, host : "Your.Machine's.LocalHost:27018" }
          ]
       }
    )
    

    3) actually initiating the replica with the command below.

    rs.initiate(rs0)
    

    I hope this helps.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.