Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 795
cyruslk Avatar asked Feb 01 '20 19:02

cyruslk


5 Answers

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.

like image 182
Nikhil Thorat Avatar answered Oct 18 '22 18:10

Nikhil Thorat


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

like image 20
IndustryDesigns Avatar answered Oct 18 '22 18:10

IndustryDesigns


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

like image 6
Daniya Niazi Avatar answered Oct 18 '22 19:10

Daniya Niazi


First, the error MongoServerError: bad auth: Authentication failed indicates username and password may be incorrect.

Solution: In credential.js remove the brackets <> around username and password then you have a database connection.

like image 1
Ken Mwangi Avatar answered Oct 18 '22 18:10

Ken Mwangi


After lots of headache searching for the answer, I realize that my in the connection stream still had the <> around the password. Make sure to remove this.

like image 1
JavaEddie Avatar answered Oct 18 '22 18:10

JavaEddie