Issue
Hi friends!
I'm trying to connect node.js project with mongodb, I started with mongodb atlas and it worked fine but when I tried with mongodb compass nothing worked! I don't know what is happening but I'll post my node.js code with the terminal and mongod result on CMD and compass capture
Node.JS code
const joi = require('joi')
const express = require('express')
const app = express()
const port = 3000
const mongoose = require('mongoose');
const { string } = require('joi');
const dataBaseLink = `mongodb://localhost:27017/myapp`;
mongoose.connect(dataBaseLink, {useNewUrlParser: true})
.then(result =>{
app.listen(port, () => {
console.log('Connected Successfully');
console.log(`Example app listening on port ${port}`)
})})
.catch(err => {
console.error("Connection problem, please try again!.");});
terminal result
Windows PowerShell
Copyright (C) 2014 Microsoft Corporation. All rights reserved.
PS E:\Folder\Mine\node projects\myapp> node app
Connection problem, please try again!.
PS E:\MyBackUp\Mine\node projects\myapp>
mongodb compass capture
mongod CMD result
Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.
C:\Users\Mazen>mongod
2022-11-08T21:30:22.135+0200 I CONTROL [initandlisten] MongoDB starting : pid=2
816 port=27017 dbpath=C:\data\db\ 64-bit host=MazenOthman
2022-11-08T21:30:22.136+0200 I CONTROL [initandlisten] targetMinOS: Windows 7/W
indows Server 2008 R2
2022-11-08T21:30:22.136+0200 I CONTROL [initandlisten] db version v3.6.23
2022-11-08T21:30:22.137+0200 I CONTROL [initandlisten] git version: d352e6a4764
659e0d0350ce77279de3c1f243e5c
2022-11-08T21:30:22.137+0200 I CONTROL [initandlisten] OpenSSL version: OpenSSL
1.0.2s-fips 28 May 2019
2022-11-08T21:30:22.137+0200 I CONTROL [initandlisten] allocator: tcmalloc
2022-11-08T21:30:22.137+0200 I CONTROL [initandlisten] modules: none
2022-11-08T21:30:22.137+0200 I CONTROL [initandlisten] build environment:
2022-11-08T21:30:22.137+0200 I CONTROL [initandlisten] distmod: 2008plus-ss
l
2022-11-08T21:30:22.137+0200 I CONTROL [initandlisten] distarch: x86_64
2022-11-08T21:30:22.137+0200 I CONTROL [initandlisten] target_arch: x86_64
2022-11-08T21:30:22.137+0200 I CONTROL [initandlisten] options: {}
2022-11-08T21:30:22.140+0200 I - [initandlisten] Detected data files in C
:\data\db\ created by the 'wiredTiger' storage engine, so setting the active sto
rage engine to 'wiredTiger'.
2022-11-08T21:30:22.140+0200 I STORAGE [initandlisten] wiredtiger_open config:
create,cache_size=1433M,cache_overflow=(file_max=0M),session_max=20000,eviction=
(threads_min=4,threads_max=4),config_base=false,statistics=(fast),compatibility=
(release="3.0",require_max="3.0"),log=(enabled=true,archive=true,path=journal,co
mpressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),v
erbose=(recovery_progress),
2022-11-08T21:30:22.701+0200 I STORAGE [initandlisten] WiredTiger message [1667
935822:701436][2816:140713401127040], txn-recover: Main recovery loop: starting
at 4/12800
2022-11-08T21:30:22.982+0200 I STORAGE [initandlisten] WiredTiger message [1667
935822:981680][2816:140713401127040], txn-recover: Recovering log 4 through 5
2022-11-08T21:30:23.197+0200 I STORAGE [initandlisten] WiredTiger message [1667
935823:196874][2816:140713401127040], txn-recover: Recovering log 5 through 5
2022-11-08T21:30:23.341+0200 I STORAGE [initandlisten] WiredTiger message [1667
935823:341005][2816:140713401127040], txn-recover: Set global recovery timestamp
: 0
2022-11-08T21:30:23.667+0200 I CONTROL [initandlisten]
2022-11-08T21:30:23.667+0200 I CONTROL [initandlisten] ** WARNING: Access contr
ol is not enabled for the database.
2022-11-08T21:30:23.668+0200 I CONTROL [initandlisten] ** Read and wri
te access to data and configuration is unrestricted.
2022-11-08T21:30:23.669+0200 I CONTROL [initandlisten]
2022-11-08T21:30:23.670+0200 I CONTROL [initandlisten] ** WARNING: This server
is bound to localhost.
2022-11-08T21:30:23.670+0200 I CONTROL [initandlisten] ** Remote syste
ms will be unable to connect to this server.
2022-11-08T21:30:23.671+0200 I CONTROL [initandlisten] ** Start the se
rver with --bind_ip <address> to specify which IP
2022-11-08T21:30:23.672+0200 I CONTROL [initandlisten] ** addresses it
should serve responses from, or with --bind_ip_all to
2022-11-08T21:30:23.673+0200 I CONTROL [initandlisten] ** bind to all
interfaces. If this behavior is desired, start the
2022-11-08T21:30:23.674+0200 I CONTROL [initandlisten] ** server with
--bind_ip 127.0.0.1 to disable this warning.
2022-11-08T21:30:23.675+0200 I CONTROL [initandlisten]
2022-11-08T21:30:24.345+0200 I FTDC [initandlisten] Initializing full-time d
iagnostic data capture with directory 'C:/data/db/diagnostic.data'
2022-11-08T21:30:24.348+0200 I NETWORK [initandlisten] listening via socket bou
nd to 127.0.0.1
2022-11-08T21:30:24.348+0200 I NETWORK [initandlisten] waiting for connections
on port 27017
2022-11-08T21:30:24.501+0200 I NETWORK [listener] connection accepted from 127.
0.0.1:60712 #1 (1 connection now open)
2022-11-08T21:30:24.535+0200 I NETWORK [conn1] received client metadata from 12
7.0.0.1:60712 conn1: { driver: { name: "nodejs", version: "4.8.1" }, os: { type:
"Windows_NT", name: "win32", architecture: "x64", version: "6.3.9600" }, platfo
rm: "Node.js v16.5.0, LE (unified)|Node.js v16.5.0, LE (unified)", application:
{ name: "MongoDB Compass" } }
2022-11-08T21:30:24.719+0200 I NETWORK [listener] connection accepted from 127.
0.0.1:60713 #2 (2 connections now open)
2022-11-08T21:30:24.726+0200 I NETWORK [listener] connection accepted from 127.
0.0.1:60714 #3 (3 connections now open)
2022-11-08T21:30:24.727+0200 I NETWORK [conn2] received client metadata from 12
7.0.0.1:60713 conn2: { driver: { name: "nodejs", version: "4.8.1" }, os: { type:
"Windows_NT", name: "win32", architecture: "x64", version: "6.3.9600" }, platfo
rm: "Node.js v16.5.0, LE (unified)|Node.js v16.5.0, LE (unified)", application:
{ name: "MongoDB Compass" } }
2022-11-08T21:30:24.730+0200 I NETWORK [conn3] received client metadata from 12
7.0.0.1:60714 conn3: { driver: { name: "nodejs", version: "4.8.1" }, os: { type:
"Windows_NT", name: "win32", architecture: "x64", version: "6.3.9600" }, platfo
rm: "Node.js v16.5.0, LE (unified)|Node.js v16.5.0, LE (unified)", application:
{ name: "MongoDB Compass" } }
2022-11-08T21:30:24.742+0200 I NETWORK [conn1] end connection 127.0.0.1:60712 (
2 connections now open)
2022-11-08T21:30:24.743+0200 I NETWORK [listener] connection accepted from 127.
0.0.1:60715 #4 (3 connections now open)
2022-11-08T21:30:24.746+0200 I NETWORK [conn4] received client metadata from 12
7.0.0.1:60715 conn4: { driver: { name: "nodejs", version: "4.8.1" }, os: { type:
"Windows_NT", name: "win32", architecture: "x64", version: "6.3.9600" }, platfo
rm: "Node.js v16.5.0, LE (unified)|Node.js v16.5.0, LE (unified)", application:
{ name: "MongoDB Compass" } }
2022-11-08T21:30:24.776+0200 I NETWORK [conn2] end connection 127.0.0.1:60713 (
2 connections now open)
2022-11-08T21:30:24.887+0200 I NETWORK [conn4] end connection 127.0.0.1:60715 (
1 connection now open)
2022-11-08T21:30:24.907+0200 I NETWORK [conn3] end connection 127.0.0.1:60714 (
0 connections now open)
2022-11-08T21:31:05.556+0200 I NETWORK [listener] connection accepted from 127.
0.0.1:60720 #5 (1 connection now open)
2022-11-08T21:31:05.561+0200 I NETWORK [conn5] received client metadata from 12
7.0.0.1:60720 conn5: { driver: { name: "nodejs", version: "4.8.1" }, os: { type:
"Windows_NT", name: "win32", architecture: "x64", version: "6.3.9600" }, platfo
rm: "Node.js v16.5.0, LE (unified)|Node.js v16.5.0, LE (unified)", application:
{ name: "MongoDB Compass" } }
2022-11-08T21:31:05.571+0200 I NETWORK [listener] connection accepted from 127.
0.0.1:60721 #6 (2 connections now open)
2022-11-08T21:31:05.573+0200 I NETWORK [conn6] received client metadata from 12
7.0.0.1:60721 conn6: { driver: { name: "nodejs", version: "4.8.1" }, os: { type:
"Windows_NT", name: "win32", architecture: "x64", version: "6.3.9600" }, platfo
rm: "Node.js v16.5.0, LE (unified)|Node.js v16.5.0, LE (unified)", application:
{ name: "MongoDB Compass" } }
2022-11-08T21:31:05.993+0200 I NETWORK [listener] connection accepted from 127.
0.0.1:60722 #7 (3 connections now open)
2022-11-08T21:31:05.994+0200 I NETWORK [listener] connection accepted from 127.
0.0.1:60723 #8 (4 connections now open)
2022-11-08T21:31:06.005+0200 I NETWORK [conn7] received client metadata from 12
7.0.0.1:60722 conn7: { driver: { name: "nodejs", version: "4.8.1" }, os: { type:
"Windows_NT", name: "win32", architecture: "x64", version: "6.3.9600" }, platfo
rm: "Node.js v16.5.0, LE (unified)|Node.js v16.5.0, LE (unified)", application:
{ name: "MongoDB Compass" } }
2022-11-08T21:31:06.006+0200 I NETWORK [conn8] received client metadata from 12
7.0.0.1:60723 conn8: { driver: { name: "nodejs", version: "4.8.1" }, os: { type:
"Windows_NT", name: "win32", architecture: "x64", version: "6.3.9600" }, platfo
rm: "Node.js v16.5.0, LE (unified)|Node.js v16.5.0, LE (unified)", application:
{ name: "MongoDB Compass" } }
2022-11-08T21:31:06.033+0200 I NETWORK [listener] connection accepted from 127.
0.0.1:60724 #9 (5 connections now open)
2022-11-08T21:31:06.034+0200 I NETWORK [listener] connection accepted from 127.
0.0.1:60725 #10 (6 connections now open)
2022-11-08T21:31:06.076+0200 I NETWORK [conn9] received client metadata from 12
7.0.0.1:60724 conn9: { driver: { name: "nodejs", version: "4.8.1" }, os: { type:
"Windows_NT", name: "win32", architecture: "x64", version: "6.3.9600" }, platfo
rm: "Node.js v16.5.0, LE (unified)|Node.js v16.5.0, LE (unified)", application:
{ name: "MongoDB Compass" } }
2022-11-08T21:31:06.077+0200 I NETWORK [conn10] received client metadata from 1
27.0.0.1:60725 conn10: { driver: { name: "nodejs", version: "4.8.1" }, os: { typ
e: "Windows_NT", name: "win32", architecture: "x64", version: "6.3.9600" }, plat
form: "Node.js v16.5.0, LE (unified)|Node.js v16.5.0, LE (unified)", application
: { name: "MongoDB Compass" } }
2022-11-08T21:31:06.162+0200 I NETWORK [listener] connection accepted from 127.
0.0.1:60726 #11 (7 connections now open)
2022-11-08T21:31:06.176+0200 I NETWORK [conn11] received client metadata from 1
27.0.0.1:60726 conn11: { driver: { name: "nodejs", version: "4.8.1" }, os: { typ
e: "Windows_NT", name: "win32", architecture: "x64", version: "6.3.9600" }, plat
form: "Node.js v16.5.0, LE (unified)|Node.js v16.5.0, LE (unified)", application
: { name: "MongoDB Compass" } }
2022-11-08T21:31:06.987+0200 I NETWORK [listener] connection accepted from 127.
0.0.1:60727 #12 (8 connections now open)
2022-11-08T21:31:06.996+0200 I NETWORK [conn12] received client metadata from 1
27.0.0.1:60727 conn12: { driver: { name: "nodejs", version: "4.8.1" }, os: { typ
e: "Windows_NT", name: "win32", architecture: "x64", version: "6.3.9600" }, plat
form: "Node.js v16.5.0, LE (unified)|Node.js v16.5.0, LE (unified)", application
: { name: "MongoDB Compass" } }
2022-11-08T21:31:07.277+0200 I NETWORK [listener] connection accepted from 127.
0.0.1:60728 #13 (9 connections now open)
2022-11-08T21:31:07.277+0200 I NETWORK [listener] connection accepted from 127.
0.0.1:60729 #14 (10 connections now open)
2022-11-08T21:31:07.279+0200 I NETWORK [conn13] received client metadata from 1
27.0.0.1:60728 conn13: { driver: { name: "nodejs", version: "4.8.1" }, os: { typ
e: "Windows_NT", name: "win32", architecture: "x64", version: "6.3.9600" }, plat
form: "Node.js v16.5.0, LE (unified)|Node.js v16.5.0, LE (unified)", application
: { name: "MongoDB Compass" } }
2022-11-08T21:31:07.281+0200 I NETWORK [conn14] received client metadata from 1
27.0.0.1:60729 conn14: { driver: { name: "nodejs", version: "4.8.1" }, os: { typ
e: "Windows_NT", name: "win32", architecture: "x64", version: "6.3.9600" }, plat
form: "Node.js v16.5.0, LE (unified)|Node.js v16.5.0, LE (unified)", application
: { name: "MongoDB Compass" } }
2022-11-08T21:31:07.294+0200 I NETWORK [listener] connection accepted from 127.
0.0.1:60730 #15 (11 connections now open)
2022-11-08T21:31:07.297+0200 I NETWORK [conn15] received client metadata from 1
27.0.0.1:60730 conn15: { driver: { name: "nodejs", version: "4.8.1" }, os: { typ
e: "Windows_NT", name: "win32", architecture: "x64", version: "6.3.9600" }, plat
form: "Node.js v16.5.0, LE (unified)|Node.js v16.5.0, LE (unified)", application
: { name: "MongoDB Compass" } }
I tried with mongodb atlas and it worked fine but I connected it with compass nothing worked and it shows me that nothing on this URL.
Solution
hello again!
I'll show you what's happened and what I did to discover this issue but in short if you faced this issue too just try to change localhost word to 127.0.0.1;
At first I deleted the promise and async await part from the code before running it and the terminal showed me this:
Windows PowerShell
Copyright (C) 2014 Microsoft Corporation. All rights reserved.
PS C:\Users\Mazen\Desktop\nodejs\myapp> nodemon app
[nodemon] 2.0.20
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node app.js`
Connected Successfully
Example app listening on port 3000
C:\Users\Mazen\Desktop\nodejs\myapp\node_modules\mongoose\lib\connection.js:824
const serverSelectionError = new ServerSelectionError();
^
MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
at Connection.openUri (C:\Users\Mazen\Desktop\nodejs\myapp\node_modules\mongoose\lib\connection.js:824:32)
at C:\Users\Mazen\Desktop\nodejs\myapp\node_modules\mongoose\lib\index.js:413:10
at C:\Users\Mazen\Desktop\nodejs\myapp\node_modules\mongoose\lib\helpers\promiseOrCallback.js:41:5
at new Promise (<anonymous>)
at promiseOrCallback (C:\Users\Mazen\Desktop\nodejs\myapp\node_modules\mongoose\lib\helpers\promiseOrCallback.js:40:10)
at Mongoose._promiseOrCallback (C:\Users\Mazen\Desktop\nodejs\myapp\node_modules\mongoose\lib\index.js:1266:10)
at Mongoose.connect (C:\Users\Mazen\Desktop\nodejs\myapp\node_modules\mongoose\lib\index.js:412:20)
at Object.<anonymous> (C:\Users\Mazen\Desktop\nodejs\myapp\app.js:5:10)
at Module._compile (node:internal/modules/cjs/loader:1119:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1173:10) {
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: 3794704279,
lastWriteDate: 0,
error: MongoNetworkError: connect ECONNREFUSED ::1:27017
at connectionFailureError (C:\Users\Mazen\Desktop\nodejs\myapp\node_modules\mongodb\lib\cmap\connect.js:387:20)
at Socket.<anonymous> (C:\Users\Mazen\Desktop\nodejs\myapp\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: connect ECONNREFUSED ::1:27017
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1300:16) {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '::1',
port: 27017
},
[Symbol(errorLabels)]: Set(1) { 'ResetPool' }
},
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
}
Node.js v18.9.1
[nodemon] app crashed - waiting for file changes before starting...
and after some researches I found this on MongoDB Developers Community:
Your connection refused message indicates an attempted connection via the IPv6 localhost alias ::1 (0:0:0:0:0:0:0:1).
By default mongod does not bind to IPv6 addresses: you would have to set net.ipv6 to true and add appropriate IPv6 addresses in net.bindIp.
If you are using the default mongod configuration you should be able to connect to localhost:27017 or the equivalent IPv4 address 127.0.0.1:27017.
I hope this helps if someone faced similar issue.
Answered By - Mazen Othman
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.