Mongo Connection Error in Node JS

Hi There, i hope every one is fine
While connecting to mongo db(local), am getting this error
error MongooseServerSelectionError: connect ECONNREFUSED ::1:27017

Even i checked in services Mongodb is in runnning state and i restarted as well.

My Code is
mongoose.connect(“mongodb://localhost:27017/database_name”,{useNewUrlParser:true, useUnifiedTopology:true}).then((result) =>{

console.log(‘Server Connected’)

}).catch(err => console.log(‘error’,err))

This has been answered many times. Search for ECONNREFUSED.

One of 2 reasons:

  1. mongod is not listening at the given address ::1:26017, which is IPv6 localhost. mongod might be not running at all or might be listening to IPv6 only
  2. you want to connect to IPv4 localhost but your localhost is defined as IPv6

I also read somewhere that some lib were sorting the IP address coming from DNS resolution. This lib might not doing that anymore. If localhost IPv6 is defined as the first one, the connection is tried with this and the IPv4 is not tried. Before, IPv4 was be returned first.

If you want to make sure to connnect to IPv4 localhost defined as use rather that localhost.