No living connections Error while Elasticsearch connections in nodejs - node.js

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= **

Related

MongoNetworkError on first connect using Docker and NodeJS

I'm still incredibly new to coding. I'm trying to launch an app using docker, everything goes well until I get this error:
{ MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
at Pool.<anonymous> (/app/node_modules/mongodb/lib/core/topologies/server.js:433:11)
at Pool.emit (events.js:198:13)
at createConnection (/app/node_modules/mongodb/lib/core/connection/pool.js:577:14)
at connect (/app/node_modules/mongodb/lib/core/connection/pool.js:1007:11)
at makeConnection (/app/node_modules/mongodb/lib/core/connection/connect.js:31:7)
at callback (/app/node_modules/mongodb/lib/core/connection/connect.js:247:5)
at Socket.err (/app/node_modules/mongodb/lib/core/connection/connect.js:276:7)
at Object.onceWrapper (events.js:286:20)
at Socket.emit (events.js:198:13)
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
name: 'MongoNetworkError',
[Symbol(mongoErrorContextSymbol)]: {} }
[nodemon] clean exit - waiting for changes before restart
I have mongodb running as a server on Windows 10, and I also have it running in another window, so I'm not sure what could be causing it. I've tried all solutions on this page, but to no avail. Thanks in advance!
Localhost is the container, not your machine. When working with containers, you need to think of container as a separate machine and use a full network adressess to communicate between your machine and container.
I was using a similar concept like yours. Try to run both node-app and Mongo service under the same network.
In Mongo connection string, the Hostname should be the same as your service name mentioned in docker-compose.yml file.

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

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

Connection refused while connection elasticsearch in node

I am using node version 0.10.33 and elasticsearch version 3.0.0 but i am not able to connect to elasticsearch.
http://localhost:9200/
Elasticsearch ERROR: 2014-11-25T16:30:16Z
Error: Request error, retrying -- connect ECONNREFUSED
at Log.error (/Users/shank/Documents/MyUmpa/Server/node_modules/elasticsearch/src/lib/log.js:213:60)
at checkRespForFailure (/Users/shank/Documents/MyUmpa/Server/node_modules/elasticsearch/src/lib/transport.js:195:18)
at HttpConnector.<anonymous> (/Users/shank/Documents/MyUmpa/Server/node_modules/elasticsearch/src/lib/connectors/http.js:146:7)
at ClientRequest.bound (/Users/shank/Documents/MyUmpa/Server/node_modules/elasticsearch/node_modules/lodash-node/modern/internals/baseBind.js:56:17)
at ClientRequest.emit (events.js:95:17)
at Socket.socketErrorListener (http.js:1551:9)
at Socket.emit (events.js:95:17)
at net.js:440:14
at process._tickCallback (node.js:419:13)
Trace: elasticsearch cluster is down!
at app.post.resturantController.ResturantInfo.name (/Users/shank/Documents/MyUmpa/Server/app.js:32:13)
at respond (/Users/shank/Documents/MyUmpa/Server/node_modules/elasticsearch/src/lib/transport.js:254:9)
at sendReqWithConnection (/Users/shank/Documents/MyUmpa/Server/node_modules/elasticsearch/src/lib/transport.js:174:7)
at next (/Users/shank/Documents/MyUmpa/Server/node_modules/elasticsearch/src/lib/connection_pool.js:213:7)
at process._tickCallback (node.js:419:13)
Have you run elastic search server? If not then Download Elasticsearch and unzip it into whatever directory you like. Next, open a terminal, cd to the directory you just unzipped, and run bin/elasticsearch (bin/elasticsearch.bat on Windows). After running elasticsearch server, run the node server!

Resources