Can't connect to a mongodb database that has a user/pass on local dev windows

829 views
Skip to first unread message

Zelf1995

unread,
Jul 22, 2014, 1:27:42 PM7/22/14
to mongod...@googlegroups.com

How do I properly connect via a php script to a local windows mongodb database, which has a username/password setup for it? It seems pretty simple, but the following code does not work.

  1. Using robomongo as my gui admin I created a database called "ezr". Then I created a user called "ezr" with a password of "test" and gave this user all permissions. 
  2. Then I restarted mongod.exe --auth. Remember I'm on windows. From the docs this should allow for mongodb to accept connection with auth info.
  3. PHP has the mongo extension installed and I've tested connecting to my local mongodb to a database without auth info and it works just fine. I can create collections, insert documents, etc.
  4. Here's my php code: 
    1. $conn = new MongoClient('mongodb://ezr:test@localhost/ezr');

    2. Here is the exception error message php gives me: Failed to connect to: localhost:27017: Authentication failed on database 'ezr' with username 'ezr': auth failed
  5. From the command liine when I connect to mongo, these are the results:
    mongo.exe ezr -u ezr -p

    MongoDB shell version: 2.6.3
    Enter password: {I enter "test" here}
    connecting to: ezr
    2014-07-21T16:16:05.649-0700 Error: 18 { ok: 0.0, errmsg: "auth failed", code: 1 8 } at src/mongo/shell/db.js:1210
    exception: login failed

Again, seems so simple. In mysql it is beyond easy to setup a user and roles, what am I missing here with mongodb on a local windows dev setup when trying to setup a user on a database?


Zelf1995

unread,
Jul 22, 2014, 1:44:59 PM7/22/14
to mongod...@googlegroups.com
In the mongodb documentation it states:
"Prerequisites

To create a user on a system that uses authentication, you must authenticate as a user administrator. If you have not yet created a user administrator, do so as described in Create a User Administrator."
After creating the administrator user on the admin database, now auth works just fine. Hopefully someone else will come across this and it will be helpful.
Reply all
Reply to author
Forward
0 new messages