Unable to Connect From Dockerized Node Program to Localhost Mongodb - node.js

I am able to npm start the code from cmd and have it running with mongodb but docker build the code and docker run it generates this error:
/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/server.js:265
process.nextTick(function() { throw err; })
^
MongoError: failed to connect to server [127.0.0.1:27017] on first connect
at null.<anonymous> (/usr/src/app/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:325:35)
at emitOne (events.js:77:13)
at emit (events.js:169:7)
at null.<anonymous> (/usr/src/app/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/pool.js:270:12)
at g (events.js:260:16)
at emitTwo (events.js:87:13)
at emit (events.js:172:7)
at Socket.<anonymous> (/usr/src/app/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:173:49)
at Socket.g (events.js:260:16)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at emitErrorNT (net.js:1269:8)
at nextTickCallbackWith2Args (node.js:458:9)
at process._tickCallback (node.js:372:17)
My app.js is listening in on port 8080 and i exposed port 8080 and 27017 on dockerfile. May i know what else should i be doing to resolve the error. I am a beginner on this, pardon me.

you only expose 8080 and that's why it couldn't connect to 27017!
instead of -p 8080:8080 use -P to expose 27017 too!
so you're build command would be this:
docker run -P -d ryan/edocker

Related

Unable to use port forwarding with mongoose

I am trying to bind my local port to remote mongo port as remote server is not directly accessible from local. Im trying SSH port binding like this.
ssh -L 27017:localhost:27017 <remote server>
when i try to open mongo shell locally using mongo mongodb://localhost:27017. mongo is connected to remote server but when i do the same from node app using mongoose. i am getting connection error. Can someone suggest system level solution to this problem.
Something broke!!! - MongoError: failed to connect to server [server1:27017] on first connect [MongoError: getaddrinfo ENOTFOUND server1 server1:27017]
at null.<anonymous> (/home/user1/src/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:336:35)
at emitOne (events.js:77:13)
at emit (events.js:169:7)
at null.<anonymous> (/home/user1/src/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/pool.js:280:12)
at g (events.js:260:16)
at emitTwo (events.js:87:13)
at emit (events.js:172:7)
at Socket.<anonymous> (/home/user1/src/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:187:49)
at Socket.g (events.js:260:16)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at connectErrorNT (net.js:1016:8)
at nextTickCallbackWith2Args (node.js:511:9)
at process._tickCallback (node.js:425:17)

connect my noSQL(mongoDB) database with my nodeJS project. But it is not working fine

I am trying to connect my nodejs with mongoDB but it is not working fine as the following result below.
**
(node:11776) DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your o
wn promise library instead: http://mongoosejs.com/docs/promises.html
{ MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0
.1:27017]
at Pool.<anonymous> (D:\KIT\YEAR II\Project\Nodejs\Vidjot\node_modules\mongodb-core\lib\topologies\server.js:336:3
5)
at emitOne (events.js:116:13)
at Pool.emit (events.js:211:7)
at Connection.<anonymous> (D:\KIT\YEAR II\Project\Nodejs\Vidjot\node_modules\mongodb-core\lib\connection\pool.js:2
80:12)
at Object.onceWrapper (events.js:317:30)
at emitTwo (events.js:126:13)
at Connection.emit (events.js:214:7)
at Socket.<anonymous> (D:\KIT\YEAR II\Project\Nodejs\Vidjot\node_modules\mongodb-core\lib\connection\connection.js
:187:49)
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: 'MongoError',
message: 'failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1
:27017]' }
**
Here is my source code
Make sure your mongo server is running # localhost:27017 and you have directories created to store data.
start using this command:
>mongod --port 27017 --dbpath c:\data\db

API Pod failed to connect to mongo pod in the same kubernetes cluster

I was running a nodejs api as a pod on a google compute engine managed by kubernetes. The API was connecting to database well but then it failed suddenly showing this error:
listening to http server on 0.0.0.0:8080...
events.js:160
throw er; // Unhandled 'error' event
^
MongoError: failed to connect to server [mongo:27017] on first connect [MongoError: getaddrinfo ENOTFOUND mongo mongo:27017]
at Pool.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/topologies/server.js:328:35)
at emitOne (events.js:96:13)
at Pool.emit (events.js:188:7)
at Connection.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/pool.js:274:12)
at Connection.g (events.js:292:16)
at emitTwo (events.js:106:13)
at Connection.emit (events.js:191:7)
at Socket.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/connection.js:177:49)
at Socket.g (events.js:292:16)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at connectErrorNT (net.js:1021:8)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickDomainCallback (internal/process/next_tick.js:128:9)
I tried to restart the prods, to delete and re-create containers but no success?
this is how I connect to database:
mongoose.connect(process.env.MONGO_DEV_URL || process.env.MONGODB_URI || 'mongodb://mongo:27017:yaxiDb/yaxiDb', { useMongoClient: true });
How can I debug this, where could be the problem?
I just found the solution, the order of creating services matters. I've deleted all services that use that database and re-create them:
kubectl delete -f containers/backend/
kubectl create -f containers/backend/

Failed to connect mongodb from nodejs using mongodbDriver on windows server 2003

I check the MongoDB service is in running and I can connect MongoDB unsing /bin/mongo.exe BUT I cannot connect to MongoDB server using nodejs driver.
My codes are working on Windows 10. The error messages are below.
C:\Documents and Settings\Administrator\Desktop\src\server>node server.js
Listening on port 80
C:\Documents and Settings\Administrator\Desktop\src\server\node_modules\mongodb\
lib\mongo_client.js:225
throw err
^
MongoError: failed to connect to server [127.0.0.1:27017] on first connect
at null.<anonymous> (C:\Documents and Settings\Administrator\Desktop\src\ser
ver\node_modules\mongodb\node_modules\mongodb-core\lib\topologies\server.js:313:
35)
at emitOne (events.js:77:13)
at emit (events.js:169:7)
at null.<anonymous> (C:\Documents and Settings\Administrator\Desktop\src\ser
ver\node_modules\mongodb\node_modules\mongodb-core\lib\connection\pool.js:260:12
)
at g (events.js:260:16)
at emitTwo (events.js:87:13)
at emit (events.js:172:7)
at Socket.<anonymous> (C:\Documents and Settings\Administrator\Desktop\src\s
erver\node_modules\mongodb\node_modules\mongodb-core\lib\connection\connection.j
s:162:49)
at Socket.g (events.js:260:16)
at emitOne (events.js:77:13)
Use lower version of mongodb driver.
In this case, I use 1.4.

Parse-Server NPM Install - Internal Server Error

I completed the Parse-Serve npm install as follows
$ npm install -g parse-server mongodb-runner
$ mongodb-runner start
$ parse-server --appId APPLICATION_ID --masterKey MASTER_KEY
when I tried to access it with the follow command.....
$ curl -X POST -H "X-Parse-Application-Id: AppID123" -H "Content-Type: application/json" -d '{"score":1337,"playerName":"Sean Plott","cheatMode":false}' http://localhost:1337/parse/classes/GameScore
i got the following error:
{"code":1,"message":"Internal server error."}
Here is the result from running parse-server....
$ parse-server --appId AppID123 --masterKey Master123
appId: AppID8Rasp
masterKey: ***REDACTED***
port: 1337
mountPath: /parse
maxUploadSize: 20mb
serverURL: http://localhost:1337/parse
parse-server running on http://localhost:1337/parse
warn: Unable to ensure uniqueness for usernames: MongoError: failed to connect to server [localhost:27017] on first connect
at null.<anonymous> (/usr/local/lib/node_modules/parse-server/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:311:35)
at emitOne (events.js:77:13)
at emit (events.js:169:7)
at null.<anonymous> (/usr/local/lib/node_modules/parse-server/node_modules/mongodb/node_modules/mongodb-core/lib/connection/pool.js:254:12)
at g (events.js:260:16)
at emitTwo (events.js:87:13)
at emit (events.js:172:7)
at Socket.<anonymous> (/usr/local/lib/node_modules/parse-server/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:158:49)
at Socket.g (events.js:260:16)
at emitOne (events.js:77:13)
warn: Unable to ensure uniqueness for user email addresses: MongoError: failed to connect to server [localhost:27017] on first connect
at null.<anonymous> (/usr/local/lib/node_modules/parse-server/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:311:35)
at emitOne (events.js:77:13)
at emit (events.js:169:7)
at null.<anonymous> (/usr/local/lib/node_modules/parse-server/node_modules/mongodb/node_modules/mongodb-core/lib/connection/pool.js:254:12)
at g (events.js:260:16)
at emitTwo (events.js:87:13)
at emit (events.js:172:7)
at Socket.<anonymous> (/usr/local/lib/node_modules/parse-server/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:158:49)
at Socket.g (events.js:260:16)
at emitOne (events.js:77:13)
error: Error generating response. { [MongoError: failed to connect to server [localhost:27017] on first connect]
name: 'MongoError',
message: 'failed to connect to server [localhost:27017] on first connect' } name=MongoError, message=failed to connect to server [localhost:27017] on first connect
error: Uncaught internal server error. { [MongoError: failed to connect to server [localhost:27017] on first connect]
name: 'MongoError',
message: 'failed to connect to server [localhost:27017] on first connect' } MongoError: failed to connect to server [localhost:27017] on first connect
at null.<anonymous> (/usr/local/lib/node_modules/parse-server/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:311:35)
at emitOne (events.js:77:13)
at emit (events.js:169:7)
at null.<anonymous> (/usr/local/lib/node_modules/parse-server/node_modules/mongodb/node_modules/mongodb-core/lib/connection/pool.js:254:12)
at g (events.js:260:16)
at emitTwo (events.js:87:13)
at emit (events.js:172:7)
at Socket.<anonymous> (/usr/local/lib/node_modules/parse-server/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:158:49)
at Socket.g (events.js:260:16)
at emitOne (events.js:77:13)
MongoError: failed to connect to server [localhost:27017] on first connect
at null.<anonymous> (/usr/local/lib/node_modules/parse-server/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:311:35)
at emitOne (events.js:77:13)
at emit (events.js:169:7)
at null.<anonymous> (/usr/local/lib/node_modules/parse-server/node_modules/mongodb/node_modules/mongodb-core/lib/connection/pool.js:254:12)
at g (events.js:260:16)
at emitTwo (events.js:87:13)
at emit (events.js:172:7)
at Socket.<anonymous> (/usr/local/lib/node_modules/parse-server/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:158:49)
at Socket.g (events.js:260:16)
at emitOne (events.js:77:13)
Sure could use some pointers !! thanks.
1) stop your server.
2) install mongodb
sudo apt-get install -y mongodb-org
3) test to make sure it is running
sudo apt-get install -y mongodb-org
A suitable response would be :
'mongod start/running, process 18777'
4) restart your server:
npm start
5) try creating the post again
curl -X POST -H "X-Parse-Application-Id: AppID123" -H "Content-Type: application/json" -d '{"score":1337,"playerName":"Sean Plott","cheatMode":false}' http://localhost:1337/parse/classes/GameScore
p.s. "localhost" should really be the address of your server.
for a more detailed explanation :
https://www.digitalocean.com/community/tutorials/how-to-install-mongodb-on-ubuntu-14-04

Resources