I just implemented auth for mongodb, There is a user ‘admin’ in db ‘admin’ and ‘appadmin’ in db ‘mydb’ all working fine. below are my db auth settings:
use admin
db.auth(‘’, ‘’)
db.getUsers()
[
{
"_id" : "admin.admin",
"user" : "admin",
"db" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
},
{
"role" : "clusterMonitor",
"db" : "admin"
}
]
}
]
use mydb
db.auth()
db.getUsers()
{
"_id" : "mydb.appadmin",
"user" : "mydb",
"db" : "mydb",
"roles" : [
{
"role" : "readWrite",
"db" : "mydb"
},
{
"role" : "userAdmin",
"db" : "mydb"
}
]
}
].
If I run the following,
mongostat --username=admin --password=mypassword --authenticationDatabase=admin
insert query update delete getmore command flushes mapped vsize res faults qr|qw ar|aw netIn netOut conn time
*0 *0 *0 *0 0 1|0 0 240.0M 678.0M 91.0M 0 0|0 0|0 79b 10k 1 11:49:18
*0 *0 *0 *0 0 1|0 0 240.0M 678.0M 91.0M 0 0|0 0|0 79b 10k 1 11:49:19
*0 *0 *0 *0 0 1|0 0 240.0M 678.0M 91.0M 0 0|0 0|0 79b 10k 1 11:49:20
But when I run
* mongostat --username=appadmin --password=mypassword --authenticationDatabase=mydb
Failed: not authorized on admin to execute command { serverStatus: 1, recordStats: 0 },
So I tried to add role ‘clusterMonitor’ in mydb.
db.updateUser(“appadmin”, {roles: [{role: "readWrite", db: “mydb”}, {role: "userAdmin", db: “mydb”}, {role: "clusterMonitor", db: “mydb”}]})
E QUERY Error: Updating user failed: No role named clusterMonitor@mydb.
What is the best way to do mongostat in a auth enabled mongoldb? Please help me to fix the issue or suggest best auth settings. Note: my mongodb version 3.0.6
To use mongostat , keep your current MongoDB shell connection, and open another terminal window to access your server shell. In the second server shell, run the mongostat command: mongostat -u AdminSammy --authenticationDatabase admin.
mongostat. This command checks the status of all running mongod instances and return counters of database operations. These counters include inserts, queries, updates, deletes, and cursors.
mongotop provides a method to track the amount of time a MongoDB instance mongod spends reading and writing data. mongotop provides statistics on a per-collection level. By default, mongotop returns values every second. Run mongotop from the system command line, not the mongo shell.
To view the statistics for a collection, including the data size, use the db. collection. stats() method from the mongo shell. Q 22 - Which of the following commands can cause the database to be locked?
finally I got the solution, I add more roles to admin db,
use admin
db.getUsers()
{
"_id" : "admin.admin",
"user" : "admin",
"db" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
},
{
"role" : "readWriteAnyDatabase",
"db" : "admin"
},
{
"role" : "dbAdminAnyDatabase",
"db" : "admin"
},
{
"role" : "clusterAdmin",
"db" : "admin"
},
{
"role" : "clusterMonitor",
"db" : "admin"
}
]
}
]
and run mongostat --username=admin --password=mypassword --authenticationDatabase=admin, fix the issues
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With