Help setting up replication between Windows servers

Hi Team,

We are trying to replicate the MongoDB from one windows server to another windows server. Basically for any fail over / High availability.

We followed steps from link - How to install MongoDB 3.4 in replication on Windows Server 2016 but somehow we are unable to see DB’s from Secondary Machine which was replication from Primary. Please help us on this.

Hello @NithiN_Katta, welcome to the MongoDB Community forum.

In general, you should be following the tutorial to deploy the replica-set (or any of the other available tutorials for replica-sets) to deploy one.

Please verify the steps from the tutorial and see if you had followed all of them. Also, tell about the hardware and the MongoDB versions you are using.

1 Like

It would be useful if you could share the output of

rs.status()
r2schools:PRIMARY> rs.status()
{
        "set" : "r2schools",
        "date" : ISODate("2021-02-02T05:53:25.883Z"),
        "myState" : 1,
        "term" : NumberLong(5),
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 1,
        "writeMajorityCount" : 1,
        "votingMembersCount" : 1,
        "writableVotingMembersCount" : 1,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1612245201, 1),
                        "t" : NumberLong(5)
                },
                "lastCommittedWallTime" : ISODate("2021-02-02T05:53:21.067Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1612245201, 1),
                        "t" : NumberLong(5)
                },
                "readConcernMajorityWallTime" : ISODate("2021-02-02T05:53:21.067Z"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1612245201, 1),
                        "t" : NumberLong(5)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1612245201, 1),
                        "t" : NumberLong(5)
                },
                "lastAppliedWallTime" : ISODate("2021-02-02T05:53:21.067Z"),
                "lastDurableWallTime" : ISODate("2021-02-02T05:53:21.067Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1612245171, 1),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "electionTimeout",
                "lastElectionDate" : ISODate("2021-02-02T05:35:50.893Z"),
                "electionTerm" : NumberLong(5),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1611897984, 1),
                        "t" : NumberLong(4)
                },
                "numVotesNeeded" : 1,
                "priorityAtElection" : 1,
                "electionTimeoutMillis" : NumberLong(10000),
                "newTermStartDate" : ISODate("2021-02-02T05:35:50.897Z"),
                "wMajorityWriteAvailabilityDate" : ISODate("2021-02-02T05:35:50.918Z")
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "localhost:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 1056,
                        "optime" : {
                                "ts" : Timestamp(1612245201, 1),
                                "t" : NumberLong(5)
                        },
                        "optimeDate" : ISODate("2021-02-02T05:53:21Z"),
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1612244150, 1),
                        "electionDate" : ISODate("2021-02-02T05:35:50Z"),
                        "configVersion" : 1,
                        "configTerm" : 5,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1612245201, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1612245201, 1)
}
r2schools:PRIMARY>

Hi @NithiN_Katta, the status shows that there is only one member in the replica-set and it is a Primary node.

1 Like

Exactly, even after giving multiple servers in rs.add we see below error msg. Not sure what is going wrong here, please suggest.

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

If there is any document for replica/fail over between server to server please provide me the link, tried using all links from google, still its same

In the links provided in my earlier post “other available tutorials for replica-sets” there is this topic called as “Add Members to a Replica Set”. Since, you already have a replica-set with one member (your rs.status() shows that), you can add a second member (or more as needed) to the replica-set.

As your replicaset was initialised with localhost as the member you will have to reconfigure the replicaset.
You need to use the hostname for this, one that other members will use to connect on.

This is the state you are at, localhost member and unable to add a new member:

replicaset-locahost
 rs0:PRIMARY> rs.config()
{
	"_id" : "rs0",
	"version" : 2,
	"term" : 1,
	"protocolVersion" : NumberLong(1),
	"writeConcernMajorityJournalDefault" : true,
	"members" : [
		{
			"_id" : 0,
			"host" : "localhost:27017",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		}
	],
	"settings" : {
		"chainingAllowed" : true,
		"heartbeatIntervalMillis" : 2000,
		"heartbeatTimeoutSecs" : 10,
		"electionTimeoutMillis" : 10000,
		"catchUpTimeoutMillis" : -1,
		"catchUpTakeoverDelayMillis" : 30000,
		"getLastErrorModes" : {
			
		},
		"getLastErrorDefaults" : {
			"w" : 1,
			"wtimeout" : 0
		},
		"replicaSetId" : ObjectId("601bf79d6dc7b0a48a730b18")
	}
}
rs0:PRIMARY> rs.add('mongo-0-b')
{
	"operationTime" : Timestamp(1612445708, 1),
	"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",
	"$clusterTime" : {
		"clusterTime" : Timestamp(1612445708, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}

Now, to reconfigure to use hostname instead of localhost:

reconfigure
rs0:PRIMARY> var c = rs.conf()
rs0:PRIMARY> c.members[0].host='mongo-0-a:27017'
mongo-0-a:27017
rs0:PRIMARY> rs.reconfig(c)
{
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1612445905, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1612445905, 1)
}

After the reconfigue, adding a new member works:

add member
rs0:PRIMARY> rs.add('mongo-0-b')
{
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1612445940, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1612445940, 1)
}

