问题
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)
回答1:
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
回答2:
I've faced a similar problem.
My string was:
.connect(
"mongoURI": "mongodb+srv://user:<password>@cluster0-lo1zs.mongodb.net/<dbname>?retryWrites=true&w=majority"
)
VS code terminal was shows this errors.
[0] Server started on port 9999<br/>
[0] queryTxt ETIMEOUT cluster0-lo1zs.mongodb.net<br/>
[1] Proxy error: Could not proxy request /api/profile/user/5f2be6e70fa40287805f6488 from localhost:3000
to http://localhost:9999/.<br/>
[1] See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (ECONNRESET).<br/>
[1]<br/>
[0] [nodemon] app crashed - waiting for file changes before starting...<br/>
This raised an error I've solved by making this change:
.connect(
"mongoURI": "mongodb://user:<password>@cluster0-shard-00-00-lo1zs.mongodb.net:27017,cluster0-shard-00-01-lo1zs.mongodb.net:27017,cluster0-shard-00-02-lo1zs.mongodb.net:27017/<dbname>?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true",
)
More details about can be found here
来源:https://stackoverflow.com/questions/56334603/cant-connect-to-mongodb-atlas