Database – MongoDB Auth, can be authenticated in the shell but not via the command line

databasedatabase-administrationmongodb

I have an application that connects to a MongoDB database called discussions. I've created a user

Mongo shell:

> use discussions
switched to db discussions
> db.auth("discussions","XXXXXXXXX")
1
> show users
{
        "_id" : "discussions.discussions",
        "user" : "discussions",
        "db" : "discussions",
        "roles" : [
                {
                        "role" : "dbOwner",
                        "db" : "discussions"
                }
        ]
}

So that is to confirm that I do have one account on the database and it's a dbOwner. According to the docs it has READ, WRITE, etc privileges.

The configuration file has the property "auth = true" enabled, and the service has been bounced since the change, more than once.

However, the problem is that when I attempt to connect to the database from outside of the shell I always get the error:

mongo  discussions -u 'discussions' -p 'XXXXXXXXX'
MongoDB shell version: 2.6.3
connecting to: discussion
2014-08-05T01:00:39.026+0400 Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18 } at src/mongo/shell/db.js:1210
exception: login failed

I've seen questions on Stackoverflow about this and they all had something to do with the quotes used around the username and password… but changing the single quote to a double quote didn't yield a better result.

Best Answer

Maybe I'm wrong but the correct form is like this:

mongo --port 27017 -u manager -p 12345678 --authenticationDatabase admin

You can read about this in mongo documentation:

http://docs.mongodb.org/manual/tutorial/add-user-to-database/

Related Topic