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

  • Docker Maven plugin: how to specify shm size when running a container
  • How to use STOPSIGNAL instruction within Docker?
  • Docker Data Volume for SBT Dependencies
  • Creating proper build or deployment chain with Microservices, Docker, Jenkins and GIT
  • docker: push image not work
  • docker blockio can not monitor when using dd to test
  • 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

  • Running GUI from a container on a mac resolve in “libGL error: No matching fbConfigs or visuals found”
  • When to use -type d-exec chmod 750 / 640 in docker
  • Dockerfile or Registry? Which is the preferred strategy for distribution?
  • Docker with GUI application
  • Vagrant and docker Protocol error mounting directory windows 7
  • Linking Nodejs app container to elastic search container securely (Docker)
  • 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.