'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.
Solution 1:[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.
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
.
Solution 2:[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!
Solution 3:[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
Solution 4:[4]
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.
Solution 5:[5]
Also try it as,
'CLIENT' : { 'host' : 'connection_string_to_mongodb' }
since, connection_string has username, password, database name, clustername and so, just have this one key-value pair in the client. It worked for me.
Solution 6:[6]
Also make sure your password does not have any numbers or special characters
Solution 7:[7]
Might as well try to add your IP to the white list. Go to Network Access -> Add IP Adress -> Current IP, then save changes and try again, hope it'll help.
Solution 8:[8]
making a new admin user account worked for me. i found this was not only case with pymongo but also when I was working with mongo shell.
Solution 9:[9]
For some reason when I avoid copy / paste and type in the username and password it works.
Solution 10:[10]
the solution is to remove brackets <> from the user name and password. enter the correct database name. Give access to your database so that the data is posted into your database.
Solution 11:[11]
The solution for this is make sure you username and password should include '_' between name and number if in case your name or password includes number
Solution 12:[12]
Use this
const config = {
mongoConnectionURL: "mongodb://cyruslk:[email protected]: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",
}
NOTE:
remove '<' & '>' from the username and password and make sure that the username and password lies just before and after the colon (symbol = ":" ) there should be no empty space before and after the colon
Solution 13:[13]
This is Not Always Only About The Password
Though your error already answered by some people which was a password issue. However, looking at the comments and answer here, I see a lot of people having similar error but unable to solve with the provided answer. But why ?This error is not only about the password. Here Database Name, Password and Username do matter.
Since Database name, Password and Username are part of the url, therefore if you use any special character in any of these three part of your url, then you need to replace them with %
and then ASCII Code
of those special sign. For example: for $-
sign you need to replace it with %36%45
And obviously remove <
and >
sign also from the initial link you copied which is like <password>
.
See ASCII Code Here
See the image to understand more
Solution 14:[14]
For me, I solved this by creating another admin user. I forgot that I have a deployed web app that is already using an admin user. So creating another admin user and use it as a Mongo URI on a different web app helped solve me this authentication issue
Solution 15:[15]
I faced the same problem and found the solution. but changed the database password and it works. [try to keep the auto-generated password]
Solution 16:[16]
pip install djongo
pip install dnspython
DATABASES = {
'default': {
'ENGINE': 'djongo',
"NAME": "database name",
"CLIENT": {
"host": "connection_string_to_mongodb",
},
}
}
the above code worked for me. separately providing username and password didn't work
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow