2

I am not able to solve the mongodb connection problem. I am getting the error below on starting node app.

Here is my mongoose connection code.

var mongoose = require('mongoose');

mongoose.Promise = global.Promise;
mongoose.connect(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017/ml').then(
    () => { console.log('Connected to mongodb!') },
    err => { console.log('Failed to connect to mongodb'); console.log(err); }
);

module.exports = {mongoose};

Here is the error.

Started up at port 80
Failed to connect to mongodb
{ MongoNetworkError: failed to connect to server [10.160.0.2:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 10.160.0.2:27017]
    at Pool.<anonymous> (/home/shivamsharma_btp2/mlsever/magguland/node_modules/mongodb-core/lib/topologies/server.js:564:11)
    at Pool.emit (events.js:189:13)
    at Connection.<anonymous> (/home/shivamsharma_btp2/mlsever/magguland/node_modules/mongodb-core/lib/connection/pool.js:317:12)
    at Object.onceWrapper (events.js:277:13)
    at Connection.emit (events.js:189:13)
    at Socket.<anonymous> (/home/shivamsharma_btp2/mlsever/magguland/node_modules/mongodb-core/lib/connection/connection.js:246:50)
    at Object.onceWrapper (events.js:277:13)
    at Socket.emit (events.js:189: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)]: {} }

Here are firewall rules of my google cloud app.

jww
  • 97,681
  • 90
  • 411
  • 885
last_fix
  • 369
  • 1
  • 4
  • 19
  • *+1* for providing your code with the question. – jww Feb 10 '19 at 12:32
  • Is on Windows? MongoDB server is running as a service? check https://stackoverflow.com/questions/52153538/what-is-a-transienttransactionerror-in-mongoose-or-mongodb – fedeteka Feb 25 '19 at 13:09
  • no debian. mongodb is running using the commands given in its [docs page](https://docs.mongodb.com/manual/tutorial/install-mongodb-on-debian/). i have seen this question but it could not help! – last_fix Feb 26 '19 at 00:25

1 Answers1

0

Got solution!

Use sudo before the command to start server.

last_fix
  • 369
  • 1
  • 4
  • 19