R
R
Rustem Vorozheikin2015-05-26 14:50:16
MongoDB
Rustem Vorozheikin, 2015-05-26 14:50:16

How does authorization work in Mongo?

Hello.
The server has mongo version 2.4.9 installed.
By default, the auth=true line in the config is commented out.
Users created in mongo:

> use admin
switched to db admin
> show users
{
  "_id" : ObjectId("55631c4ced1bdfc880fda8e0"),
  "user" : "admin",
  "pwd" : "f306c561a12b1be8478e4cc3b460433a",
  "roles" : [
    "userAdminAnyDatabase",
    "dbAdminAnyDatabase",
    "readWriteAnyDatabase"
  ]
}
{
  "_id" : ObjectId("5563371cb7537b3e14bdfb4b"),
  "user" : "rUser",
  "pwd" : "eee18df57c91293310dbf78649955a84",
  "roles" : [
    "readAnyDatabase"
  ]
}

When I remove the hash mark from the "#auth=true" line in the config and "auth=true" remains, then when I call the "mongo" command, I enter the mongo console, but I don't see the database by itself (I get an error on the "show dbs" command " ...listDatabases failed:{ "ok" : 0, "errmsg" : "unauthorized" }...").
But why does this error occur:
> use admin
switched to db admin
> db.auth('admin', 'password')
1
> show dbs
Tue May 26 19:48:05.074 listDatabases failed:{ "ok" : 0, "errmsg" : "unauthorized" } at src/mongo/shell/mongo.js:46

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lega, 2015-05-27
@lega

Works for me:
Started the server:
I add a user:

$ mongo --port 27020
MongoDB shell version: 2.6.10
connecting to: 127.0.0.1:27020/test
> show dbs
admin  (empty)
local  0.031GB
> use admin
switched to db admin
> db.addUser({user:'name', pwd:'password', roles:["userAdminAnyDatabase","dbAdminAnyDatabase", "readWriteAnyDatabase"]})
WARNING: The 'addUser' shell helper is DEPRECATED. Please use 'createUser' instead
Successfully added user: {
        "user" : "name",
        "roles" : [
                "userAdminAnyDatabase",
                "dbAdminAnyDatabase",
                "readWriteAnyDatabase"
        ]
}
> exit
bye

Checking:
$ mongo --port 27020
MongoDB shell version: 2.6.10
connecting to: 127.0.0.1:27020/test
> show dbs
2015-05-27T16:35:46.071+0500 listDatabases failed:{
        "ok" : 0,
        "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
        "code" : 13
} at src/mongo/shell/mongo.js:47
> use admin
switched to db admin
> db.auth('name', 'password')
1
> show dbs
admin  0.031GB
local  0.031GB

Those. at connection there are no rights before authorization.
Alternatively, you can try updating to 2.6, there are not many changes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question