Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

express - Can't connect to MongoDB Atlas (queryTxt ETIMEOUT)

I can't connect to MongoDB Atlas either via driver, mongoshell, or MongoDB compass. Get to error: queryTxt ETIMEOUT

Error:  { Error: queryTxt ETIMEOUT clustermasjeed1-ekpfe.mongodb.net
    at QueryReqWrap.onresolve [as oncomplete] (dns.js:197:19)
  errno: 'ETIMEOUT',
  code: 'ETIMEOUT',
  syscall: 'queryTxt',
  hostname: 'clustermasjeed1-ekpfe.mongodb.net' }

I followed the guide from mongodb atlas (mongodb.cloud) on how to connect:

const MongoClient = require(‘mongodb’).MongoClient;
const uri = "mongodb+srv://<username>:<password>@clustermasjeed1-ekpfe.mongodb.net/test?retryWrites=true";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
  const collection = client.db("test").collection("devices");
  // perform actions on the collection object
  client.close();
});

with username and password replaced with real string value. I have strong feeling the cause is +srv part. When using mlab before, the connection is only mongodb:// (without the +srv)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Resolved the issue by contacting MongoDB support. The string URI should be changed to:

mongodb://<username>:<password>@clustermasjeed1-shard-00-00-ekpfe.mongodb.net:27017,clustermasjeed1-shard-00-01-ekpfe.mongodb.net:27017,clustermasjeed1-shard-00-02-ekpfe.mongodb.net:27017/test?ssl=true&replicaSet=ClusterMasjeed1-shard-0&authSource=admin&retryWrites=true

These are all the hostnames (primary and secondary) that you can see on the metrics tab of the cluster


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...