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

22,014

Solution 1

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.

Solution 2

I just had this problem knowing that I was using the correct username, password and DBname.

I tried to change the password for the db user to double check, but it still didn't work.

I then instead created a new user, gave the user admin role, set the password etc, and then used that new user and password (same dbname) for the connection and it worked.

I don't know exactly what the issue was, but hoping it can help some of you save some time :)

Good luck!

enter image description here

Solution 3

I faced the same problem I have changed the password but it didn't work. My Password includes numbers I removed them and it worked

Share:
22,014
cyruslk
Author by

cyruslk

Updated on February 20, 2022

Comments

  • cyruslk
    cyruslk about 2 years

    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.

  • cyruslk
    cyruslk over 3 years
    Thank you, fixed it in the meantime but it took me a while to understand.
  • Shrikant Prabhu
    Shrikant Prabhu over 3 years
    You need to url encode it using urllib.parse.quote("[email protected]") and urllib.parse.quote("p@ssword"). Check this answer here stackoverflow.com/a/39283299/337666
  • Lohith
    Lohith over 3 years
    Not sure the problem with the default user, but creating new user worked for me as well.
  • hugoncosta
    hugoncosta about 3 years
    Not at all clear why, but this solved it.
  • Arijit Das
    Arijit Das almost 3 years
    you can change the password from the Database Access sections of MongoDB. and try to keep auto-generated password.
  • Gibron
    Gibron over 2 years
    Piling on here that this solved my instance of this issue. Alas, it is unsatisfying as I would like to understand why. If anyone has insight please share. 🙏
  • jatin grover
    jatin grover almost 2 years
    Thank you. I was frustrated after trying for hours.. 🙏