- Error code
- 6
- Code name
HostUnreachable- Product
- MongoDB
- Versions
- MongoDB 7.0 and 8.0 (wordings checked against the 8.0 source)
- Category
- Replication & Connectivity
- Last reviewed
- 24 October 2026
On this page
What it means
Error 6 (HostUnreachable) means a MongoDB server could not reach another MongoDB process it needed: a replica set member, a shard or a config server. The text after "caused by" is the network reason, such as Connection refused.
This is mainly a server-to-server error. Replica set members send each other heartbeats, secondaries pull the oplog from a sync source, and mongos talks to shards and config servers. When one of those connections fails, MongoDB 8.0's internal connection pool reports the failure as HostUnreachable and keeps the original reason. The message names the host as configured and, in brackets, the address it resolved to, then the cause: Connection refused (nothing listening on that port), a time-out, or a closed or reset connection. The cause text after the last caused by comes from the operating system or network layer, so its wording can vary by platform.
Applications usually do not see code 6 directly. A driver that cannot reach a server reports its own error, typically a server selection timeout; code 6 appears in rs.status() (in a member's lastHeartbeatMessage), in the server log, and in errors that mongos passes back when a shard cannot be reached. error_codes.yml puts it in the NetworkError and RetriableError categories.
The documentation's replica set troubleshooting guide is the reference here: every member must be able to connect to every other member, in both directions, and a mongod bound only to localhost does not accept connections from other hosts, including other members.
Common causes
- The target mongod or mongos is stopped, crashed or still starting, so the port refuses connections.
- A firewall, security group or network ACL blocks the port between the two hosts, often in only one direction.
- The target listens only on localhost (net.bindIp) or on another port than the one in the replica set configuration.
- The host name in the replica set configuration resolves to a wrong or unreachable address from this member.
- A network partition, or connections closed by a load balancer or proxy between members.
Diagnose it
Read-only These mongosh commands only read data, indexes, statistics and server state (find, aggregate without $out or $merge, getIndexes, stats, currentOp, rs.status). They do not change data, users or settings, so they are safe to run first.
Which members cannot be reached, and why?
rs.status().members.map(m => ({ name: m.name, health: m.health, state: m.stateStr, msg: m.lastHeartbeatMessage })) Members with health: 0 are down from this member's point of view; lastHeartbeatMessage holds the reason, such as Error connecting to ... :: caused by :: Connection refused.
Which host names does the set use?
rs.conf().members.map(m => m.host) The exact host:port of each member. Every member, and every client, must be able to resolve and reach each of these names.
Test the connection from the host that reports the error
mongosh --host m2.example.net --port 27017 --eval "db.hello().me" Run from the member that reports the error, as the troubleshooting guide describes, and test both directions. A refusal or time-out here confirms a network, firewall or bind problem rather than a MongoDB one.
What is the target listening on?
// on the target member
db.adminCommand({ getCmdLineOpts: 1 }).parsed.net The bindIp and port the server started with. If bindIp is only localhost, other members cannot connect.
How to fix it
Each fix is labelled with what it changes. Try read-only checks and the least invasive fix first, and test changes outside production.
Start the target process and check why it stopped
Changes database stateIf the target is down, start it with its service manager and read its log for the reason it stopped before relying on it again.
# Linux (systemd)
sudo systemctl start mongod
# Windows (PowerShell, default service name)
Start-Service MongoDB
Open the port between members
Changes configurationAllow the MongoDB port (27017 by default) between all members and from mongos to every shard and config server, in both directions. Limit the rule to those hosts and your application servers.
Bind to the interface other members use
Changes configurationAdd the host's own name or private address to net.bindIp in the configuration file, for example bindIp: localhost,m2.example.net, and restart. Keep authentication on and use TLS when other hosts can connect; never open the port to all interfaces and networks without both.
Caution: A restart makes the member unavailable for a short time; on the primary it causes an election. Restart one member at a time and confirm it is healthy in rs.status() before the next.
Correct the member host names in the replica set configuration
Changes configurationIf a member is configured with a name that other members cannot resolve (for example a container-internal name), change it to a resolvable name on the primary.
Caution: A wrong host name in rs.reconfig can remove a member from the set or cost the majority. Change one member at a time, keep a copy of the old configuration, and check rs.status() afterwards.
cfg = rs.conf()
cfg.members[1].host = "m2.example.net:27017"
rs.reconfig(cfg)
Examples
A member that is down, seen in rs.status()
Not reproduced here: no MongoDB server is installed for this site. The field names are from the replSetGetStatus documentation; the heartbeat message follows the format built by makeConnectError() in MongoDB 8.0's asio_transport_layer.cpp, which the connection pool reports as HostUnreachable. The state text and host names are illustrative.
rs.status().members[1]
{
name: 'm2.example.net:27017',
health: 0,
stateStr: '(not reachable/healthy)',
lastHeartbeatMessage: 'Error connecting to m2.example.net:27017 (10.0.0.12:27017) :: caused by :: Connection refused',
...
}
Choosing a sync source that cannot be reached
Not reproduced here: no MongoDB server is installed for this site. The text is built for the replSetSyncFrom command in MongoDB 8.0's topology_coordinator.cpp with code HostUnreachable when the requested member is not reachable from this one; the host name is illustrative.
rs.syncFrom("m3.example.net:27017")
MongoServerError: I cannot reach the requested member: m3.example.net:27017
How to prevent it
Use stable, resolvable host names for members, monitor member health and heartbeat messages, and test connectivity in both directions whenever firewall rules or networks change.
Keep members on the same port and bind configuration, and document which hosts must reach which, including mongos to every shard and config server.
Common questions
What does "Error connecting to ... caused by Connection refused" mean?
The host was reachable but nothing accepted the connection on that port: the mongod is not running, listens on another port, or is bound to localhost only.
Why does my application show a server selection timeout instead of error 6?
Drivers report their own connection errors. Code 6 is mostly seen between MongoDB processes: in rs.status(), the server log and errors relayed by mongos.
Is HostUnreachable retriable?
MongoDB classes it as a network error and a retriable error, so internal operations retry; a refusal that lasts means the target is down or blocked and needs fixing.
Learn more on CodeWithSQL
Official documentation
- Troubleshoot replica sets (test connections between members)
- replSetGetStatus (lastHeartbeatMessage)
- Security checklist and IP binding
- MongoDB error codes (6 HostUnreachable)
Last technically reviewed 24 October 2026; examples and diagnostics run on MongoDB 8.0 documentation and source (error_codes.yml r8.0.0); not run locally. Codes and code names are checked against MongoDB's error_codes.yml (8.0); no MongoDB server was available, so the examples were not run locally.