Neeraj Jain Neeraj Jain - 2 months ago 16
Java Question

Error while creating Replica set - MongoDb

I am trying to create replica but unable to proceed

Script to create 3 mongod instance :

sudo mkdir -p /data/rs1 /data/rs2 /data/rs3

sudo mongod --replSet rs1 --logpath "1.log" --dbpath /data/rs1 --port 27017 --fork
sudo mongod --replSet rs2 --logpath "2.log" --dbpath /data/rs2 --port 27018 --fork
sudo mongod --replSet rs3 --logpath "3.log" --dbpath /data/rs3 --port 27019 --fork


This executes successfully but after this i try to provide rs1 information about rs2 and rs3 via below script :

init_replica.js :

config = {
_id:"rs1",members:[
{_id:0,host:"grit-lenevo-pc:27017",priority:0,slaveDelay:5},
{_id:1,host:"grit-lenevo-pc:27018"},
{_id:2,host:"grit-lenevo-pc:27019"}]
}

rs.initiate(config)
rs.status()


Now when i try to run :

mongo --port 27018 < init_replica.js


I am getting :

MongoDB shell version: 3.2.8
connecting to: 127.0.0.1:27018/test
{
"_id" : "rs1",
"members" : [
{
"_id" : 0,
"host" : "grit-lenevo-pc:27017",
"priority" : 0,
"slaveDelay" : 5
},
{
"_id" : 1,
"host" : "grit-lenevo-pc:27018"
},
{
"_id" : 2,
"host" : "grit-lenevo-pc:27019"
}
]
}
{
"ok" : 0,
"errmsg" : "Attempting to initiate a replica set with name rs1, but command line reports rs2; rejecting",
"code" : 93
}
{
"info" : "run rs.initiate(...) if not yet done for the set",
"ok" : 0,
"errmsg" : "no replset config has been received",
"code" : 94
}
bye


Note : The same command works fine if i try below command :

mongo --port 27017 < init_replica.js


Following tutorials : M101 Mongo Db For Java Developers

Answer

It's right about there:

"Attempting to initiate a replica set with name rs1, but command line reports rs2; rejecting"

You should supply all members with the same replica set name as the seed (s1). For the second member:

sudo mongod --replSet rs1 ...

and not

sudo mongod --replSet rs2 ...

Same principal goes for third member