我在尝试连接到 MongoDB 时遇到问题。我在这里使用 Mongoose ,
console.log("trying here"); // prints
await mongoose.connect(db, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false
});
console.log("Mongo DB connected"); // never reached
它会抛出一个错误
getaddrinfo ENOTFOUND cluster0-shard-00-01-abcde.mongodb.net
我以前从未遇到过这个错误,它过去工作得很好——不知道从这里到哪里去。我已经尝试构建一个新的集群,通过 IP 的网络访问设置为公共(public) 0.0.0.0
。它只是不断抛出相同的错误。
最佳答案
请注意,如果指定 useNewUrlParser: true
,则必须在连接字符串中指定一个端口,
像 mongodb://localhost:27017/dbname
。
新的 url 解析器不支持没有端口的连接字符串,例如 mongodb://localhost/dbname
。
还有一个选项,如果你使用 node.js +mongo Atlas:
- 登录https://cloud.mongodb.com/
- 转到您的 cluster0 或 其他集群,
- 点击连接...选择连接您的应用程序
- 选择 node.js,然后选择 2.2.12 或更高版本 -
您将获得以 mongodb://开头的连接字符串
insted of mongodb+srv://(for node js 3.0 or later)
关于node.js - MongoDB 连接错误 getaddrinfo ENOTFOUND Cluster0_shard_name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57853987/