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?
Maybe start again, there are 3 stages to starting the replica
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
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" }
]
}
)
actually initiating the replica with the command below.
rs.initiate(rs0)