google cloud cannot connect to mongodb cluster - node.js

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

Related

cannot connect to SQLExpress usign node.js

I am using SQL Server (SQL EXPRESS) 2005 9.0.3042
I have poured through lots of examples.
Made sure TCP port 1433 open for all IP.
Made sure browser is started
Tried all kinds of config settings
using tedious or mssql
Just trying to find something that will work !!
But all I get is the following errors
ConnectionError: Failed to connect to localhost:1433 - Cannot call write after a stream was destroyed
at ConnectionError (C:\git\adv_energy\sql_node_js\node_modules\tedious\lib\errors.js:13:12)
at Connection.socketError (C:\git\adv_energy\sql_node_js\node_modules\tedious\lib\connection.js:1628:56)
at Socket.<anonymous> (C:\git\adv_energy\sql_node_js\node_modules\tedious\lib\connection.js:1395:14)
at Socket.emit (events.js:327:22)
at errorOrDestroy (internal/streams/destroy.js:108:12)
at onwriteError (_stream_writable.js:418:5)
at onwrite (_stream_writable.js:445:5)
at doWrite (_stream_writable.js:399:11)
at writeOrBuffer (_stream_writable.js:387:5)
at Socket.Writable.write (_stream_writable.js:318:11) {
code: 'ESOCKET'
}

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

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 :)

No living connections Error while Elasticsearch connections in nodejs

I am having this problem while connecting the elasticsearch connections.
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({ host: 'localhost:9200',
log: 'trace'});
Elasticsearch ERROR: 2016-07-19T19:09:26Z
Error: Request error, retrying -- connect ECONNREFUSED 127.0.0.1:9200
at Log.error (/root/git_build/FirstMoveChess/node_modules/elasticsearch/src/lib/log.js:225:56)
at checkRespForFailure (/root/git_build/FirstMoveChess/node_modules/elasticsearch/src/lib/transport.js:195:18)
at HttpConnector. (/root/git_build/FirstMoveChess/node_modules/elasticsearch/src/lib/connectors/http.js:154:7)
at ClientRequest.bound (/root/git_build/FirstMoveChess/node_modules/lodash-node/modern/internals/baseBind.js:56:17)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:188:7)
at Socket.socketErrorListener (_http_client.js:308:9)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at emitErrorNT (net.js:1272:8)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
Elasticsearch TRACE: 2016-07-19T19:09:27Z
-> HEAD http://localhost:9200/
I understand this question is quite old but I wanted to share how you can solve this problem.
If you are locally using elasticsearch
First thing you must do is running elasticsearch on your machine.
Error: Request error, retrying -- connect ECONNREFUSED 127.0.0.1:9200
Because above message indicates that you're not running elasticsearch locally.
So, visit the link and follow the insturction.
Docker environment
It gets much trickier here.
First, follow the instruction here.
And in case you're using node.js elasticsearch client, you have to specify elasticsearch host as 172.24.0.1.
If you use container_name or private IP of container in docker-compose.yml, it won't work.
In case of Docker Container Environment after changing from http://localhost:9200 to http://ipaddress:9200 in docker-compose.yml
please change the following live in docker-compose.yml that is related to CORS
Change this
** http.cors.allow-origin=/https?://localhost(:[0-9]+)?/ **
into this
*- http.cors.allow-origin= **

Resources