How to fix DNSHostNotFound: Failed to look up service "_mongodb._tcp.cluster0.mongodb.net" in MongoDB
This DNS lookup error occurs when your system cannot resolve MongoDB Atlas SRV records, typically due to DNS server limitations or network configuration issues preventing mongodb+srv:// connections.
Quick fix
Fixes DNSHostNotFound: Failed to look up service "_mongodb._tcp.cluster0.mongodb.net"
# Edit resolv.conf
sudo nano /etc/resolv.conf
# Add Google DNS
nameserver 8.8.8.8
nameserver 8.8.4.4
# Or Cloudflare DNS
nameserver 1.1.1.1
nameserver 1.0.0.1- Verify the MongoDB Atlas cluster exists and is active
- Switch to Google Public DNS or Cloudflare DNS
- Use standard connection string instead of SRV format
- Update MongoDB driver to latest version
# Edit resolv.confsudo nano /etc/resolv.conf# Add Google DNSnameserver 8.8.8.8nameserver 8.8.4.4# Or Cloudflare DNSnameserver 1.1.1.1nameserver 1.0.0.1DNSHostNotFound: Failed to look up service "_mongodb._tcp.cluster0.mongodb.net"
What this error means
This error occurs when MongoDB drivers attempt to use the mongodb+srv:// connection string format but cannot resolve the DNS SRV records required for cluster discovery. MongoDB Atlas uses DNS SRV records (Service records) prefixed with _mongodb._tcp to automatically discover all replica set members in a cluster. When your system's DNS resolver cannot look up these special SRV records, the connection fails immediately. The mongodb+srv:// protocol is a convenience feature that allows MongoDB drivers to query DNS for the full list of hosts in your cluster, eliminating the need to hardcode every replica set member in your connection string. However, this requires a DNS server that supports SRV record lookups, which some ISP-provided DNS servers and restrictive network environments do not provide. This error is most commonly seen with MongoDB Atlas connections but can occur with any MongoDB deployment using SRV-based connection strings.
Typical symptoms
- Connection to MongoDB Atlas fails immediately with DNSHostNotFound error
- Application cannot connect when using mongodb+srv:// format
- Connection works locally but fails in certain network environments (Docker, Kubernetes, corporate networks)
- Standard mongodb:// connections work but mongodb+srv:// connections do not
- Error message includes "_mongodb._tcp" in the service name
Common causes
- ISP DNS server does not support SRV record lookups required by mongodb+srv:// protocol
- Corporate firewall or network policy blocking DNS SRV queries
- DNS resolver configuration in Docker or Kubernetes not supporting SRV lookups
- Outdated MongoDB driver version that does not properly handle SRV connections
- Network environment using DNS server that filters or blocks certain record types
- Typo in the connection string hostname or cluster name
- MongoDB Atlas cluster is paused, deleted, or the hostname has changed
How to fix "DNSHostNotFound: Failed to look up service "_mongodb._tcp.cluster0.mongodb.net""
Log into your MongoDB Atlas account and confirm:
- The cluster is running (not paused or terminated)
- The cluster hostname matches your connection string
- Copy the connection string again from Atlas to ensure accuracy
In Atlas Dashboard:
1. Navigate to your cluster
2. Click "Connect"
3. Choose "Connect your application"
4. Copy the fresh connection string and compare with yours
Change your system DNS to a public DNS server that supports SRV records.
On Linux:
# Edit resolv.conf
sudo nano /etc/resolv.conf
# Add Google DNS
nameserver 8.8.8.8
nameserver 8.8.4.4
# Or Cloudflare DNS
nameserver 1.1.1.1
nameserver 1.0.0.1On macOS:
# Open System Preferences > Network
# Select your connection > Advanced > DNS
# Add 8.8.8.8 and 8.8.4.4On Windows:
# Open Network Connections
# Right-click adapter > Properties
# Select IPv4 > Properties
# Use: 8.8.8.8 and 8.8.4.4For Docker containers:
// In docker-compose.yml
services:
app:
dns:
- 8.8.8.8
- 8.8.4.4Replace mongodb+srv:// with mongodb:// and specify hosts explicitly.
Original SRV format:
mongodb+srv://username:password@cluster0.mongodb.net/databaseStandard format:
mongodb://username:password@cluster0-shard-00-00.mongodb.net:27017,cluster0-shard-00-01.mongodb.net:27017,cluster0-shard-00-02.mongodb.net:27017/database?ssl=true&replicaSet=atlas-xxxxx-shard-0&authSource=admin&retryWrites=true&w=majorityGet the standard format from Atlas:
1. Click "Connect" on your cluster
2. Choose "Connect your application"
3. Select "2.2.12 or later" driver version (shows standard format)
4. Click "I am not using the SRV connection format"
Note: You'll need to manually update if Atlas adds/removes nodes.
Ensure you're using a MongoDB driver version that properly supports SRV connections.
Node.js:
npm install mongodb@latest
# or
npm install mongoose@latestPython:
pip install --upgrade pymongoJava:
<!-- Update pom.xml -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>4.11.1</version>
</dependency>Minimum versions for SRV support:
- Node.js: mongodb 3.1.0+, mongoose 5.2.0+
- Python: pymongo 3.6.0+
- Java: 3.6.0+
- C#: 2.5.0+
Verify your system can resolve SRV records:
Using dig (Linux/macOS):
dig +short SRV _mongodb._tcp.cluster0.mongodb.netExpected output shows host and port:
0 0 27017 cluster0-shard-00-00.mongodb.net.
0 0 27017 cluster0-shard-00-01.mongodb.net.
0 0 27017 cluster0-shard-00-02.mongodb.net.Using nslookup (Windows):
nslookup -type=SRV _mongodb._tcp.cluster0.mongodb.netIf no results appear, your DNS server doesn't support SRV lookups. Switch to a different DNS server (step 2) or use standard format (step 3).
For Docker and Kubernetes deployments:
Docker Compose:
version: '3.8'
services:
app:
image: myapp
dns:
- 8.8.8.8
- 1.1.1.1
dns_search:
- .Kubernetes Pod:
apiVersion: v1
kind: Pod
metadata:
name: myapp
spec:
dnsPolicy: "None"
dnsConfig:
nameservers:
- 8.8.8.8
- 1.1.1.1Environment variable workaround (some drivers):
export MONGODB_DNS_SERVER=8.8.8.8Advanced notes
SRV vs Standard Connection Strings:
The mongodb+srv:// format is convenient because it automatically discovers all replica set members via DNS, making connection strings shorter and self-updating when Atlas scales your cluster. However, it requires DNS infrastructure that supports SRV (Service) and TXT (for connection options) record types. The standard mongodb:// format works everywhere but requires manual updates if your cluster topology changes.
Corporate and Restricted Networks:
Many corporate networks use DNS servers that filter record types for security reasons, blocking SRV queries. If you're behind a corporate firewall and cannot change DNS settings, the standard connection string is your best option. Contact your network administrator if you need SRV support enabled.
DNS Caching Issues:
After switching DNS servers, you may need to flush your DNS cache:
- Linux: sudo systemd-resolve --flush-caches or sudo service nscd restart
- macOS: sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
- Windows: ipconfig /flushdns
Kubernetes DNS Considerations:
Kubernetes uses CoreDNS or kube-dns, which may have limitations with external SRV lookups. Setting dnsPolicy: "None" with explicit nameservers bypasses cluster DNS for your pod. Alternatively, configure CoreDNS to forward SRV queries to external resolvers.
Driver-Specific Behavior:
Some drivers (particularly Java and .NET) may have environment-specific DNS resolution issues. Check your driver's documentation for DNS configuration options. The Node.js driver uses the system's DNS resolver, while others may have built-in resolvers that behave differently.
Atlas IP Whitelisting:
After resolving DNS issues, ensure your IP address is whitelisted in Atlas Network Access settings, or you'll encounter authentication errors instead.
Frequently asked questions
- What does "DNSHostNotFound: Failed to look up service "_mongodb._tcp.cluster0.mon" mean?
- This error occurs when MongoDB drivers attempt to use the mongodb+srv:// connection string format but cannot resolve the DNS SRV records required for cluster discovery. _tcp to automatically discover all replica set members in a cluster. When your system's DNS resolver cannot look up these special SRV records, the connection fails immediately.
- How do I fix "DNSHostNotFound: Failed to look up service "_mongodb._tcp.cluster0.mon"?
- This DNS lookup error occurs when your system cannot resolve MongoDB Atlas SRV records, typically due to DNS server limitations or network configuration issues preventing mongodb+srv:// connections.
- What causes "DNSHostNotFound: Failed to look up service "_mongodb._tcp.cluster0.mon"?
- ISP DNS server does not support SRV record lookups required by mongodb+srv:// protocol Corporate firewall or network policy blocking DNS SRV queries DNS resolver configuration in Docker or Kubernetes not supporting SRV lookups
- Is the "DNSHostNotFound: Failed to look up service "_mongodb._tcp.cluster0.mon" error serious?
- It is a medium-severity error โ more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- ๐MongoDB Official Documentation
- ๐mongodb.com - troubleshoot connection(Official Docs)
- ๐
- ๐mongodb.com - 109605(Official Docs)
- ๐
- ๐mongodb.com - 315254(Official Docs)