MongDB: connect ECONNREFUSED 127.0.0.1:27017

Hi everyone,

I am connecting my pc with mongoDB on ubuntu server,

Everything works fine on my machine, which means when I type yarn start my node app returns as DB-Connect,

But on the server, it keeps saying connect ECONNREFUSED 127.0.0.1:27017, I have researched for more than 4 hours but still no results

Hope anyone can help me,

Thanks

this is my file env

MONGO_USERNAME = 'trungpham'
MONGO_PASSWORD = 'something'
MONGO_HOSTNAME = '127.0.0.1'
MONGO_PORT = '27017'
MONGO_DB = 'furniture'

this is my code

const url = `mongodb://${process.env.MONGO_USERNAME}:${process.env.MONGO_PASSWORD}@${process.env.MONGO_HOSTNAME}:${process.env.MONGO_PORT}/${process.env.MONGO_DB}?authSource=admin`;

mongoose.connect(url,{useNewUrlParser: true})
.then((res)=> console.log(">>>>>DB connected"))
.catch((err)=> console.error("Connect fail", err));

this is log on server

@Trung_Ph_m_D_c Hey!

That configuration would be for a local instance of the mongod server.

For connecting you need to make sure it has started, and that it is waiting for tcp connections at the right port.

Did you start the mongo process? If you can double check it is running (in linux it would be systemctl status mongod.service or maybe ps -ef [Mm]ongo or also you can show the configuration file.

If this makes no sense at all, you may need to quickly look up how to start the server.


Allleeerrrrttt

By the way I suspect this file

MONGO_USERNAME = 'trungpham'
MONGO_PASSWORD = 'something'
MONGO_HOSTNAME = '127.0.0.1'
MONGO_PORT = '27017'
MONGO_DB = 'furniture'

does not like quotes ' nor it likes spaces around the = sign. You can console.log() your current connection string, or the variables to double check they are correct!

Hi @santimir ,

Thank you for reply

I checked my mongoDB, and it’s working fine

as to file env,

This is log my connection string, all of them have no spaces or trailing quote

mongodb://trungpham:something@127.0.0.1:27017/furniture?authSource=admin
Connect fail MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
    at NativeConnection.Connection.openUri (/Users/ductrung/Documents/ec2Decord/node_modules/mongoose/lib/connection.js:797:32)
    at /Users/ductrung/Documents/ec2Decord/node_modules/mongoose/lib/index.js:341:10
    at /Users/ductrung/Documents/ec2Decord/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
    at new Promise (<anonymous>)
    at promiseOrCallback (/Users/ductrung/Documents/ec2Decord/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
    at Mongoose._promiseOrCallback (/Users/ductrung/Documents/ec2Decord/node_modules/mongoose/lib/index.js:1167:10)
    at Mongoose.connect (/Users/ductrung/Documents/ec2Decord/node_modules/mongoose/lib/index.js:340:20)
    at Object.<anonymous> (/Users/ductrung/Documents/ec2Decord/app.js:19:10)
    at Module._compile (node:internal/modules/cjs/loader:1102:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1131:10) {
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map(1) { '127.0.0.1:27017' => [ServerDescription] },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    logicalSessionTimeoutMinutes: undefined
  }
}

@Trung_Ph_m_D_c,

Thank you for showing the data.

To remove Mongoose from the picture, can you try to connect from the mongo shell, and see what happens?

You probably know this, but It would be mongo mongodb://trungpham:something@127.0.0.1:27017/furniture?authSource=admin in the terminal.

The error ECONNREFUSED means no mongod is listening at the given address on the given port.

From your screenshot we can see that you have a mongod running on your system. We need to see the parameters used to start it.

  1. Share the configuration file
  2. Share the output of the commands
ss -tlnp
ps -aef | grep [m]ongod

If ss does not exists on you system, try netstat with the same parameters.

1 Like

I thought ss would only list sockets with at least one connection, because mongod woudn’t sometimes show up. But have now tested it and it shows up.

Thanks for that one, have a good day.

If you look at the man page, you will see:

-l or –listening

    Display only listening sockets (these are omitted by default).

1 Like