Mongo authentication fail or invalid connection string

时间:2019-04-03 09:42:32

标签: node.js mongoose

Using "mongoose": "^5.4.21", and Mongo Atlas, I can not seem to connect to my database in my node.js application

I tried to encode my connection string but that gives me Invalid connection string, if I remove encodeURIComponent I get authentication failed

mongoose.connect(encodeURIComponent("mongodb+srv://name:}Izu@[{!6o@cluster-oxzyp.mongodb.net/"), { dbName: "website", useNewUrlParser: true });

1 个答案:

答案 0 :(得分:0)

您可以将连接URI的用户名和密码部分分成options部分,如下所示:

mongoose.connect("mongodb+srv://cluster-oxzyp.mongodb.net", {
user: 'name',   //assuming this is your username
pass: '}Izu@[{!6o', //assuming this is your password
dbName: "website",
useNewUrlParser: true

});

这很可能是由于您的密码中包含@字符造成的。或者您可以尝试如下操作:

mongoose.connect(`mongodb+srv://name:${encodeURIComponent('}Izu@[{!6o')}@cluster-oxzyp.mongodb.net/`, {
user: '',
pass: '',
dbName: 'website',
useNewUrlParser: true });