venkatesh
venkatesh

Reputation: 141

mongodb localhost replicasetup

I want to setup a replicasetup in my local machine,I am using to instances of mongodb(mongod1.conf,mongod2.conf), rs initiated mongo1 on port 27018 and i want to add the members to 27018 , rs.add('ThinkPad-X230:27019') it is throwing an error called

commands :

mongod --replSet Replicaset1 --dbpath home/data --port 27018
mongo --port 27018
>> rs.initiate()
>> rs.add("ThinkPad-X230:27019")

mongod --dbpath home/data2 --port 27019

mongo --port 27019

i have checked the db.serverStatus().host in 27019 port and adding host name "ThinkPad-X230:27019" to rs.add() members it is throwing the error.

{
    "ok" : 0,
    "errmsg" : "Either all host names in a replica set configuration must be localhost references, or none must be; found 1 out of 2",
    "code" : 103,
    "codeName" : "NewReplicaSetConfigurationIncompatible",
    "operationTime" : Timestamp(1568943205, 1),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1568943205, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}

Upvotes: 1

Views: 725

Answers (1)

Shubham Vaishnav
Shubham Vaishnav

Reputation: 1720

As you started your first instance of mongod with --replSet Replicaset1 option, it is configured to be a part of Replicaset1 replica set.

And when you initialised the replica set, this instance was added to the replica set as a member. Below is the output snippet of rs.status()

{
    "_id" : 0,
    "name" : "localhost:27018",
    "health" : 1,
    "state" : 1,
    "stateStr" : "PRIMARY",
    "uptime" : 228,
    "optime" : {
        "ts" : Timestamp(1569751005, 1),
        "t" : NumberLong(1)
    },
    "optimeDate" : ISODate("2019-09-29T09:56:45Z"),
    "electionTime" : Timestamp(1569750830, 2),
    "electionDate" : ISODate("2019-09-29T09:53:50Z"),
    "configVersion" : 3,
    "self" : true
}

As you can see the name of the member is "localhost:2018".

So, when you try to add another member to this replica set as rs.add('ThinkPad-X230:27019'), it gives you the following error which is a valid error to be thrown, as one member is "localhost:2018" and another which you are trying to add is "ThinkPad-X230:27019" and both must be localhost.

"errmsg" : "Either all host names in a replica set configuration must be localhost references, or none must be; found 1 out of 2"

Try to add the member using the following command,

rs.add("localhost:27019")

And it will be added successfully.

Upvotes: 1

Related Questions