Akshay Vaghasiya
Akshay Vaghasiya

Reputation: 1637

Uncaught exception 'MongoConnectionException' with message 'Failed to connect to: localhost:27017

I have successfully installed the MongoDB in localhost. I have tried to connect to the MongoDB using PHP.

It throws the error like this.

Uncaught exception 'MongoConnectionException' with message 'Failed to connect to: 
localhost:27017: No connection could be made because the target machine actively 
refused it.

Here is my code.

$username = 'user';
$password = 'pass';

$m = new MongoClient("mongodb://user:pass@localhost");

If I start command prompt as admin and run the mongod command, then it connects (No error using upper code). But I know it must be done by coding. How to do it?

Upvotes: 0

Views: 2165

Answers (1)

Alex
Alex

Reputation: 21766

Unless you have installed Mongo as a service, you need to start a mongod process first before you will be able to connect to it using mongo. mongod is the actually database server, whereas mongo is the client. Check mongodb.org for instructions on how to run mongod as a service, instructions will differ depending on what platform you are using.

Upvotes: 1

Related Questions