Error
Error Code: 231

MongoDB Error 231: DNS Resolution Failure

📦 MongoDB
📋

Description

Error 231, 'D N S Protocol Error', indicates that MongoDB encountered an issue while attempting to resolve a hostname via the Domain Name System. This often occurs when MongoDB tries to connect to other servers, such as replica set members or sharded cluster components, but cannot successfully communicate with a DNS server or interpret its response.
💬

Error Message

D N S Protocol Error
🔍

Known Causes

4 known causes
⚠️
Incorrect DNS Configuration
The operating system or network interface where MongoDB is running has misconfigured DNS settings, preventing proper hostname resolution.
⚠️
Network Connectivity Issues
Network problems, such as firewalls blocking DNS traffic (port 53) or general network outages, prevent MongoDB from reaching DNS servers.
⚠️
Unreachable DNS Server
The configured DNS server is unavailable, unresponsive, or experiencing issues, leading to failed resolution attempts by MongoDB.
⚠️
Hostname Resolution Problems
The specific hostname MongoDB is trying to resolve does not exist, is misspelled, or is not correctly registered in DNS.
🛠️

Solutions

3 solutions available

1. Verify Network Connectivity and DNS Server Configuration easy

Ensure the MongoDB server can reach its DNS servers and that DNS is configured correctly.

1
From the MongoDB server (or the client attempting to connect), ping a known external domain to test general network connectivity.
ping google.com
2
From the MongoDB server, attempt to resolve the domain name of your MongoDB cluster or replica set members. Replace `your.mongodb.domain` with the actual domain.
nslookup your.mongodb.domain
3
Check the DNS server configuration on the operating system where MongoDB is running. This is typically found in `/etc/resolv.conf` on Linux/macOS or through network adapter settings on Windows.
cat /etc/resolv.conf
4
If the DNS servers listed in `resolv.conf` are incorrect or unreachable, update them to valid and accessible DNS servers (e.g., 8.8.8.8 for Google DNS, 1.1.1.1 for Cloudflare DNS). This may require administrator privileges.
# Example for Linux - edit /etc/resolv.conf directly or use network manager tools
nameserver 8.8.8.8
nameserver 1.1.1.1
5
Restart the MongoDB service to ensure it picks up the new DNS configuration.
# For systems using systemd
sudo systemctl restart mongod

# For systems using init.d
sudo service mongod restart

2. Check MongoDB Connection String and Hostnames easy

Ensure the MongoDB connection string uses correct and resolvable hostnames.

1
Review the MongoDB connection string used by your application or `mongosh` client. Verify that all hostnames or IP addresses listed are accurate and can be resolved.
# Example connection string
mongodb://mongo1.example.com:27017,mongo2.example.com:27017/mydatabase?replicaSet=myrepl
2
If using hostnames, perform a `nslookup` or `dig` on each hostname from the client machine to confirm they resolve correctly to the expected IP addresses.
nslookup mongo1.example.com
3
If you encounter issues with DNS resolution for hostnames, consider temporarily using IP addresses in the connection string to isolate the problem. Note that this is not a long-term solution for dynamic environments.
# Example using IP addresses (temporary)
mongodb://192.168.1.10:27017,192.168.1.11:27017/mydatabase?replicaSet=myrepl
4
If the issue persists with hostnames, investigate your local `hosts` file (`/etc/hosts` on Linux/macOS, `C:\Windows\System32\drivers\etc\hosts` on Windows) to ensure there are no conflicting or incorrect entries for your MongoDB hosts.
# Example of a hosts file entry
192.168.1.10 mongo1.example.com

3. Configure MongoDB's `net.bindIp` and Network Interfaces medium

Ensure MongoDB is configured to listen on the correct network interfaces accessible by DNS.

1
Locate your MongoDB configuration file (`mongod.conf` or `mongos.conf`). This is typically found at `/etc/mongod.conf` on Linux.
# Example path
/etc/mongod.conf
2
Examine the `net.bindIp` setting. If it's set to `localhost` or `127.0.0.1`, MongoDB will only accept connections from the local machine. For remote connections, it needs to be set to the server's IP address or `0.0.0.0` (to listen on all interfaces).
# Example configuration snippet
net:
  port: 27017
  bindIp: 192.168.1.10, 127.0.0.1  # Listen on a specific IP and localhost
3
If `bindIp` is set to specific IP addresses, ensure these IP addresses are correctly resolvable via DNS on your network. If you are using a hostname in your connection string, that hostname must resolve to one of the IPs listed in `bindIp`.
# To listen on all available network interfaces (use with caution and ensure firewall rules are in place)
net:
  port: 27017
  bindIp: 0.0.0.0
4
After modifying the configuration file, restart the MongoDB service.
sudo systemctl restart mongod
🔗

Related Errors

5 related errors