Hi I am in arch based garuda linux and I am facing MongooseServerSelectionError: connect ECONNREFUSED ::1:27017 error while connecting my node app to mongodb.
The error is
MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
at NativeConnection.Connection.openUri (/home/linux/Desktop/mongo tutorial/node_modules/mongoose/lib/connection.js:797:32)
at /home/linux/Desktop/mongo tutorial/node_modules/mongoose/lib/index.js:341:10
at /home/linux/Desktop/mongo tutorial/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
at new Promise (<anonymous>)
at promiseOrCallback (/home/linux/Desktop/mongo tutorial/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (/home/linux/Desktop/mongo tutorial/node_modules/mongoose/lib/index.js:1167:10)
at Mongoose.connect (/home/linux/Desktop/mongo tutorial/node_modules/mongoose/lib/index.js:340:20)
at Object.<anonymous> (/home/linux/Desktop/mongo tutorial/index.js:2:10)
at Module._compile (node:internal/modules/cjs/loader:1097:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10) { reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) {
'localhost:27017' => ServerDescription {
_hostAddress: HostAddress { isIPv6: false, host: 'localhost', port: 27017 },
address: 'localhost:27017',
type: 'Unknown',
hosts: [],
passives: [],
arbiters: [],
tags: {},
minWireVersion: 0,
maxWireVersion: 0,
roundTripTime: -1,
lastUpdateTime: 19125836,
lastWriteDate: 0,
error: MongoNetworkError: connect ECONNREFUSED ::1:27017
at connectionFailureError (/home/linux/Desktop/mongo tutorial/node_modules/mongodb/lib/cmap/connect.js:293:20)
at Socket.<anonymous> (/home/linux/Desktop/mongo tutorial/node_modules/mongodb/lib/cmap/connect.js:267:22)
at Object.onceWrapper (node:events:510:26)
at Socket.emit (node:events:390:28)
at emitErrorNT (node:internal/streams/destroy:164:8)
at emitErrorCloseNT (node:internal/streams/destroy:129:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
}
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
logicalSessionTimeoutMinutes: undefined } }
Node.js v17.3.0
My mongodb compass screenschot
This is working on win 10.
mongoose.connect("mongodb://0.0.0.0/test-db");
The problem was not with the code it was the version.
Instead of using
mongoose.connect("mongodb://localhost/test")
I used
mongoose.connect("mongodb://127.0.0.1/test")
it worked.
reference: https://www.mongodb.com/community/forums/t/mongooseserverselectionerror-connect-econnrefused-127-0-0-1-27017/123421
Related
Hello I am new to MongoDB. I am trying to Connect to MongoDb with 'mongodb://localhost:27017' but not connecting. However I try to enter with MongoDb Compass and mongosh shell command it is working.
The error codes are
node app.js
node:internal/process/promises:288
triggerUncaughtException(err, true /* fromPromise */);
^
AssertionError [ERR_ASSERTION]: null == MongoServerSelectionError: connect ECONNREFUSED ::1:27017
at Timeout._onTimeout (C:\Users\kokophone\Documents\MEGAsync\Mystudy\Web\Udemy\Angela Bootcamp\Mgphone One\MangoDB\FruitProject\node_modules\mongodb\lib\sdam\topology.js:292:38)
at listOnTimeout (node:internal/timers:564:17)
at process.processTimers (node:internal/timers:507:7) {
[Symbol(errorLabels)]: Set(0) {},
code: undefined,
reason: TopologyDescription {
commonWireVersion: 0,
compatible: true,
heartbeatFrequencyM...
at C:\Users\kokophone\Documents\MEGAsync\Mystudy\Web\Udemy\Angela Bootcamp\Mgphone One\MangoDB\FruitProject\app.js:15:10
at C:\Users\kokophone\Documents\MEGAsync\Mystudy\Web\Udemy\Angela Bootcamp\Mgphone One\MangoDB\FruitProject\node_modules\mongodb\lib\utils.js:349:66 {
generatedMessage: true,
code: 'ERR_ASSERTION',
actual: null,
expected: MongoServerSelectionError: connect ECONNREFUSED ::1:27017
at Timeout._onTimeout (C:\Users\kokophone\Documents\MEGAsync\Mystudy\Web\Udemy\Angela Bootcamp\Mgphone One\MangoDB\FruitProject\node_modules\mongodb\lib\sdam\topology.js:292:38)
at listOnTimeout (node:internal/timers:564:17)
at process.processTimers (node:internal/timers:507:7) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) {
'localhost:27017' => ServerDescription {
address: 'localhost:27017',
type: 'Unknown',
hosts: [],
passives: [],
arbiters: [],
tags: {},
minWireVersion: 0,
maxWireVersion: 0,
roundTripTime: -1,
lastUpdateTime: 2313988,
lastWriteDate: 0,
error: MongoNetworkError: connect ECONNREFUSED ::1:27017
at connectionFailureError (C:\Users\kokophone\Documents\MEGAsync\Mystudy\Web\Udemy\Angela Bootcamp\Mgphone One\MangoDB\FruitProject\node_modules\mongodb\lib\cmap\connect.js:387:20)
at Socket.<anonymous> (C:\Users\kokophone\Documents\MEGAsync\Mystudy\Web\Udemy\Angela Bootcamp\Mgphone One\MangoDB\FruitProject\node_modules\mongodb\lib\cmap\connect.js:310:22)
at Object.onceWrapper (node:events:628:26)
at Socket.emit (node:events:513:28)
at emitErrorNT (node:internal/streams/destroy:151:8)
at emitErrorCloseNT (node:internal/streams/destroy:116:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
cause: [Error],
[Symbol(errorLabels)]: [Set]
},
topologyVersion: null,
setName: null,
setVersion: null,
electionId: null,
logicalSessionTimeoutMinutes: null,
primary: null,
me: null,
'$clusterTime': null
}
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: null,
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
},
code: undefined,
[Symbol(errorLabels)]: Set(0) {}
},
operator: '=='
}
Node.js v18.12.1
please help me out.
Change localhost by 0.0.0.0. in the source code .
const url = "mongodb://localhost:27017/";
const client = new MongoClient(url);
to
const url = "mongodb://0.0.0.0:27017/";
const client = new MongoClient(url);
I downloaded MongoDB with Compass and the shell as accessories and started learning from W3Schools Node.js MongoDB section.
Step 1: type into command prompt>npm install mongodb
success
step 2: create a MongoClient object file with this code and then save
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydb";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
console.log("Database created!");
db.close();
});
Step 3: Once the code above is saved run the file by calling it on the command prompt
C:\Users\MyName>node fileName.js
and this is the response I'm getting
C:\Users\*****\MongoDB Tutorials>node demo_create_mongo_db.js
C:\Users\*****\node_modules\mongodb\lib\utils.js:418
throw error;
^
MongoServerSelectionError: connect ECONNREFUSED ::1:27017
at Timeout._onTimeout (C:\Users\*****\node_modules\mongodb\lib\sdam\topology.js:293:38)
at listOnTimeout (node:internal/timers:557:17)
at processTimers (node:internal/timers:500:7) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) {
'localhost:27017' => ServerDescription {
_hostAddress: HostAddress { isIPv6: false, host: 'localhost', port: 27017 },
address: 'localhost:27017',
type: 'Unknown',
hosts: [],
passives: [],
arbiters: [],
tags: {},
minWireVersion: 0,
maxWireVersion: 0,
roundTripTime: -1,
lastUpdateTime: 11442648,
lastWriteDate: 0,
error: MongoNetworkError: connect ECONNREFUSED ::1:27017
at connectionFailureError (C:\Users\*****\node_modules\mongodb\lib\cmap\connect.js:379:20)
at Socket.<anonymous> (C:\Users\*****\node_modules\mongodb\lib\cmap\connect.js:302:22)
at Object.onceWrapper (node:events:510:26)
at Socket.emit (node:events:390:28)
at emitErrorNT (node:internal/streams/destroy:164:8)
at emitErrorCloseNT (node:internal/streams/destroy:129:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
[Symbol(errorLabels)]: Set(0) {}
}
}
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
logicalSessionTimeoutMinutes: undefined
},
code: undefined,
[Symbol(errorLabels)]: Set(0) {}
}
Node.js v17.2.0
What do I do about this?
when I run it alone or together, there are no errors in my frontend and backend parts and they work as I want.and now I want to use docker, but when I try a error appears. I tried to solve with a previous solution that wrote before, but doesn't work., I create my Docker file and I type "docker build ." and this works
then
I type docker run Image Id
Error
PS D:\javascript_projects\nimble_dersleri\backend> docker run 880a0a3e9b21
> ders-6#1.0.0 start
> node .bin/www
(node:18) [DEP0128] DeprecationWarning: Invalid 'main' field in '/package.json' of 'service.js'. Please either fix that or report it to the module author
(Use `node --trace-deprecation ...` to show where the warning was created)
started listening on 3000
connection error MongooseServerSelectionError: getaddrinfo EAI_AGAIN db
at Connection.openUri (/node_modules/mongoose/lib/connection.js:819:32)
at /node_modules/mongoose/lib/index.js:378:10
at /node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
at new Promise (<anonymous>)
at promiseOrCallback (/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (/node_modules/mongoose/lib/index.js:1223:10)
at Mongoose.connect (/node_modules/mongoose/lib/index.js:377:20)
at Object.<anonymous> (/routes/mongo-connection.js:7:10)
at Module._compile (node:internal/modules/cjs/loader:1120:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1174:10) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) { 'db:27017' => [ServerDescription] },
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
logicalSessionTimeoutMinutes: undefined
},
code: undefined
}
node:internal/process/promises:288
triggerUncaughtException(err, true /* fromPromise */);
^
MongooseServerSelectionError: getaddrinfo EAI_AGAIN db
at Connection.openUri (/node_modules/mongoose/lib/connection.js:819:32)
at /node_modules/mongoose/lib/index.js:378:10
at /node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
at new Promise (<anonymous>)
at promiseOrCallback (/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (/node_modules/mongoose/lib/index.js:1223:10)
at Mongoose.connect (/node_modules/mongoose/lib/index.js:377:20)
at Object.<anonymous> (/routes/mongo-connection.js:7:10)
at Module._compile (node:internal/modules/cjs/loader:1120:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1174:10) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) {
'db:27017' => ServerDescription {
address: 'db:27017',
passives: [],
arbiters: [],
tags: {},
minWireVersion: 0,
maxWireVersion: 0,
roundTripTime: -1,
lastUpdateTime: 1358575,
lastWriteDate: 0,
error: MongoNetworkError: getaddrinfo EAI_AGAIN db
at connectionFailureError (/node_modules/mongodb/lib/cmap/connect.js:382:20)
at Socket.<anonymous> (/node_modules/mongodb/lib/cmap/connect.js:302:22)
at Object.onceWrapper (node:events:628:26)
at Socket.emit (node:events:513:28)
at emitErrorNT (node:internal/streams/destroy:151:8)
at emitErrorCloseNT (node:internal/streams/destroy:116:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
[Symbol(errorLabels)]: Set(0) {}
}
}
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
logicalSessionTimeoutMinutes: undefined
},
code: undefined
}
Node.js v18.6.0
mongo-connection
const mongoose = require('mongoose');
var db = mongoose.connection
mongoose.connect('mongodb://db:27017/week6');
db.on('error', console.error.bind(console,'connection error'))
db.once('open',function(){
console.log('we are connected to mongodb!')
})
Dockerfile
FROM node:18.6-alpine3.15
COPY . .
RUN npm install
CMD ["npm", "start"]
Hi I tried every thing like mongoose.connect('mongodb://localhost/blog') but I am not able to connect mongoose to node here is my code....
const express = require('express')
const mongoose = require('mongoose')
const articleRouter = require('./routes/articles')
const app = express()
mongoose.connect('mongodb://localhost:27017/blog')
app.set('view engine','ejs')
Here is the error:
/home/vishwajeet/webdeve/MARKDOWN-BLOG/node_modules/mongoose/lib/connection.js:807
const serverSelectionError = new ServerSelectionError();
^
MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
at NativeConnection.Connection.openUri (/home/vishwajeet/webdeve/MARKDOWN-BLOG/node_modules/mongoose/lib/connection.js:807:32)
at /home/vishwajeet/webdeve/MARKDOWN-BLOG/node_modules/mongoose/lib/index.js:342:10
at /home/vishwajeet/webdeve/MARKDOWN-BLOG/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
at new Promise (<anonymous>)
at promiseOrCallback (/home/vishwajeet/webdeve/MARKDOWN-BLOG/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (/home/vishwajeet/webdeve/MARKDOWN-BLOG/node_modules/mongoose/lib/index.js:1181:10)
at Mongoose.connect (/home/vishwajeet/webdeve/MARKDOWN-BLOG/node_modules/mongoose/lib/index.js:341:20)
at Object.<anonymous> (/home/vishwajeet/webdeve/MARKDOWN-BLOG/server.js:8:10)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) {
'localhost:27017' => ServerDescription {
_hostAddress: HostAddress { isIPv6: false, host: 'localhost', port: 27017 },
address: 'localhost:27017',
type: 'Unknown',
hosts: [],
passives: [],
arbiters: [],
tags: {},
minWireVersion: 0,
maxWireVersion: 0,
roundTripTime: -1,
lastUpdateTime: 774709,
lastWriteDate: 0,
error: MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017
at connectionFailureError (/home/vishwajeet/webdeve/MARKDOWN-BLOG/node_modules/mongodb/lib/cmap/connect.js:375:20)
at Socket.<anonymous> (/home/vishwajeet/webdeve/MARKDOWN-BLOG/node_modules/mongodb/lib/cmap/connect.js:295:22)
at Object.onceWrapper (node:events:646:26)
at Socket.emit (node:events:526:28)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
[Symbol(errorLabels)]: Set(0) {}
}
}
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
logicalSessionTimeoutMinutes: undefined
},
code: undefined
}
Hey I think your problem is that you don't have mongodb installed on your computer.
Once you download it you can change
mongoose.connect('mongodb://localhost:27017/blog')
to
mongoose.connect('mongodb://127.0.0.1/blog')
if the problem is still there
Youtube video for mongodb install
const mongoose = require('mongoose');
main().catch(err => console.log(err));
async function main() {
await mongoose.connect('mongodb://localhost:27017/test');
}
error
MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
at NativeConnection.Connection.openUri (E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\connection.js:807:32) at E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\index.js:340:10
at E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
at new Promise (<anonymous>)
at promiseOrCallback (E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\index.js:1140:10)
at Mongoose.connect (E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\index.js:339:20)
at Object.<anonymous> (E:\Web Dev\Backend\New folder\index.js:4:10)
at Module._compile (node:internal/modules/cjs/loader:1097:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) {
'localhost:27017' => ServerDescription {
_hostAddress: HostAddress { isIPv6: false, host: 'localhost', port: 27017 },
address: 'localhost:27017',
type: 'Unknown',
hosts: [],
passives: [],
arbiters: [],
tags: {},
minWireVersion: 0,
maxWireVersion: 0,
roundTripTime: -1,
lastUpdateTime: 194037884,
lastWriteDate: 0,
error: MongoNetworkError: connect ECONNREFUSED ::1:27017
at connectionFailureError (E:\Web Dev\Backend\New folder\node_modules\mongodb\lib\cmap\connect.js:381:20)
at Socket. (E:\Web Dev\Backend\New folder\node_modules\mongodb\lib\cmap\connect.js:301:22)
at Object.onceWrapper (node:events:640:26)
at Socket.emit (node:events:520:28)
at emitErrorNT (node:internal/streams/destroy:164:8)
at emitErrorCloseNT (node:internal/streams/destroy:129:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
}
},
stale: false,
compatible: true,
}
}
Maybe this does the trick:
async function main () {
const connectionOptions = {
useNewUrlParser: true,
useUnifiedTopology: true
}
try {
await moongose.connect(/*Your URL*/,connectionOptions)
console.log(`Connected to MongoDB`)
} catch (err) {
console.log(`Couldn't connect: ${err}`)
}
}