简体   繁体   中英

Can't execute mongo command from C# driver when I permisions

I just added user authentication to my monngodb replica set and now I'm getting a MongoCommandException.

Command 'listDatabases' failed: not authorized on admin to execute command { listDatabases: 1 } (response: { "ok" : 0.0, "errmsg" : "not authorized on admin to execute command { listDatabases: 1 }", "code" : 13 })

What's odd is I can log into mongo in the terminal with the same user and it works just fine.

Here is my user:

....    
{
    "_id" : "admin.rulesEngineDBAdmin",
    "user" : "rulesEngineDBAdmin",
    "db" : "admin",
    "roles" : [
        {
            "role" : "dbOwner",
            "db" : "rules-engine"
        },
        {
            "role" : "clusterAdmin",
            "db" : "admin"
        }
    ]
}
....

According to http://docs.mongodb.org/manual/reference/built-in-roles/ :

clusterAdmin combines the privileges granted by the clusterManager, clusterMonitor,
andhostManager... clusterMonitor provides access to the listDatabases command.

Here is when I run it from the terminal:

rules_engine:PRIMARY> db.auth("rulesEngineDBAdmin", "rulesEngineDBAdmin");
1
rules_engine:PRIMARY> db.runCommand( { listDatabases: 1 } )

{
    "databases" : [
            {
                    "name" : "admin",
                    "sizeOnDisk" : 83886080,
                    "empty" : false
            },
            {
                    "name" : "local",
                    "sizeOnDisk" : 1157627904,
                    "empty" : false
            }
    ],
    "totalSize" : 1241513984,
    "ok" : 1
}

But when I run this code:

doesRulesEngineDbExists = Server.DatabaseExists(databaseName);

With this connection string:

mongodb://rulesEngineDBAdmin:rulesEngineDBAdmin@...

I get this MongoCommandException:

Command 'listDatabases' failed: not authorized on admin to execute command
{ listDatabases: 1 }
( response: 
    { "ok" : 0.0, "errmsg" : "not authorized on admin to execute command 
        { listDatabases: 1 }", "code" : 13 
    }
)

If anyone has encountered this before, I'd love to know what's going on, and what I can do to fix it.

To answer my own question:

This ended up being a mistake on my part. I hadn't added "authorization: enabled" to my configuration file under the security section, because that part was not mentioned in the tutorial that i was following (All i had was the keyfile). So it looked like I was able to connect, but I couldn't execute any commands.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM