MongoNetworkError : failed to connect to server - node.js

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.

Related

MongoNetworkError on node js and mongo db atlas

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

Error connecting web server on Elastic Beanstalk with database server on MongoDB Atlas

I am trying to connect my web servers on Elastic Beanstalk with my database servers on MongoDB Atlas, but somehow I get the following error
Listening at http://:::3001
Unable to connect to the mongodb instance. Error: { MongoNetworkError: failed to connect to server [<server>.mongodb.net:27017] on first connect [MongoNetworkError: connection 5 to <server>.mongodb.net:27017 closed]
at Pool.<anonymous> (/var/app/current/node_modules/mongodb-core/lib/topologies/server.js:431:11)
at Pool.emit (events.js:198:13)
at connect (/var/app/current/node_modules/mongodb-core/lib/connection/pool.js:557:14)
at callback (/var/app/current/node_modules/mongodb-core/lib/connection/connect.js:109:5)
at runCommand (/var/app/current/node_modules/mongodb-core/lib/connection/connect.js:129:7)
at Connection.errorHandler (/var/app/current/node_modules/mongodb-core/lib/connection/connect.js:321:5)
at Object.onceWrapper (events.js:286:20)
at Connection.emit (events.js:198:13)
at TLSSocket.<anonymous> (/var/app/current/node_modules/mongodb-core/lib/connection/connection.js:350:12)
at Object.onceWrapper (events.js:286:20)
at TLSSocket.emit (events.js:198:13)
at _handle.close (net.js:606:12)
at TCP.done (_tls_wrap.js:388:7)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
Listening at http://:::3001
(node:11482) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
Listening at http://:::3001
I am using mongoose as follows
mongoose.connect('mongodb+srv://<UserName>:<Password>#<cluster>.mongodb.net/test?retryWrites=true&w=majority',
{ useNewUrlParser: true, useUnifiedTopology: true })
.catch(function (reason) {
console.log('Unable to connect to the mongodb instance. Error: ', reason);
});
The problem is not in the IP Whitelist on MongoDB Atlas, since I already ssh'd into the server on elastic beanstalk and from there connected to the Mongo Servers on Atlas directly. The problem is in the code, although I can't seem to see it.
I had the same issue. Add 0.0.0.0/0 to your IP whitelist.

MongoDB refuses Connection on Windows 10 machine

i'm running a Node.js app on my Windows 10 machine. I have Mongoose installed and setup like this:
// Init Database
var mongoose = require("mongoose");
var mongoDB = "mongodb://127.0.0.1/sampledata"
mongoose.connect(mongoDB);
var db = mongoose.connection;
// Check DB connection
db.once("open", function(){
console.log("Connected to MongoDB");
});
// Check for DB errors
db.on("error", function(err){
console.log(err);
});
Now, when I start the server with nodemon, the following error appears in the console:
{ MongoNetworkError: failed to connect to server [127.0.0.1:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
at Pool.<anonymous> (C:\Users\...\node_modules\mongodb-core\lib\topologies\server.js:564:11)
at emitOne (events.js:96:13)
at Pool.emit (events.js:188:7)
at Connection.<anonymous> (C:\Users\...\node_modules\mongodb-core\lib\connection\pool.js:317:12)
at Connection.g (events.js:291:16)
at emitTwo (events.js:106:13)
at Connection.emit (events.js:191:7)
at Socket.<anonymous> (C:\...\node_modules\mongodb-core\lib\connection\connection.js:246:50)
at Socket.g (events.js:291:16)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at emitErrorNT (net.js:1278:8)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ]
}
When I try to start mongo over the console, I get the following error:
C:\Program Files\MongoDB\Server\4.0\bin>mongo
MongoDB shell version v4.0.2
connecting to: mongodb://127.0.0.1:27017
2018-10-03T19:05:24.194+0200 E QUERY [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Es konnte keine Verbindung hergestellt werden, da der Zielcomputer die Verbindung verweigerte (in english: A connection couldn't be established, because the target computer has refused the connection). :
connect#src/mongo/shell/mongo.js:257:13
#(connect):1:6
exception: connect failed
I've never encountered this error before and as stupid as it sounds, yesterday everything worked fine. Haven't changed the code or anything, so it must be an issue with my computer I guess.
Do you guys know what could cause this error?
TL;DR
Start mongod server
mongod
Open another terminal window
Start mongo shell
mongo
Longer Version:
Open Command prompt as an administrator and run netstat -anb, check which port mongodb is listening to (Usually 27017). If you dont see a listing for mongo then it means that mongod is not running, so go ahead and start mongod.exe from the mongodb binaries(or if environment variables are set, run mongod from anywhere on your cmd).
if this doesnt work, your firewall is probably blocking access on that port, so check that out
I was getting this error as well. In my case, I was trying to upgrade to MongoDB 4.0 without first upgrading to 3.6.
Please see https://docs.mongodb.com/manual/release-notes/4.0/#upgrade-procedures if you recently updated MongoDB.

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');

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