PLease find the output:

C:\Program Files\MongoDB\Server\4.4\bin>mongo
MongoDB shell version v4.4.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("5c18cf32-dc14-4acc-b17e-cecdc904b9b1") }
MongoDB server version: 4.4.3
---
The server generated these startup warnings when booting:
        2021-02-03T21:39:24.567-06:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2021-02-03T21:39:24.568-06:00: This server is bound to localhost. Remote systems will be unable to connect to this server. Start the server with --bind_ip <address> to specify which IP addresses it should serve responses from, or with --bind_ip_all to bind to all interfaces. If this behavior is desired, start the server with --bind_ip 127.0.0.1 to disable this warning
---
---
        Enable MongoDB's free cloud-based monitoring service, which will then receive and display
        metrics about your deployment (disk utilization, CPU, operation statistics, etc).

        The monitoring data will be available on a MongoDB website with a unique URL accessible to you
        and anyone you share the URL with. MongoDB may use this information to make product
        improvements and to suggest MongoDB products and deployment options to you.

        To enable free monitoring, run the following command: db.enableFreeMonitoring()
        To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
r2schools:PRIMARY>  rsconf={_id:"cbre",members:[{_id:0,host:"localhost:27017",_id:1,host:"10.71.101.187:27017",_id:2,host:"10.71.136.191:27017"}]}
{
        "_id" : "cbre",
        "members" : [
                {
                        "_id" : 2,
                        "host" : "10.71.136.191:27017"
                }
        ]
}
r2schools:PRIMARY> rs.initiate(rsconf)
{
        "operationTime" : Timestamp(1612763918, 1),
        "ok" : 0,
        "errmsg" : "already initialized",
        "code" : 23,
        "codeName" : "AlreadyInitialized",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1612763918, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}
r2schools:PRIMARY> rs.add("10.71.101.186:27017")
{
        "operationTime" : Timestamp(1612763928, 1),
        "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",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1612763928, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}
r2schools:PRIMARY> rs.config()
{
        "_id" : "r2schools",
        "version" : 1,
        "term" : 7,
        "protocolVersion" : NumberLong(1),
        "writeConcernMajorityJournalDefault" : true,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "localhost:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "catchUpTimeoutMillis" : -1,
                "catchUpTakeoverDelayMillis" : 30000,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("60128e486bd6d3eea3782ebd")
        }
}
r2schools:PRIMARY> rs.add("10.71.101.187:27017")
{
        "operationTime" : Timestamp(1612763958, 1),
        "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",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1612763958, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}

You cannot mix localhost with non-localhost. Use the lan ip or hostname of the host instead of localhost.

1 Like

In addition, your syntax is wrong. You are missing braces between members.

[ Edited to illustrate my comment above posted from my tablet ]

As you have seen:

mongo > rsconf={_id:"cbre",members:[{_id:0,host:"localhost:27017",_id:1,host:"10.71.101.187:27017",_id:2,host:"10.71.136.191:27017"}]}

gives the following value as you posted

mongo > rsconf
{
	"_id" : "cbre",
	"members" : [
		{
			"_id" : 2,
			"host" : "10.71.136.191:27017"
		}
	]
}

So you only have 1 object in the array members. And since you cannot have 2 fields with the same key in a single document you end up with a document that contains the value of the last occurrence of a key. What you really wanted, (with the correct bracing) is:

mongo > rsconf={_id:"cbre",members:[{_id:0,host:"localhost:27017"},{_id:1,host:"10.71.101.187:27017"},{_id:2,host:"10.71.136.191:27017"}]}
{
	"_id" : "cbre",
	"members" : [
		{
			"_id" : 0,
			"host" : "localhost:27017"
		},
		{
			"_id" : 1,
			"host" : "10.71.101.187:27017"
		},
		{
			"_id" : 2,
			"host" : "10.71.136.191:27017"
		}
	]
}

But the above will still be wrong despite having the correct syntax because

2 Likes

Here is a post with an answer for the issue being discussed, and it says:

As per the error message says, you have added the Primary node of the replica set as localhost . The rule is all nodes of the replica set should be localhost or all should be hostname/ip address…

Even after provide IP/HOST i still see same issue,somehow still it reads localhost. In windows HOST file also i have set to IP address.

r2schools:PRIMARY>  rsconf={_id:"r2schools",members:[{_id:0,host:"10.71.101.186:27017",_id:1,host:"10.71.101.187:27017",_id:2,host:"10.71.136.191:27017"}]}
{
        "_id" : "r2schools",
        "members" : [
                {
                        "_id" : 2,
                        "host" : "10.71.136.191:27017"
                }
        ]
}
r2schools:PRIMARY> rs.initiate(rsconf)
{
        "operationTime" : Timestamp(1612837719, 1),
        "ok" : 0,
        "errmsg" : "already initialized",
        "code" : 23,
        "codeName" : "AlreadyInitialized",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1612837719, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}
r2schools:PRIMARY> rs.add("10.71.101.186:27017")
{
        "operationTime" : Timestamp(1612837769, 1),
        "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",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1612837769, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}
r2schools:PRIMARY> rs.config()
{
        "_id" : "r2schools",
        "version" : 1,
        "term" : 7,
        "protocolVersion" : NumberLong(1),
        "writeConcernMajorityJournalDefault" : true,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "localhost:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "catchUpTimeoutMillis" : -1,
                "catchUpTakeoverDelayMillis" : 30000,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("60128e486bd6d3eea3782ebd")
        }
}
r2schools:PRIMARY> rs.add("10.71.101.187:27017")
{
        "operationTime" : Timestamp(1612837789, 1),
        "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",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1612837789, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}
r2schools:PRIMARY> rs.add("10.71.136.191:27017")
{
        "operationTime" : Timestamp(1612837789, 1),
        "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",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1612837789, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}
r2schools:PRIMARY> rs.status()
{
        "set" : "r2schools",
        "date" : ISODate("2021-02-09T02:30:04.835Z"),
        "myState" : 1,
        "term" : NumberLong(7),
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 1,
        "writeMajorityCount" : 1,
        "votingMembersCount" : 1,
        "writableVotingMembersCount" : 1,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1612837799, 1),
                        "t" : NumberLong(7)
                },
                "lastCommittedWallTime" : ISODate("2021-02-09T02:29:59.837Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1612837799, 1),
                        "t" : NumberLong(7)
                },
                "readConcernMajorityWallTime" : ISODate("2021-02-09T02:29:59.837Z"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1612837799, 1),
                        "t" : NumberLong(7)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1612837799, 1),
                        "t" : NumberLong(7)
                },
                "lastAppliedWallTime" : ISODate("2021-02-09T02:29:59.837Z"),
                "lastDurableWallTime" : ISODate("2021-02-09T02:29:59.837Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1612837759, 1),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "electionTimeout",
                "lastElectionDate" : ISODate("2021-02-04T03:39:24.812Z"),
                "electionTerm" : NumberLong(7),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1612250005, 1),
                        "t" : NumberLong(6)
                },
                "numVotesNeeded" : 1,
                "priorityAtElection" : 1,
                "electionTimeoutMillis" : NumberLong(10000),
                "newTermStartDate" : ISODate("2021-02-04T03:39:24.818Z"),
                "wMajorityWriteAvailabilityDate" : ISODate("2021-02-04T03:39:24.868Z")
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "localhost:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 427841,
                        "optime" : {
                                "ts" : Timestamp(1612837799, 1),
                                "t" : NumberLong(7)
                        },
                        "optimeDate" : ISODate("2021-02-09T02:29:59Z"),
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1612409964, 1),
                        "electionDate" : ISODate("2021-02-04T03:39:24Z"),
                        "configVersion" : 1,
                        "configTerm" : 7,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1612837799, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1612837799, 1)
}

The changes you made did not get applied
Your rs.status still shows localhost
Unless you run reconfig changes will not happen
Please check the note from Prasad_Saya

@NithiN_Katta

I posted 5 days ago stating what your issue is and how to resolve it by reconfiguring your replicaset.
This is the easiest non-impacting way to resolve your issue.

Another option is to wipe your data and initiate the replicaset correctly.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.