简体   繁体   English

来自Node.JS的MongoDB Atlas授权失败

[英]Authorization fail to MongoDB Atlas from Node.JS

I am trying to connect to my mongoDB atlas cluster but get an authentication fail. 我正在尝试连接到我的mongoDB atlas集群,但是验证失败了。 I am able to connect from a client like Studio 3T and from the Mongo shell. 我能够从Studio 3T和Mongo shell等客户端进行连接。

Here's my connection URI: 这是我的连接URI:

var conn = mongoose.connect("mongodb://<user>:<password>@xxx-shard-00-00-kqmqb.mongodb.net:27017,xxx-shard-00-01-kqmqb.mongodb.net:27017,xxx-shard-00-02-kqmqb.mongodb.net:27017/myDB?ssl=true&replicaSet=xxxCluster-shard-0&authSource=admin")

I copied this from the atlas console. 我从atlas控制台复制了这个。 I'm using Mongoose 4.9.7 which uses MongoDB 2.2.26 so I'm using the latest versions of these modules. 我正在使用Mongoose 4.9.7,它使用MongoDB 2.2.26,所以我使用的是这些模块的最新版本。

The error I get is the following: 我得到的错误如下:

MongoError: authentication fail

Any idea what this could be? 知道这可能是什么?

Found the solution. 找到了解决方案。 My password has special characters so I have to encode it properly. 我的密码有特殊字符,所以我必须正确编码。 Changed the implementation to 将实现更改为

var f = require('util').format;    
var user = encodeURIComponent('user');
var password = encodeURIComponent('p@ssw0rdWithSpecialCharacter');
var url = f("mongodb://%s:%s@xxx-shard-00-00-kqmqb.mongodb.net:27017,xxx-shard-00-01-kqmqb.mongodb.net:27017,xxx-shard-00-02-kqmqb.mongodb.net:27017/myDB?ssl=true&replicaSet=xxx-shard-0&authSource=admin",user,password);
var conn = mongoose.connect(url);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM