title: MongooseServerSelectionError: connect ECONNREFUSED
body: <p><strong>I'm trying to connect my remote EC2 mongodb but it's saying MongooseServerSelectionError: connect ECONNREFUSED awsip:27017</strong></p>
  <p>In aws mongodb config file I change bindIp to 0.0.0.0 but I didn't change the security section.</p>
  <p>This is an express application I have try to connect 2 way with aws mongodb and the both way I got the same error, here is my db setup:</p>
  <pre><code>Setup: 01
  
  const DATABASE_URL = `mongodb://${cfg.dbUser}:${cfg.dbPass}@${cfg.dbHost}:${cfg.dbPort}/${cfg.dbName}`
  
  server.listen(port, () =&gt; {
    mongoose.connect(DATABASE_URL,
      {
        // auth: {
        //   user: cfg.dbUser,
        //   password: cfg.dbPass
        // },
        useNewUrlParser: true,
        useUnifiedTopology: true,
      })
      .then(() =&gt; {
        console.log(&quot;[  ] AWS MongoDB database connected.&quot;);
        console.log(`[ Server ] waiting on: ${cfg.serverHost}:${port}`);
      })
      .catch(err =&gt; {
        console.log(
          &quot;[  ] Database connection error&quot;,
          { Error: err }
        );
      })
  });
  
  
  Setup: 02
  
  const DATABASE_URL = `mongodb://${cfg.dbHost}:${cfg.dbPort}/${cfg.dbName}`
  server.listen(port, () =&gt; {
    mongoose.connect(DATABASE_URL,
      {
        auth: {
          user: cfg.dbUser,
          password: cfg.dbPass
        },
        useNewUrlParser: true,
        useUnifiedTopology: true,
      })
      .then(() =&gt; {
        console.log(&quot;[  ] AWS MongoDB database connected.&quot;);
        console.log(`[ Server ] waiting on: ${cfg.serverHost}:${port}`);
      })
      .catch(err =&gt; {
        console.log(
          &quot;[  ] Database connection error&quot;,
          { Error: err }
        );
      })
  });
  </code></pre>
  <p><strong>I have created database in aws and all the database user permission credential.</strong></p>
  <p><em><strong>I'm using dotenv to load all the aws credintial</strong></em></p>
  <p>Also the same problem when I'm trying to connect with MongoDB-Compass from my machine.</p>
  <pre><code>connect ECONNREFUSED awsip:27017
  </code></pre>
  <p>Here is my terminal picture
  <a href="https://i.stack.imgur.com/zyqdb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zyqdb.png" alt="enter image description here" /></a></p>
  <p><em><strong>Hope expert will help me.</strong></em></p>