Mongodb + Atlas: 'bad auth Authentication failed.', code: 8000,

cyruslk picture cyruslk · Feb 1, 2020 · Viewed 8.7k times · Source

I've tried this and it's not working.

I have the following error with Mongodb + Atlas:

MongoError: bad auth Authentication failed.
    at /Users/cyrus/Documents/Code/01. Code/Franklin-ford/franklin-ford-server/node_modules/mongodb-core/lib/auth/auth_provider.js:46:25
    at /Users/cyrus/Documents/Code/01. Code/Franklin-ford/franklin-ford-server/node_modules/mongodb-core/lib/auth/scram.js:215:18
    at Connection.messageHandler (/Users/cyrus/Documents/Code/01. Code/Franklin-ford/franklin-ford-server/node_modules/mongodb-core/lib/connection/connect.js:334:5)
    at Connection.emit (events.js:203:13)
    at processMessage (/Users/cyrus/Documents/Code/01. Code/Franklin-ford/franklin-ford-server/node_modules/mongodb-core/lib/connection/connection.js:364:10)
    at TLSSocket.<anonymous> (/Users/cyrus/Documents/Code/01. Code/Franklin-ford/franklin-ford-server/node_modules/mongodb-core/lib/connection/connection.js:533:15)
    at TLSSocket.emit (events.js:203:13)
    at addChunk (_stream_readable.js:295:12)
    at readableAddChunk (_stream_readable.js:276:11)
    at TLSSocket.Readable.push (_stream_readable.js:210:10)
    at TLSWrap.onStreamRead (internal/stream_base_commons.js:166:17) {
  ok: 0,
  errmsg: 'bad auth Authentication failed.',
  code: 8000,
  codeName: 'AtlasError',
  name: 'MongoError',
  [Symbol(mongoErrorContextSymbol)]: {}

Here's how I instantiate my DB:

From the credentials.js:

const config = {
    mongoConnectionURL: "mongodb://cyruslk:<MY_PASS_HERE>@franklinford-shard-00-00-3dveb.mongodb.net:27017,franklinford-shard-00-01-3dveb.mongodb.net:27017,franklinford-shard-00-02-3dveb.mongodb.net:27017/test?ssl=true&replicaSet=FranklinFord-shard-0&authSource=admin&retryWrites=true&w=majority",
    mongoDatabaseName: "FranklinFord",
  }

From my main server.js app:

const {MongoClient} = require("mongodb");
const connectionURL = config.mongoConnectionURL;
const databaseName = config.mongoDatabaseName;


  let sendToDb = (dataToInsert) => {
    MongoClient.connect(connectionURL, {
      useNewUrlParser: true,
    }, (error, client) => {

      if(error){
        console.log(error);
        return console.log("Unable to connect to the db.");
      }
      const db = client.db(databaseName);
      console.log(db);

      db.collection("ford_twitter").insertOne({
        masterData: dataToInsert
      }, (error, result) => {
        if(error){
          return console.log("unable to insert users");
        }
        console.log("Data successfully inserted");
      })
    })
  }

You can find the code here

Thanks for helping me. I have no idea what's happening.

Answer

Nikhil Thorat picture Nikhil Thorat · Nov 15, 2020

I had same problem, in that connection string it was written "mongodb+srv://<username>:<password>@cluster0.4h226.mongodb.n..." and you are supposed to enter username and password in that string.

  1. suppose my username is "user" and password is password, we are supposed to write "mongodb+srv://user:[email protected]...", without "<" and ">";

  2. If there are any special characters in your password, replace them with their ascii code preceded by the % sign. For example, # would be %35.