title: Mongoose connection error with cluster
body: <p>I'm having an issue with node.js/cluster and mongoose connection.
  I'm not sure I can connect multiple fork of my web server on the same db using mongoose, is it ?</p>
  
  <p>There is a snippet of my code:</p>
  
  <pre><code>const express = require('express');
  const app = express();
  const bodyParser = require('body-parser');
  const helmet = require('helmet');
  const mongoose = require('mongoose');
  const http = require('http');
  
  if (cluster.isMaster) {
    const cpuCount = require('os').cpus();
    cpuCount.forEach((cpu) =&gt; {
      cluster.fork(); // fork web server
    });
    cluster.on('exit', (worker, code, signal) =&gt; {
      ... // log
      cluster.fork(); // on dying worker, respawn
    });
  } else {
  
    //express middleware
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: false }));
    app.use(helmet());
    ... // express and global config
  
    // The mongoose connection dit not work with cluster
    mongoose.connect(__DB_URL__); // where __DB_URL__ = 'mongodb://database/test'
  
    ... //server config
    const server = http.createServer(app);
    server.listen
  }
  </code></pre>
  
  <p>I'm able to run this server without using <code>cluster</code> (without the first part of the code) <em>(Or with <code>cluster</code> but without mongoose...)</em>, and when I run this server using <code>cluster</code>; workers didn't crash but mongoose connection !!</p>
  
  <p>This is the json error:</p>
  
  <pre><code>{ MongoError: failed to connect to server [database:27017] on first connect
      at Pool.&lt;anonymous&gt; (/Users/...)
      ... // long path error
    name: 'MongoError',
    message: 'failed to connect to server [database:27017] on first connect' }
  </code></pre>
  
  <p>I am really able to use mongoose in each fork of cluster or there is another solution ?</p>