我尝试通过Mongoose.connect()尝试连接mongoDB Atlas上的集群,但每次尝试连接时都会出现异常" MongoError:身份验证失败" 我知道MongoDB Atlas是新的mongo,因为服务可能不被mongoose支持吗?。
答案 0 :(得分:2)
The answer in this related post是正确的。你应该:
使用atlas提供的连接字符串 ,并将其提供给
mongoose.connect(uri);
答案 1 :(得分:2)
try {
mongoose.connect( uri, {useNewUrlParser: true, useUnifiedTopology: true}, () =>
console.log("connected"));
}catch (error) {
console.log("could not connect");
}
这很好,请尝试
答案 2 :(得分:2)
MongoError:身份验证失败-这表示您的名称或密码或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";
与月色鹅相连
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");
}
答案 3 :(得分:2)
const mongoAtlasUri =
"mongodb+srv://<username>:<password>@firstcluster.4rc4s.mongodb.net/<dbname>?retryWrites=true&w=majority";
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");
}
const dbConnection = mongoose.connection;
dbConnection.on("error", (err) => console.log(`Connection error ${err}`));
dbConnection.once("open", () => console.log("Connected to DB!"));
答案 4 :(得分:-2)
"mongodb+srv://:@cluster0.vvkuk.mongodb.net/" 同样在地图集,在安全进入网络访问,将有小按钮编辑和删除点击编辑,在编辑中,将有两个选项,第一个选项是添加当前 IP 地址,第二个选项将是允许访问从任何地方 选择第一个选项,然后点击确认