无法连接到 MongoDB Atlas(queryTxt ETIMEOUT)

Can't connect to MongoDB Atlas (queryTxt ETIMEOUT)

我无法通过驱动程序、mongoshell 或 MongoDB 指南针连接到 MongoDB Atlas。报错: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' }

我遵循了 mongodb atlas (mongodb.cloud) 中关于如何连接的指南:

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();
});

将用户名和密码替换为真实的字符串值。我有强烈的感觉原因是 +srv 部分。之前使用mlab时,连接只有mongodb://(没有+srv)

通过联系 MongoDB 支持解决了问题。字符串 URI 应更改为:

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

这些是您可以在集群的指标选项卡上看到的所有主机名(主要和次要)

我遇到过类似的问题。

我的字符串是:

.connect(
    "mongoURI": "mongodb+srv://user:<password>@cluster0-lo1zs.mongodb.net/<dbname>?retryWrites=true&w=majority"
  )

VS 代码终端显示此错误。

[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/>

这引发了一个错误,我已经通过进行此更改解决了:

.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",
)

可以找到有关的更多详细信息here

我在研究了很多网站之后写了这个答案,甚至联系了 MongoDB 支持。

问题出在您的 wifi 的 DNS 服务器上。请更改您的 wifi 或使用移动热点并将当前连接的 IP 列入 MongoDB 上的白名单,这肯定会有所帮助,或者更改您的 wifi 的 DNS。

感谢 David buck 和 Abhay kumar:

此问题与网络连接有关。 wifi 或其他有线连接没有获得传输或连接您的系统与 MongoDB 网络的完整权限。解决方案:Mongodb中的IP白名单,您需要在以太网-IP V4.

下系统的Public DNS中添加8.8.8.8

我通过下拉菜单将版本选择为“2.2.12”简单地解决了这个问题。

字符串更改自

mongodb+srv://username:password@cluster0.nbp7d.mongodb.net/myFirstDatabase?retryWrites=true&w=majority

mongodb://username:password@cluster0-shard-00-00.nbp7d.mongodb.net:27017,cluster0-shard-00-01.nbp7d.mongodb.net:27017,cluster0-shard-00-02.nbp7d.mongodb.net:27017/myFirstDatabase?ssl=true&replicaSet=atlas-11wic9-shard-0&authSource=admin&retryWrites=true&w=majority

它在 2021 年 10 月 20 日对我有用。(Node js 和 Macbook 为 OS)

请查看屏幕截图以获得更多帮助。

我有同样的问题,并通过降级节点驱动程序的连接版本而不是使用节点 v4 尝试使用节点 2.2 来修复它。