13

I am trying to deploy mongodb replica set . I have made my mongodb instance as primary of the replica . Now i am trying to add another mongodb instance hosted at different iP address, as secondary of the replica, but i am getting the following error :

    rs0:PRIMARY> rs.add("<ip address>:27017")
{
    "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
}

What I am doing wrong ?

Anuj Jain
  • 245
  • 1
  • 2
  • 5

3 Answers3

8

I face this issue when i tried to run two mongod instance in same machine. It throws error when i provide like

   rs.add("localhost:27027")
   (or)
   rs.add("127.0.0.1:27027")

where 27027 is the port number of secondary.

Solution:

Pass the hostname instead of ip address

  rs.add("myhostname:27027")
Fizer Khan
  • 88,237
  • 28
  • 143
  • 153
  • For me rs.add("127.0.0.1:27027") worked. Both other options rs.add("localhost:27027") and rs.add("hostname:27017") threw and error – nithin Sep 08 '20 at 20:09
  • I tried using the IP address of the machine in which all of the replica sets were hosted, 172.16.10.160, rs.add("172.16.10.160:27018") that worked after failing with hostname, but mainly because I set the listen address to 0.0.0.0 – Shahnewaz Ul Islam Chowdhury Apr 24 '21 at 16:52
1

Does the IP address of your newly added replica set member resolve to localhost ? Or, does your already existing member resolve to localhost ? Either way, a replica set will not allow mixed localhost / non-localhost configuration.

Take a look at the source code location of this message.

Also, the Mongo guide on sharding says this:

If you use either “localhost” or 127.0.0.1 as the hostname portion of any host identifier, for example as the host argument to addShard or the value to the --configdb run time option, then you must use “localhost” or 127.0.0.1 for all host settings for any MongoDB instances in the cluster. If you mix localhost addresses and remote host address, MongoDB will error.

The same applies for replica sets.

Kai Sternad
  • 22,214
  • 7
  • 47
  • 42
  • Any idea on how to deal with this when trying to add a member via a ssh tunnel? – Daniel F May 17 '17 at 20:05
  • Sorry, I can't say off the bat. But in theory the tunnel should be transparent and therefore not make a difference. But you have to try it out. – Kai Sternad May 18 '17 at 09:55
  • The problem is that the tunnel is then accessible as "localhost:", but I need to have the primary mongodb bound to 127.0.0.1 and 172.17.0.1, because containers in Docker need to access it. It is then this 172.17.0.1 which causes the trouble. Here is a better explanation https://dba.stackexchange.com/questions/173911/mongodb-replica-set-over-ssh-tunnels – Daniel F May 18 '17 at 09:57
0

To reconfigure your 1-node replica set's primary from localhost / 127.0.0.1 to external hostname / ip you can do the following:

cfg = rs.conf()
cfg.members[0].host = "mongo0.example.net:27017"
rs.reconfig(cfg)

First line gets current replica set configuration, second line modifies the host field of member 0, and the last one applies the new configuration.

See more details here: https://docs.mongodb.com/manual/tutorial/change-hostnames-in-a-replica-set/