2

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

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
}

Is this setup for my scenario correct or not? If it is not correct, how to fix it?

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Bryan
  • 1,477
  • 1
  • 21
  • 38
  • The first thing I would check is that the one member running is in the applications seed list. If the other members that are currently down / removed from the set are the only ones in the seed list then your application won't be able to find the set. – James Wahlin Sep 19 '16 at 12:47
  • so my setup is not correct? should I need to create more mongo containers (say 3 instances) then I shut down and keep one? – Bryan Sep 19 '16 at 13:13
  • 1
    Can you post your application connection strings? We would need to see those to understand whether there is a seedlist issue. – James Wahlin Sep 19 '16 at 16:31
  • this is mine: `uri: 'mongodb://192.168.0.100:27018/testmongo?replicaSet=rs0',` – Bryan Sep 20 '16 at 02:52
  • If the mongod running on port 27017 is the only one active, you need to include as part of your connection string. Otherwise your application has no way of discovering replica set membership. I suggest adding all members to your connection uri. – James Wahlin Sep 20 '16 at 12:48
  • you can set many nodes in your docker-compose https://smaillns.medium.com/set-up-a-mongodb-replicaset-using-docker-799029493edf – Smaillns May 09 '22 at 06:51

1 Answers1

0

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)
    
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Nick Ko
  • 435
  • 4
  • 16