我希望有人能帮助我,因为我混淆了这个问题,我以为我的代码是好的,但阿特拉斯似乎拒绝,所以我无法连接到阿特拉斯。连接如此简单。我填写这些参数从nodemon.json:
const {
MONGODB_ATLAS_USERNAME,
MONGODB_ATLAS_PASSWORD,
MONGODB_ATLAS_DBNAME
} = process.env
var mongoose = require('mongoose');
app.listen(PORT, () => {
console.info(`App is running! at http://localhost:${PORT}`)
})
mongoose.connect(`mongodb://${MONGODB_ATLAS_USERNAME}:${MONGODB_ATLAS_PASSWORD}@cluster0.lzjty.mongodb.net/${MONGODB_ATLAS_DBNAME}?retryWrites=true&w=majority&ssl=true`,{useNewUrlParser:true,useUnifiedTopology:true});
在Atlas上,我将我的IP列入白名单,并允许所有IP连接
,但每次运行npm时,返回总是:
App is running! at http://localhost:8080
[1] (node:2012) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/
2条答案
按热度按时间rseugnpd1#
在我的例子中,我添加了&tls=true,它工作了。
yx2lnoni2#
一般在使用公共IP的时候会发生,每次上网都会发生变化,需要对MONGOMap集进行修改,我建议您进行这些修改,以利于发展。
遵循以下命令:-
MongoDB Atlas
-〉NetworkAccess
-〉Edit
-〉Allow Any