Database authentication failed – “MongoServerError: bad auth : authentication failed”

I Could n’t connect with the MongoDB database.

I was getting this issue when I was trying to connect MongoDB atlas with my Program.

Below source code is used for server.js

const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const cors = require('cors');
const dotenv = require('dotenv');
const app = express();

const PORT = process.env.PORT || 8070;

app.use(cors());
app.use(bodyParser.json);

const URL = process.env.MONGODB_URL;

mongoose.connect("mongodb+srv://arunamongo:<password>@cluster0.0sihpzj.mongodb.net/?retryWrites=true&w=majority", {useNewUrlParser: true, useUnifiedTopology: true } )

.then(() => console.log('Connected Successfully'))

.catch((err) => { console.error(err); });


const connection = mongoose.connection;
connection.once("open", () => {
    console.log("Mongodb connection sucess!");
})

app.listen(PORT, () => {
    console.log(`Server is up and running on port ${PORT}`);
})

  • 1

    See stackoverflow.com/questions/63754742/…

    – 

Leave a Comment