title: Uncaught exception 'MongoConnectionException' with message 'Failed to connect to: localhost:27017
body: <p>I have successfully installed the MongoDB in localhost. I have tried to connect to the MongoDB using PHP.</p>
  
  <p>It throws the error like this.</p>
  
  <pre><code>Uncaught exception 'MongoConnectionException' with message 'Failed to connect to: localhost:27017: No connection could be made because the target machine actively refused it.
  </code></pre>
  
  <p>Here is my code.</p>
  
  <pre><code>$username = 'user';
  $password = 'pass';
  
  $m = new MongoClient("mongodb://user:pass@localhost");
  </code></pre>
  
  <p>If I start command prompt as admin and run the <code>mongod</code> command, then it connects (No error using upper code). But I know it must be done by coding. So If anyone knows the solution, it will be appreciated.</p>