1
votes

I am trying to connect to my mongoDB atlas cluster but get an authentication fail. I am able to connect from a client like Studio 3T and from the Mongo shell.

Here's my connection 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. I'm using Mongoose 4.9.7 which uses MongoDB 2.2.26 so I'm using the latest versions of these modules.

The error I get is the following:

MongoError: authentication fail

Any idea what this could be?

1

1 Answers

3
votes

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:%[email protected]: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);