I have logged in into MongoDB Atlas online with my google account. I am quite sure that I know the password for my google account.
I try to connect to a database with NodeJS like this
/*
mongodb+srv://imelflorianingerl:<db_password>@cluster0.s8sclhk.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0
Replace <db_password> with the password for the imelflorianingerl database user. Ensure any option params are
URL encoded */
import { Schema, model, connect } from ‘mongoose’;
// 1. Create an interface representing a document in MongoDB.
interface IUser {
name: string;
email: string;
avatar?: string;
}
// 2. Create a Schema corresponding to the document interface.
const userSchema = new Schema({
name: { type: String, required: true },
email: { type: String, required: true },
avatar: String
});
// 3. Create a Model.
const User = model(‘User’, userSchema);
run().catch(err => console.log(err));
async function run() {
// 4. Connect to MongoDB
await connect(‘mongodb+srv://imelflorianingerl:my_password@cluster0.s8sclhk.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0’);
const user = new User({
name: ‘Bill’,
email: ‘bill@initech.com’,
avatar: ‘Imgur: The magic of the Internet’
});
await user.save();
console.log(user.email); // ‘bill@initech.com’
}
I always get this error:
MongoServerError: bad auth : authentication failed
at Connection.sendCommand (C:\Users\imelf\Documents\floriloveslanguagesserver\node_modules\mongodb\src\cmap\connection.ts:525:17)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Connection.command (C:\Users\imelf\Documents\floriloveslanguagesserver\node_modules\mongodb\src\cmap\connection.ts:597:22)
at async continueScramConversation (C:\Users\imelf\Documents\floriloveslanguagesserver\node_modules\mongodb\src\cmap\auth\scram.ts:187:13)
at async executeScram (C:\Users\imelf\Documents\floriloveslanguagesserver\node_modules\mongodb\src\cmap\auth\scram.ts:114:3)
at async ScramSHA1.auth (C:\Users\imelf\Documents\floriloveslanguagesserver\node_modules\mongodb\src\cmap\auth\scram.ts:60:12)
at async performInitialHandshake (C:\Users\imelf\Documents\floriloveslanguagesserver\node_modules\mongodb\src\cmap\connect.ts:163:7)
at async connect (C:\Users\imelf\Documents\floriloveslanguagesserver\node_modules\mongodb\src\cmap\connect.ts:43:5) {
errorResponse: {
ok: 0,
errmsg: ‘bad auth : authentication failed’,
code: 8000,
codeName: ‘AtlasError’
},
ok: 0,
code: 8000,
codeName: ‘AtlasError’,
connectionGeneration: 0,
[Symbol(errorLabels)]: Set(2) { ‘HandshakeError’, ‘ResetPool’ }
}
So somehow my password is wrong. How do I find out which password I should use?
When I copied the connection string, it said also:
/*
mongodb+srv://imelflorianingerl:<db_password>@cluster0.s8sclhk.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0
Replace <db_password> with the password for the imelflorianingerl database user. Ensure any option params are
URL encoded */
I would also be happy to create a completely new user whose password I can set and connect to my database with that user. Thanks a lot for any help.