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

All we need is an easy explanation of the problem, so here it is.

while adding secondary member in replica set, i get following error:
"Either all host names in a replica set configuration must be localhost references, or none must be; found 1 out of 2"
my first machine configuration is:mongo db-Either all host names in a replica set configuration must be localhost references, or none must be; found 1 out of 2

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

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

second node config:
mongo db-Either all host names in a replica set configuration must be localhost references, or none must be; found 1 out of 2

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

You can have all replica set nodes in the same physical computer (localhost), but in different ports! (node1:27017, node2:27027, …)

You CANNNOT have different RS nodes:

  • at one machine at same port (f.ex. 27017)
  • different machine and bind to localhost address

So, you need to build your replica set using IP-addresses.

Method 2

I have remowed 127.0.0.1 from configuration on both sides and instead i have just used ip’s and the problem has solved.
1 node configuration: cat/etc/mongod.conf

net:
port: 27017
bindIp: 10.10.24.17
cat/etc/hosts
##127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
#::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.10.24.17 mongo01.barva.info mongo01
10.10.24.18 mongo02.barva.info mongo02

2 node config:
cat/etc/mongod.conf

net:
port: 27017
bindIp: 10.10.24.18
cat/etc/hosts
##127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
#::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.10.24.17 mongo01.barva.info mongo01
10.10.24.18 mongo02.barva.info mongo02.
when connecting to mongo shell, I am using $mongo 10.10.24.17 , rs.add("mongo02") command works . by the way the version of mongo is 4.4.4. hope this will help others too.
@JJussi thanks for yuor answers!

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply