Mongoose: MongoError: authentication fail while trying to connect to database through mongoose client

Created on 10 Apr 2018  路  4Comments  路  Source: Automattic/mongoose

Hi I have encountered this error while connecting through mongoose.

Error: { MongoError: authentication fail at /Users/srinivas/Documents/JavaScript/nodejs/my-node-shop/node_modules/mongodb-core/lib/topologies/replset.js:1429:15 at /Users/srinivas/Documents/JavaScript/nodejs/my-node-shop/node_modules/mongodb-core/lib/connection/pool.js:874:7 at /Users/srinivas/Documents/JavaScript/nodejs/my-node-shop/node_modules/mongodb-core/lib/connection/pool.js:850:20 at finish (/Users/srinivas/Documents/JavaScript/nodejs/my-node-shop/node_modules/mongodb-core/lib/auth/scram.js:174:16) at handleEnd (/Users/srinivas/Documents/JavaScript/nodejs/my-node-shop/node_modules/mongodb-core/lib/auth/scram.js:184:7) at /Users/srinivas/Documents/JavaScript/nodejs/my-node-shop/node_modules/mongodb-core/lib/auth/scram.js:289:15 at /Users/srinivas/Documents/JavaScript/nodejs/my-node-shop/node_modules/mongodb-core/lib/connection/pool.js:541:18 at _combinedTickCallback (internal/process/next_tick.js:131:7) at process._tickCallback (internal/process/next_tick.js:180:9) name: 'MongoError', message: 'authentication fail', errors: [ { name: 'node-rest-shop-shard-00-00-04yfe.mongodb.net:27017', err: [Object] }, { name: 'node-rest-shop-shard-00-02-04yfe.mongodb.net:27017', err: [Object] } ] } (node:69201) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoError: authentication fail (node:69201) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

here is the code block which I used to try and connect to the database

mongoose.connect('mongodb://node-iot-shop:' +
process.env.MONGO_ATLAS_PW +
'@node-rest-shop-shard-00-00-04yfe.mongodb.net:27017,node-rest-shop-shard-00-01-04yfe.mongodb.net:27017,node-rest-shop-shard-00-02-04yfe.mongodb.net:27017/test?ssl=true&replicaSet=node-rest-shop-shard-0&authSource=admin',
(err, db) => {
if(err){
console.log('Unable to connect to the server. Please start the server. Error:', err);
} else {
console.log('Connected to Server successfully!');
}
});

Versions:
node.js = v8.9.4
mongoose = [email protected]
mongoDB = v3.4.6

Can anyone please help me with solving this problem. Thank you.

Most helpful comment

It sounds like your password is incorrect. I'd recommend you do what @lineus said about printing out your environment variable, and also make sure you do encodeURIComponent(process.env.MONGO_ATLAS_PW) if you have special characters in your password

All 4 comments

@sjagadee I don't see anything wrong with your connection string.

If you console.log(process.env.MONGO_ATLAS_PW) before you try to connect, are you seeing the expected value?

If so, can you connect via mongo shell to the same replica with the same username/password?

It sounds like your password is incorrect. I'd recommend you do what @lineus said about printing out your environment variable, and also make sure you do encodeURIComponent(process.env.MONGO_ATLAS_PW) if you have special characters in your password

It sounds like your password is incorrect. I'd recommend you do what @lineus said about printing out your environment variable, and also make sure you do encodeURIComponent(process.env.MONGO_ATLAS_PW) if you have special characters in your password

Thank you man!!

I faced the same issue , what I did wrong I used mLab credentials instead of database credentials. So make sure that you are using database credentials. Furthermore, if your password contains white space, use '%' for the replacement of it.

Best of Luck :)

Was this page helpful?
0 / 5 - 0 ratings