Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongooseServerSelectionError: connect ECONNREFUSED ::1:27017

I have a problem when I try to connect my app with my database with Mongoose. Already tried following solutions that I found on google:

  • restarting MongoDB service on windows
  • manually open db with cmd located on bin file of mongodb

But I can't solve it. Can anyone help me ?

//my connection
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/notes-db-app',{
    useNewUrlParser: true, 
    useUnifiedTopology: true
})
.then(db => console.log('DB is connected'))
.catch(err => console.log(err));

And throw's me , this error

MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
    at NativeConnection.Connection.openUri (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\connection.js:797:32)
    at C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\index.js:330:10    at C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
    at new Promise (<anonymous>)
    at promiseOrCallback (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
    at Mongoose._promiseOrCallback (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\index.js:1151:10)
    at Mongoose.connect (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\index.js:329:20)
    at Object.<anonymous> (C:\Users\ivan\Desktop\NodeJS\notes-app\src\db.js:3:10)      
    at Module._compile (node:internal/modules/cjs/loader:1095:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1147:10) {      
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map(1) { 'localhost:27017' => [ServerDescription] },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    logicalSessionTimeoutMinutes: undefined
  }
}

I try to put the port on my connection code like this

//my connection
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/notes-db-app',{
    useNewUrlParser: true, 
    useUnifiedTopology: true
})
.then(db => console.log('DB is connected'))
.catch(err => console.log(err));

and it throw's me another error

MongooseServerSelectionError: Invalid message size: 1347703880, max allowed: 67108864
    at NativeConnection.Connection.openUri (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\connection.js:797:32)
    at C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\index.js:330:10    at C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
    at new Promise (<anonymous>)
    at promiseOrCallback (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
    at Mongoose._promiseOrCallback (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\index.js:1151:10)
    at Mongoose.connect (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\index.js:329:20)
    at Object.<anonymous> (C:\Users\ivan\Desktop\NodeJS\notes-app\src\db.js:3:10)      
    at Module._compile (node:internal/modules/cjs/loader:1095:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1147:10) {      
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map(1) { 'localhost:3000' => [ServerDescription] },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    logicalSessionTimeoutMinutes: undefined
  }
}
like image 651
InthraDev Avatar asked Dec 09 '25 17:12

InthraDev


1 Answers

If the Error states:

connect() Error :MongooseServerSelectionError: connect ECONNREFUSED ::1:27017

Then the connection to localhost is refused on the IPv6 address ::1 . Mongoose per default uses IPv6 ..

For a quick check you can set the IPv4 address explicit:

mongoose.connect('mongodb://127.0.0.1/test')
like image 120
Alex Avatar answered Dec 12 '25 06:12

Alex