MongoNetworkError on node js and mongo db atlas - node.js

I have made a MEAN stack project. And this project was successfully run some days ago. But suddenly it gives MongoNetworkError. I do not change anything. I use mongo atlas. The error description is given below.
error MongoNetworkError: failed to connect to server [cluster0-shard-00-01-s9niu.mongodb.net:27017] on first connect [MongoNetworkError: connection 5 to cluster0-shard-00-01-s9niu.mongodb.net:27017 closed
at TLSSocket. (D:\Angular\meal-system-server\node_modules\mongodb\lib\core\connection\connection.js:372:9)
at Object.onceWrapper (events.js:300:26)
at TLSSocket.emit (events.js:210:5)
at net.js:658:12
at TCP.done (_tls_wrap.js:481:7) {
name: 'MongoNetworkError',
}]
at Pool. (D:\Angular\meal-system-server\node_modules\mongodb\lib\core\topologies\server.js:438:11)
at Pool.emit (events.js:210:5)
at D:\Angular\meal-system-server\node_modules\mongodb\lib\core\connection\pool.js:561:14
at D:\Angular\meal-system-server\node_modules\mongodb\lib\core\connection\pool.js:1008:9
at callback (D:\Angular\meal-system-server\node_modules\mongodb\lib\core\connection\connect.js:97:5)
at D:\Angular\meal-system-server\node_modules\mongodb\lib\core\connection\connect.js:124:7
at _callback (D:\Angular\meal-system-server\node_modules\mongodb\lib\core\connection\connect.js:349:5)
at Connection.errorHandler (D:\Angular\meal-system-server\node_modules\mongodb\lib\core\connection\connect.js:365:5)
at Object.onceWrapper (events.js:300:26)
at Connection.emit (events.js:210:5)
at TLSSocket. (D:\Angular\meal-system-server\node_modules\mongodb\lib\core\connection\connection.js:370:12)
at Object.onceWrapper (events.js:300:26)
at TLSSocket.emit (events.js:210:5)
at net.js:658:12
at TCP.done (_tls_wrap.js:481:7) {
name: 'MongoNetworkError',
}

Seems like an error with the certificate, mongodb atlas recently made changes in the certificates.
Which language you are using for this application and what is the version of the language ?
Please refer this document for more information : Mongodb Atlas Security

Related

MongoNetworkError : failed to connect to server

