5

I have a MEAN project and this is a snippet from my server.js

var db = require('./config/db');
// url : 'mongodb://localhost/cdnserver'
// results are same for 127.0.0.1 or the machines ip
console.log(mongoose.connect(db.url));

mongoose.set('debug', true);
mongoose.connection.on('connected', function () {
  console.log('Mongoose default connection open to ' + db.url);
});

// If the connection throws an error
mongoose.connection.on('error',function (err) {
  console.log('Mongoose default connection error: ' + err);
});

// When the connection is disconnected
mongoose.connection.on('disconnected', function () {
  console.log('Mongoose default connection disconnected');
});

This is a setup that has been working well for over 3 months now. Now I am replicating my whole MEAN stack along with database to other machine. I took a mongodump and did mongorestore. The restored db setup looks fine through mongo from terminal.

However, when starting the server, connection-open callback is not getting called. disconnected and error callbacks are getting called if I stop the mongodb service. How do I debug this further?

I am attaching the console output from both the setups.

Setup 1 :

    Mongoose {
  connections:
   [ NativeConnection {
       base: [Circular],
       collections: {},
       models: {},
       replica: false,
       hosts: null,
       host: 'localhost',
       port: 27017,
       user: undefined,
       pass: undefined,
       name: 'cdnserver',
       options: [Object],
       otherDbs: [],
       _readyState: 2,
       _closeCalled: false,
       _hasOpened: false,
       _listening: false,
       db: [Object] } ],
  plugins: [],
  models: {},
  modelSchemas: {},
  options: { pluralization: true } }
Server up on 80
Mongoose default connection open to mongodb://localhost/cdnserver
1
Mongoose: videos.find({}) { skip: 0, limit: 5, fields: undefined }

Setup 2:

Mongoose {


connections:
   [ NativeConnection {
       base: [Circular],
       collections: {},
       models: {},
       replica: false,
       hosts: null,
       host: 'localhost',
       port: 27017,
       user: undefined,
       pass: undefined,
       name: 'cdnserver',
       options: [Object],
       otherDbs: [],
       _readyState: 2,
       _closeCalled: false,
       _hasOpened: false,
       _listening: false,
       db: [Object] } ],
  plugins: [],
  models: {},
  modelSchemas: {},
  options: { pluralization: true } }
Server up on 80
1
1
Mongoose default connection disconnected
Mongoose default connection error: Error: connection closed

cat /var/log/mongodb/mongodb.log shows exactly the same output in both the machines.

Update 1: The setup started working properly out of blue and it stopped again. I am not able to figure out what is making this happen.

55597
  • 2,033
  • 1
  • 21
  • 40
  • Can you connect mongo from your terminal? Maybe you've enabled authentication on the new DB? – GilZ Dec 28 '16 at 12:37
  • @GilZ I am able to connect, there is not authentication set. plus when the system worked intermittently i did not change any authentication setup. – 55597 Dec 28 '16 at 12:38
  • Did you check mongodb server log usually in `/var/log/mongodb/mongodb.log` when your connection drop ? – Bertrand Martel Dec 29 '16 at 01:01
  • @BertrandMartel the logs files looks similar except for the changing pId. – 55597 Dec 29 '16 at 03:44

1 Answers1

0

I figured it out finally, the new setup was using a newer version of nodejs.

When I moved to 6.x from 7.x it worked fine. I guess the mongoose, node 7, mongodb versions didnt go well together.

55597
  • 2,033
  • 1
  • 21
  • 40