0

I can not connect to MongoDb using PHP, the error I am getting is

Failed to connect to: 127.0.0.1:27017: Authentication failed on database 'abc' with username 'admin': auth fails

The code that I use to connect is:

<?php
try {
    $server = new Mongo("mongodb://admin:[email protected]:27017/abc");
} catch (Exception $e) {
    echo $e->getMessage();  
}
?>

I have also installed RockMongo which can connect to the database fine.

The control panel that I am using is: ispconfig.org, I have added port 27017 to the firewall via the control panel so it's passing trough.

7
  • Firewall should not be an issue. It's local. Are you sure those user credentials are valid for that database? Is this possibly a new database? Commented Feb 24, 2014 at 0:22
  • It is a new installed MongoDB server on which I have created a new database with no user so the default admin/admin should work. Even if I create a user for the database I get the same error. Commented Feb 24, 2014 at 0:27
  • Default install? There is no default user. Authentication is off by default. Have you connected at all and set up authentication? Commented Feb 24, 2014 at 0:30
  • I've installed MongoDB on ubuntu, did not change any settings. Upload rock mongo (a phpmyadmin like interface for mongodb) which can connect. Created a new database (rock mongo can connect to this). Made PHP script (as mentioned above) which can not connect. Commented Feb 24, 2014 at 7:47
  • 1
    Yup. So the 'admin':'admin' is the default user and password for rockmongo interface, not mongodb. MongoDb has no authentication turned on by default you just connect as in mongodb://localhost/abc Commented Feb 24, 2014 at 8:20

1 Answer 1

2

So in a nutshell you seem to confusing the default credentials for you RockMongo UI with credentials to connect to MongoDB.

By default on installation, MongoDB has no Authentication turned on. You can configure authentication if you so desire, and even add that configuration to your Rockmongo Setup so that it can work on a privileged user.

All details are in the links provided.

For now just connect as follows:

mongodb://<hostname>/<database>

Provided you have access from your or another machine.

Sign up to request clarification or add additional context in comments.

1 Comment

You where right, feel a bit silly, adding a user to the system.users for the specific database fixed the authentication issue I had, will have a look at the link you provided. Thanks for the feedback!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.