Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Error: cannot connect to MongoDB. Details: bad auth : authentication failed

default discord avatar
taun2160last year
6

Not sure why it's returning this auth error when I've just setup a Mongo account and double checked the details. I've added 0.0.0.0 for the ip includes too.



This is a fresh Payload project.



Any ideas?



Packages are up to date when I run 'yarn'



MONGODB_URI=mongodb+srv://taunhealy:<password>@cluster0.r1zqvrw.mongodb.net/
PAYLOAD_SECRET=4514798b4de5bf6843ac4f

I've replaced password with the correct password and I have no special characters in the password.



server.ts


import express from 'express';
import payload from 'payload';

require('dotenv').config();
const app = express();

// Redirect root to Admin panel
app.get('/', (_, res) => {
  res.redirect('/admin');
});

const start = async () => {
  // Initialize Payload
  await payload.init({
    secret: process.env.PAYLOAD_SECRET,
    mongoURL: process.env.MONGODB_URI,
    express: app,
    onInit: async () => {
      payload.logger.info(`Payload Admin URL: ${payload.getAdminURL()}`)
    },
  })

  // Add your own express routes here

  app.listen(3000);
}

start();
  • default discord avatar
    christopher.nowlanlast year

    Maybe try this after .net/



    ?retryWrites=true&w=majority

  • default discord avatar
    taun2160last year

    Thank, unfort it didn't work.



    I changed my password to have no numbers and it still returned bad auth



  • default discord avatar
    christopher.nowlanlast year

    Are you running payload locally or on a server?

  • default discord avatar
    taun2160last year

    locally



    Mongo server is online

  • default discord avatar
    christopher.nowlanlast year

    Have you added your IP address as well to the allowed IP addresses to access the db



    In network access

  • default discord avatar
    taun2160last year

    Yea



    Does anyone know of alternative connection methods to mongodb?



    I've started a new Payload project and I'm receiving a different Mongodb error:


    PS F:\git\TaunDevs\payload> yarn dev
    yarn run v1.22.19
    $ cross-env PAYLOAD_CONFIG_PATH=src/payload.config.ts nodemon
    [nodemon] 2.0.22
    [nodemon] to restart at any time, enter `rs`
    [nodemon] watching path(s): *.*
    [nodemon] watching extensions: ts
    [nodemon] starting `ts-node src/server.ts`
    [11:53:30] ERROR (payload): Error: cannot connect to MongoDB. Details: querySrv ENODATA _mongodb._tcp.taundevs.lm9ftk.mongodb.net
    [nodemon] app crashed - waiting for file changes before starting...


    I'm trying to connect to a different Mongo database that was working with Payload in the past and it's also returning an error:



     [12:03:00] ERROR (payload): Error: cannot connect to MongoDB. Details: querySrv ENODATA _mongodb._tcp.cluster0.zpesqyk.mongodb.net


    Solved temporarily by running locally and configuring the IP :


    https://www.youtube.com/watch?v=lEbp7JxQwpI&ab_channel=ErrorFix
Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.