How to resolve the error "not authorized on database name to execute command " when test the MongoDB connection?
book
Article ID: KB0079836
calendar_today
Updated On:
Products
Versions
TIBCO ActiveMatrix BusinessWorks Plug-in for MongoDB
All
Description
By testing MongoDB connection it threw the following error. ===== The full response is { "ok" : 0.0, "errmsg" : "not authorized on Database Name to execute command { dbstats: 1 }", "code" : 13, "codeName" : "Unauthorized" } ===== This error occurred due to the user not being granted the role "read" or "readWrite" .
Issue/Introduction
By testing the MongoDB shared connection resource it threw the error "not authorized on database name to execute command "
Environment
ALL Os
Resolution
To resolve this error follow the steps below.
1). Start the mongod instance using the command: $ mongod --auth
2). Start the mongo shell and create an administrator in the admin database: $ mongo > use admin > db.createUser( { user: "User", pwd: "PW", roles: [ { role: "userAdminAnyDatabase", db: "DB name" } ] } )
3). Executing the command db.stats()and will get an error "not authorized on DB name to execute command" > db.stats() { "ok" : 0, "errmsg" : "not authorized on admin to execute command { dbstats: 1.0, scale: undefined }", "code" : 13, "codeName" : "Unauthorized" }
4). To resolve this error execute the following command. > db.auth("User", "PW") > db.grantRolesToUser("User", [ { role: "read", db: "DB name" } ])
5).To verify the role given to the user, execute the command > db.stats() will get a successful result.
6). Configure the MongoDB connection and test connection.Then connection will be successful.