MongoDB connection error

I am having an error in my node server. I am using mongoose as an ORM with node.js and I am trying to connect to MongoDB Atlas and at first I am able to connect to my cluster but after some seconds I am getting this error in my console as,

Shutting down... Error: Error connecting to db: connect ECONNREFUSED ::1:27017

I am also able to connect to my cluster with Compass. I am really getting confused why I am getting this error as this has nothing to do with my localhost MongoDB sever but still I have enabled my server at localhost too just to check whether it can solve anything or not but really it didn’t :frowning_face:

For more specifications:
Node js version: v18.12.1
MongoDB version: MongoDB shell version v4.4.18
Mongoose version: ^5.13.14,
OS: Pop_os(22.04)

here is my code of my server.js where I am trying to connect to my MongoDB cluster.

const dotenv = require('dotenv');
const mongoose = require('mongoose');
const server = require('./app');

process.on('uncaughtException', (err) => {
    console.log('UNCAUGHT EXCEPTION! Shutting down...', err);
    process.exit(1);
});

process.on('unhandledRejection', (err) => {
    console.log('UNHANDLED REJECTION! Shutting down...', err);
    process.exit(1);
});

dotenv.config({path: './config.env'});
const PORT = process.env.PORT || 5000;

const DB_PASS = process.env.DB_PASSWORD;
const DB_URL = process.env.DB_URL.replace('<password>', DB_PASS);

mongoose
  .connect(DB_URL, {
    useNewUrlParser: true,
    useCreateIndex: true,
    useFindAndModify: false,
    useUnifiedTopology: true
  })
  .then(() => console.log('Database sucessfully connected'));

 exports.sessionUrl = DB_URL; 

server.listen(PORT, () => {
    console.log(`Server running in ${process.env.NODE_ENV} mode on port ${PORT}`);
});

1 Like

It is one of the reason already mentioned in this forum.

See Search results for 'ECONNREFUSED IPv6' - MongoDB Developer Community Forums

But I am trying to connect to my remote cluster what do IPv6 and this error have to do with that can you brief me on this?

The error you are showing us seems to come from a local server, can you make sure that:

  • That the address you connect to (in your .env file) is really the remote server address and not the local
  • The error comes from the JS code you included and not from another connection.

Or just add more information. We will help you out.

If everything is correct the error could still be mongoosejs.

1 Like