Cannot connect to MongoDB replica set cluster using Compass on localhost

Hi, I am trying to connect from my localhost computer to my 3 VM replica set nodes using Compass but I am getting ‘connection timed out’.
Here is my /etc/mongod.conf files:
1.

 logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1, 172.20.10.4


# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo

#security:

#operationProfiling:

replication:
  replSetName: "rs0"
#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:
# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true
#  engine:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1, 172.20.10.10


# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo

#security:

#operationProfiling:

replication:
  replSetName: "rs0"

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:
# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true
#  engine:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1, 172.20.10.11


# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo

#security:

#operationProfiling:

replication:
  replSetName: "rs0"
#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

Here is how I created the replica set:

> rs.initiate(
... {
... _id: "rs0",
... members: [
... { _id: 0, host: "172.20.10.4" },
... { _id: 1, host: "172.20.10.10" },
... { _id: 2, host: "172.20.10.11" }
... ]
... })

Here is the result of command - rs.status():

rs0:PRIMARY> rs.status()
{
	"set" : "rs0",
	"date" : ISODate("2023-10-27T16:39:16.746Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"majorityVoteCount" : 2,
	"writeMajorityCount" : 2,
	"votingMembersCount" : 3,
	"writableVotingMembersCount" : 3,
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1698424756, 1),
			"t" : NumberLong(1)
		},
		"lastCommittedWallTime" : ISODate("2023-10-27T16:39:16.726Z"),
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1698424756, 1),
			"t" : NumberLong(1)
		},
		"appliedOpTime" : {
			"ts" : Timestamp(1698424756, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1698424746, 1),
			"t" : NumberLong(1)
		},
		"lastAppliedWallTime" : ISODate("2023-10-27T16:39:16.726Z"),
		"lastDurableWallTime" : ISODate("2023-10-27T16:39:06.725Z")
	},
	"lastStableRecoveryTimestamp" : Timestamp(1698424706, 1),
	"electionCandidateMetrics" : {
		"lastElectionReason" : "electionTimeout",
		"lastElectionDate" : ISODate("2023-10-27T16:07:36.459Z"),
		"electionTerm" : NumberLong(1),
		"lastCommittedOpTimeAtElection" : {
			"ts" : Timestamp(1698422845, 1),
			"t" : NumberLong(-1)
		},
		"lastSeenOpTimeAtElection" : {
			"ts" : Timestamp(1698422845, 1),
			"t" : NumberLong(-1)
		},
		"numVotesNeeded" : 2,
		"priorityAtElection" : 1,
		"electionTimeoutMillis" : NumberLong(10000),
		"numCatchUpOps" : NumberLong(0),
		"newTermStartDate" : ISODate("2023-10-27T16:07:36.548Z"),
		"wMajorityWriteAvailabilityDate" : ISODate("2023-10-27T16:07:37.685Z")
	},
	"members" : [
		{
			"_id" : 0,
			"name" : "172.20.10.4:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 2348,
			"optime" : {
				"ts" : Timestamp(1698424756, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2023-10-27T16:39:16Z"),
			"lastAppliedWallTime" : ISODate("2023-10-27T16:39:16.726Z"),
			"lastDurableWallTime" : ISODate("2023-10-27T16:39:06.725Z"),
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"electionTime" : Timestamp(1698422856, 1),
			"electionDate" : ISODate("2023-10-27T16:07:36Z"),
			"configVersion" : 1,
			"configTerm" : 1,
			"self" : true,
			"lastHeartbeatMessage" : ""
		},
		{
			"_id" : 1,
			"name" : "172.20.10.10:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 1910,
			"optime" : {
				"ts" : Timestamp(1698424746, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1698424746, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2023-10-27T16:39:06Z"),
			"optimeDurableDate" : ISODate("2023-10-27T16:39:06Z"),
			"lastAppliedWallTime" : ISODate("2023-10-27T16:39:16.726Z"),
			"lastDurableWallTime" : ISODate("2023-10-27T16:39:16.726Z"),
			"lastHeartbeat" : ISODate("2023-10-27T16:39:15.593Z"),
			"lastHeartbeatRecv" : ISODate("2023-10-27T16:39:15.004Z"),
			"pingMs" : NumberLong(1),
			"lastHeartbeatMessage" : "",
			"syncSourceHost" : "172.20.10.4:27017",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1,
			"configTerm" : 1
		},
		{
			"_id" : 2,
			"name" : "172.20.10.11:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 1910,
			"optime" : {
				"ts" : Timestamp(1698424746, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1698424746, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2023-10-27T16:39:06Z"),
			"optimeDurableDate" : ISODate("2023-10-27T16:39:06Z"),
			"lastAppliedWallTime" : ISODate("2023-10-27T16:39:16.726Z"),
			"lastDurableWallTime" : ISODate("2023-10-27T16:39:16.726Z"),
			"lastHeartbeat" : ISODate("2023-10-27T16:39:15.593Z"),
			"lastHeartbeatRecv" : ISODate("2023-10-27T16:39:15.058Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncSourceHost" : "172.20.10.4:27017",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1,
			"configTerm" : 1
		}
	],
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1698424756, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1698424756, 1)
}

Here is the string that I am using when connecting with Compass:
mongodb://172.20.10.4,172.20.10.10,172.20.10.11/?replicaSet=rs0

Guys I find the problem, check the ufw rules if you activated ufw for your VM’s, make sure you allow connections for your localhost on port 27017. sudo ufw allow from localhostul-ip to any port 27017

1 Like

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