First time working with mongoDb inside a Node.js app (using mLab) but i'm unable to connect.
Same exact code works just fine if i try to connect to mongoDB Atlas.
// not working with mLab link and credentials
mongoose.connect(mLabConnectionString)
.then(success => console.log(success))
.catch(error => console.log(error))
// working with atlas link and credentials
mongoose.connect(atlasConnectionString)
.then(success => console.log(success))
.catch(error => console.log(error))
Followed these steps https://docs.mlab.com/troubleshooting-connection-issues/
and i do have network access, i'm using database user credentials to connect (not my mLab account credentials) and my password just contains chars and numbers. That's the error i'm getting :
{ MongoNetworkError: failed to connect to server [***my_database***]
on first connect [MongoNetworkError: getaddrinfo ENOTFOUND ***my_database*** ***my_database***]
at Pool.<anonymous> (C:\Users\akain\Desktop\devconnector\node_modules\mongodb-core\lib\topologies\server.js:564:11)
at Pool.emit (events.js:182:13)
at Connection.<anonymous> (C:\Users\akain\Desktop\mongotest\node_modules\mongodb-core\lib\connection\pool.js:317:12)
at Object.onceWrapper (events.js:273:13)
at Connection.emit (events.js:182:13)
at Socket.<anonymous> (C:\Users\akain\Desktop\mongotest\node_modules\mongodb-core\lib\connection\connection.js:246:50)
at Object.onceWrapper (events.js:273:13)
at Socket.emit (events.js:182:13)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
Now the weird part : If i try to connect to mLab using mongo shell, not only everything works (from the shell), but i'm also able to connect without problem from my app. As soon as i close the shell and restart the app i get the error again.
I've also tried using mongoDb Node client instead of mongoose getting the same exact behaviour.

Connecting to Mongodb docker container from another docker container

I am trying to connect mongo DB running in docker container from another docker container where my node js code is running.
So I run MongoDB docker using the following command:
docker run --name my-local-mongo -v mongo-data:/data/db -p 27017:27017 -d mongo
I can access from browser typing 0.0.0.0:27017, however when I try to connect from node js code I am getting following error. My url variable is:
var url = "mongodb://0.0.0.0:27017/surveydb";
{ MongoNetworkError: failed to connect to server [0.0.0.0:27017] on first connect [MongoNetworkError: connect **ECONNREFUSED 0.0.0.0:27017**]
at Pool.<anonymous> (/usr/src/appg08/node_modules/mongodb-core/lib/topologies/server.js:564:11)
at Pool.emit (events.js:182:13)
at Connection.<anonymous> (/usr/src/appg08/node_modules/mongodb-core/lib/connection/pool.js:317:12)
at Object.onceWrapper (events.js:273:13)
at Connection.emit (events.js:182:13)
at Socket.<anonymous> (/usr/src/appg08/node_modules/mongodb-core/lib/connection/connection.js:246:50)
at Object.onceWrapper (events.js:273:13)
at Socket.emit (events.js:182:13)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
Thanks.
var url = "mongodb://0.0.0.0:27017/surveydb";
is ip adddress is in your nodejs container only, so you should know what is ip address of mongo container or assign to ip address gateway of containers.
var url = "mongodb://172.17.0.1:27017/surveydb";
or
var url = "mongodb://ipaddressofmongocontainer:27017/surveydb";

MacOSX Express Mongoose mongoose.connect('mongodb://localhost:27071') -- ECONNREFUSED 127.0.0.1:27071 -- works fine on CLI

I'm pretty sure I've exhausted all methods here, followed the first 3 pages of Google and near every similar question here, and I am getting sick of reading every response to this "have you downloaded MongoDB?".
YES, it is downloaded, and running, and connectable from Robo3T and CLI mongo.
MongoDB shell version v4.0.2
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.2
YES, it was downloaded/installed using the recommended setup from source -- ie, home brew, brew services etc.
Jamies-MacBook-Pro:api jamiepoole$ brew services list
Name Status User Plist
mongodb started jamiepoole /Users/jamiepoole/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
YES, I have tried connecting with localhost, 127.0.0.1, hell even the computer hostname / Jamies-MacBook-Pro.local
I have no doubt I'm doing something stupid or missed something simple, but I am pretty certain its not that.
const Database = Mongoose.connect('mongodb://localhost:27071/myproject');
---
I replaced project details from the below with ... -- so this isn't a setting / in the code.
{ MongoNetworkError: failed to connect to server [localhost:27071] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27071]
at Pool.<anonymous> (/Users/.../node_modules/mongodb-core/lib/topologies/server.js:564:11)
at emitOne (events.js:116:13)
at Pool.emit (events.js:211:7)
at Connection.<anonymous> (/Users/.../node_modules/mongodb-core/lib/connection/pool.js:317:12)
at Object.onceWrapper (events.js:317:30)
at emitTwo (events.js:126:13)
at Connection.emit (events.js:214:7)
at Socket.<anonymous> (/Users/.../node_modules/mongodb-core/lib/connection/connection.js:246:50)
at Object.onceWrapper (events.js:315:30)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at emitErrorNT (internal/streams/destroy.js:64:8)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
---
At this point any help at all would be amazing. Seems like such a simple error but can't find any help anywhere.
Cheers
You have defined wrong MongoDB port in your code while your CLI is connecting to the right port which 27017. You can edit the code as follows
const Database = Mongoose.connect('mongodb://localhost:27017/myproject');

google cloud cannot connect to mongodb cluster

I have a MongoDB Cluster in MongoDB Atlas and I use this MONGO_URI that they provided to connect to it. The format is something like this:
mongodb://<USERNAME>:<PASSWORD>#cluster0-shard-00-00-nnn.gcp.mongodb.net:27017,cluster0-shard-00-01-nnn.gcp.mongodb.net:27017,cluster0-shard-00-02-nnn.gcp.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true
When I run my node.js program locally, I can read and write the database. However, when I deploy to google cloud, I'm having connecting to it. This is what I see in Google Console:
Mongoose connection "error" event fired with:
{ MongoError: connection 5 to cluster0-shard-00-02-32tq4.mongodb.net:27017 closed
at Function.MongoError.create (/srv/node_modules/mongoose/node_modules/mongodb-core/lib/error.js:29:11)
at TLSSocket.<anonymous> (/srv/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:214:22)
at Object.onceWrapper (events.js:315:30)
at emitOne (events.js:121:20)
at TLSSocket.emit (events.js:211:7)
at _handle.close (net.js:557:12)
at Socket.done (_tls_wrap.js:356:7)
at Object.onceWrapper (events.js:315:30)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at TCP._handle.close [as _onclose] (net.js:557:12)
I've tried to add allow-mongodb to firewall with protocols and ports as tcp:27017, direction ingress, IP ranges 0.0.0.0/0 and it's still not working. Can someone tell me what I'm missing here?
P.S I'm kinda suspecting that I have to add my mongodb cluster's IP to firewall as well though I'm not sure

mongoose wont connect to ec2 mongodb instance

i'm trying to create a node.js server that connect to my mongodb hosted on ec2 instance. I'm doing like this :
mongoose.connect("mongodb://username:password#ec2-xx-xx-xxx-xxx.eu-central-1.compute.amazonaws.com:27017/dbname");
db.on('error', err => {
console.log('Mongoose default connection error:', err);
db.close();
});
db.once('open', () => {
console.log(`Mongoose default connection open`);
});
But it keep saying me this error :
Mongoose default connection error: { MongoError: failed to connect to server [xxx] on first
connect [MongoError: connect ETIMEDOUT xxx:27017]
at Pool.<anonymous> (/Users/jmisiti/hm-landing/node_modules/mongodb-core/lib/topologies/server.js:336:35)
at emitOne (events.js:115:13)
at Pool.emit (events.js:210:7)
at Connection.<anonymous> (/Users/jmisiti/hm-landing/node_modules/mongodb-core/lib/connection/pool.js:280:12)
at Object.onceWrapper (events.js:318:30)
at emitTwo (events.js:125:13)
at Connection.emit (events.js:213:7)
at Socket.<anonymous> (/Users/jmisiti/hm-landing/node_modules/mongodb-core/lib/connection/connection.js:189:49)
at Object.onceWrapper (events.js:316:30)
at emitOne (events.js:115:13)
at Socket.emit (events.js:210:7)
at emitErrorNT (internal/streams/destroy.js:62:8)
at _combinedTickCallback (internal/process/next_tick.js:102:11)
at process._tickCallback (internal/process/next_tick.js:161:9)
name: 'MongoError',
message: 'failed to connect to server [xxx] on first connect [MongoError: connect ETIMEDO
UT xxx:27017]' }
My connection string is 100% sure it works. My mongoose version work great with my mongodb version. Any idea ? :)
PS : Ports are open on 27017 and there's already one of my aws instance using my mongodb. So i'm sure it work.
Make sure that you have registered 27017 port in Outbound ports In the EC2 security group.
Then try to access the DB. and if you have already added port and still experiencing the error then check if you MongoDB process is running as a service(i.e. in the background). If not then follow this procedure
Got it. I can't connect from my localhost to my mongodb server (Don't know why) but when i'm connecting to my mongodb server from my ec2 instance it work. So it's fine :)

Resources