title: MongoError: Econnrefused
body: <p>I'm just learning MongoDB and Node.js. I'm doing this really simple exercise from a video in which I try to connect my node.js app to my MongoDB, and I'm getting this error: </p>
  
  <p><em>{ [MongoError: connect ECONNREFUSED] name: 'MongoError', message: 'connect ECONNREFUSED' }</em></p>
  
  <p>Before all that, I've configured a database on mongo, and have tested running some commands on terminal:</p>
  
  <pre><code>$mongod (everything ok)
  $mongo (everything ok)
  </code></pre>
  
  <p>..and then I've created a database called 'products' and added some products to it. When I hit:</p>
  
  <pre><code>db.products.find()
  </code></pre>
  
  <p>It returns:</p>
  
  <pre><code>{
  "_id": ObjectId("55b2b27d7f9f84490b8a9170"),
    "name": "beer",
    "description": "belgian craft beer",
    "price": 20
  }
  {
    "_id": ObjectId("55b2b2c27f9f84490b8a9171"),
    "name": "whisky",
    "description": "scotch blended 18 years old",
    "price": 150
  }
  {
    "_id": ObjectId("55b2b3e47f9f84490b8a9172"),
    "name": "cachaça",
    "description": "typical brazilian cachaça",
    "price": 10
  }
  {
    "_id": ObjectId("55b2b4087f9f84490b8a9173"),
    "name": "water",
    "description": "water from the swiss alps",
    "price": 5
  }
  {
    "_id": ObjectId("55b2b2617f9f84490b8a916f"),
    "name": "wine",
    "description": "red porto wine",
    "price": 80,
    "tags": [
      "porto",
      "sweet",
      "wine"
    ]
  }
  </code></pre>
  
  <p><strong>My app.js:</strong></p>
  
  <pre><code>var mongoose = require('mongoose');
  mongoose.connect('mongodb://localhost/products');
  
  var db = mongoose.connection;
  
  db.on('error', function(err){
      console.log('Erro de conexao.', err)
  });
  db.on('open', function () {
      console.log('Conexão estabelecida.')
  });
  db.on('disconnected', function(err){
      console.log('Desconectado')
  });
  </code></pre>
  
  <p><strong>And my package.json:</strong></p>
  
  <pre><code>{
    "name": "aula-mongoose",
    "version": "0.0.1",
    "description": "aula sobre mongoose",
    "main": "app.js",
    "scripts": {
      "test": "echo \"Error: no test specified\" &amp;&amp; exit 1"
    },
    "keywords": [
      "node.js",
      "mongodb"
    ],
    "author": "Guga",
    "license": "WTFPL",
    "dependencies": {
      "mongoose": "^4.1.0"
    }
  }
  </code></pre>
  
  <p>I've read lots of posts about this, even threads here. But I couldn't find out what is wrong here. </p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Accidentally I tried to connect to $mongod while it was already running and I was prompted with this error: </p>
  
  <p><em>errno:48 Address already in use for socket: 0.0.0.0:27017</em> </p>
  
  <p>So I changed this line in my app.js to:</p>
  
  <pre><code>mongoose.connect('mongodb://0.0.0.0:27017/products');
  </code></pre>
  
  <p>And it worked. But I have no idea why. Anyone?</p>