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.
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.
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:[email protected]/databaseStandard format:
mongodb://username:[email protected]: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.8SRV 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.
StaleShardVersion: shard version mismatch
How to fix "StaleShardVersion: shard version mismatch" in MongoDB
MongoOperationTimeoutError: Operation timed out
How to fix "MongoOperationTimeoutError: Operation timed out" in MongoDB
MongoServerError: PlanExecutor error during aggregation :: caused by :: Sort exceeded memory limit of 104857600 bytes, but did not opt in to external sorting. Aborting operation.
How to fix "QueryExceededMemoryLimitNoDiskUseAllowed" in MongoDB
MissingSchemaError: Schema hasn't been registered for model
How to fix "MissingSchemaError: Schema hasn't been registered for model" in MongoDB/Mongoose
CastError: Cast to ObjectId failed for value "abc123" at path "_id"
How to fix "CastError: Cast to ObjectId failed" in MongoDB