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

default discord avatar
taun2160
3 months ago
17

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.nowlan
    3 months ago

    Maybe try this after .net/



    ?retryWrites=true&w=majority

  • default discord avatar
    taun2160
    3 months ago

    Thank, unfort it didn't work.



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



    image.png
  • default discord avatar
    christopher.nowlan
    3 months ago

    Are you running payload locally or on a server?

  • default discord avatar
    taun2160
    3 months ago

    locally



    Mongo server is online

  • default discord avatar
    christopher.nowlan
    3 months ago

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



    In network access

  • default discord avatar
    taun2160
    3 months ago

    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
Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.