我正在尝试通过 Mongoose.connect() 连接到我在 mongoDB Atlas 上的集群,但是每次我尝试连接时都会出现异常“MongoError:身份验证失败”我知道 MongoDB Atlas 是新的 mongo 作为服务可能不是猫鼬支持了吗?
原文由 M Hilal 发布,翻译遵循 CC BY-SA 4.0 许可协议
我正在尝试通过 Mongoose.connect() 连接到我在 mongoDB Atlas 上的集群,但是每次我尝试连接时都会出现异常“MongoError:身份验证失败”我知道 MongoDB Atlas 是新的 mongo 作为服务可能不是猫鼬支持了吗?
原文由 M Hilal 发布,翻译遵循 CC BY-SA 4.0 许可协议
MongoError: authentication fails - 这意味着你的名字或密码或 dbname 不正确 -
uri 样本 -
const uri =
"mongodb+srv://<username>:<password>@firstcluster.4rc4s.mongodb.net/<dbname>?retryWrites=true&w=majority";
假设用户名是 - najim ,密码是 1234 ,dbname 是 pets (注意 - 默认 dbname 是 test 但你可以写任何你想写的)那么我的 uri 将包含上面的 credentails -
const mongoAtlasUri =
"mongodb+srv://najim:1234@firstcluster.4rc4s.mongodb.net/pets?retryWrites=true&w=majority";
与 moongoose 联系
try {
// Connect to the MongoDB cluster
mongoose.connect(
mongoAtlasUri,
{ useNewUrlParser: true, useUnifiedTopology: true },
() => console.log(" Mongoose is connected")
);
} catch (e) {
console.log("could not connect");
}
原文由 Najim Ali 发布,翻译遵循 CC BY-SA 4.0 许可协议
5 回答4.8k 阅读✓ 已解决
4 回答2.4k 阅读✓ 已解决
2 回答1.7k 阅读✓ 已解决
5 回答1.9k 阅读
2 回答1.3k 阅读✓ 已解决
3 回答2k 阅读
1 回答3.2k 阅读
这个相关帖子中的答案 是正确的。你应该: