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.
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.
suppose my username is "user" and password is password, we are supposed to write "mongodb+srv://user:[email protected]..."
, without "<" and ">";
If there are any special characters in your password, replace them with their ascii code preceded by the %
sign. For example, #
would be %35
.
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!
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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With