The replica set member host changed for itself from server IP address to hostname


I set up Mongo 4.0.2 for linux server. The server hostname is

/etc/hosts content:   localhost localhost.localdomain localhost4 localhost4.localdomain4
  ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6 dbaMongoServer

As you can see the hostname established in /etc/hosts file is not the same for server hostname

Then I changed the mongod.conf to establish it as replicaset and I executed the following commands in mongoshell:


One test application that connects to MongoDB and it started to faill. This application connects by IP address, not by hostname database server. These are the following messages from application log:

  2022-11-28 08:54:53.848 INFO  14720 --- [XNIO-1 task-2] org.mongodb.driver.connection Closed connection [connectionId{localValue:3695}] to because there was a socket exception raised by this connection.
  2022-11-28 08:54:53.850 ERROR 14720 --- [XNIO-1 task-2]     org.springframework.dao.DataAccessResourceFailureException:; nested exception is com.mongodb.MongoSocketException:
Caused by: com.mongodb.MongoSocketException:
      at com.mongodb.ServerAddress.getSocketAddress(

I fixed the /etc/hosts changing its third line as follows:

The application keeps failling.

After that, I checked the replicaset configuration by db.conf(), I realized the member host was changed for itself from IP address to hostname.

   "members" : [
                    "_id" : 0,
                    "host" : "",
                    "arbiterOnly" : false,
                    "buildIndexes" : true,
                    "hidden" : false,
                    "priority" : 1,
                    "tags" : {

                    "slaveDelay" : NumberLong(0),
                    "votes" : 1

The question is why the replica set member host changed for itself from server IP address to hostname?

Did your rs.add succeed?
On which port your mongod was running?You are trying to add second node on 27017
Show us the output of rs.status()
The output you have shown must be that of primary
By default rs.initiate takes hostname with empty config or if the members were not defined at initiate time

The rs.add succeded but not how I wanted. I executed rs.add(“”) after
rs.initialize(). Even though, the host member was stored as ‘dbaMongoServer:27017’. It never happenned to me before.

The port is 27017. It’s a replicaset for only one node (primary).

The output you have shown must be that of primary

rs.initiate() will have set the host up using its own fqdn. So it would not have changed it would have been from the initialization. The subsequent rs.add() would likely have failed.

Click to expand: example: initiate, add
root@mdb:/# hostname -I 
root@mdb:/# mongo --quiet
> rs.initiate()
	"info2" : "no configuration specified. Using a default configuration for the set",
	"me" : "mdb:27017",
	"ok" : 1
s0:PRIMARY> rs.add('')
	"operationTime" : Timestamp(1669848239, 1),
	"ok" : 0,
	"errmsg" : "The hosts mdb:27017 and all map to this node in new configuration version 2 for replica set s0",
	"code" : 103,
	"codeName" : "NewReplicaSetConfigurationIncompatible",
	"$clusterTime" : {
		"clusterTime" : Timestamp(1669848239, 1),
		"signature" : {
			"keyId" : NumberLong(0)

Using fqdn’s is the recommended way to reference replicaset members.

Regardless, there are a few ways to change to change this to IP, I’ll add one here:

//matching my example from above
s0:PRIMARY> var c=rs.conf()
s0:PRIMARY> c.members[0].host=''
s0:PRIMARY> rs.reconfig(c)
	"ok" : 1,
	"operationTime" : Timestamp(1669850026, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1669850026, 1),
		"signature" : {
			"keyId" : NumberLong(0)

1 Like

I got a question.

If the /etc/hosts file was no configured well. I mean, it was containing: dbaMongoServer  // which it's a incorrect hostname. The correct is

After a period of time I corrected the hosts file. Would this have to do with a possible change of the replicaset’s member host to hostname?

On the client side understanding how the client connects to a replicaSet is important.

The client driver treats the hosts in the connection string as seed hosts, once a connection is successfully made the topology of the replicaset is retrieved by the equivalent of db.hello(). This is why the client would have tried to connect to if you specified in your connection string.

Client: connect → Server
Client: hello() → Server
Client ← Server: Replicaset looks like this, and primary is
Client: connect Cannot get an address for

You can specify directConnection=true to work around this behavior, but this is generally not want you want when connection to a replicaset as you want the application to connect to the next primary if the current one fails or is stepped down.

1 Like

Hi @Carlos_Manuel_55780

I think this is related to your mongo.conf file. you possibly have set these following lines:

# network interfaces
  port: 27017

doing so, your instances will listen to local connections and react to only requests made for “” (order matters). This also causes replica set use this name upon initialization. I am guessing you have set it there and forgot about it when you open this post.

although members mostly use same port but different names, you may have same name different ports from different machines but that would be hard to maintain in hosts file.

This is the content of /etc/mongod.conf for net section

  port: 27017
  bindIp:  # Enter,:: to bind to all IPv4 and IPv6 addresses ...

Honestly, the only time I got a similar result to yours was with that addition in the config file. I may still be wrong, but your server might be using another config file having this shape, other than “/etc/mongod.conf”.
Can you please check about this possibility? one way could be ps aux | grep mongod.

The initiate is using the servers own hostname as returned by the OS.

class CmdReplSetInitiate : public ReplSetCommand

HostAndPort me = someHostAndPortForMe();

Which eventually leads to the call of getHostName() in someHostAndPortForMe():

There is only one config file for mongod. On the other side, I can connect to the databse by MongoCompass from my PC. The hostname and IP are not added from Windows hosts file.

According to the previous messages. I keep in mind now that I have to check member host value for primary once added.

Thank you very much

1 Like