I created a user, "test_user" on mongodb for db "test_db" and granted the readWrite role as follows
db.createUser(
{
user : "test_user",
pwd : "password",
roles : [
{
role : "readWrite",
db : "test_db"
}
]
}
)
Mongo says Successfully added user
All is well, I connect to my db with RoboMongo and am able to create and list collections.
But when I connect to mongo using shell from my machine as follows
mongo -u 'test_user' --authenticationDatabase 'test_db' -p 'password'
I am not able to list or create collections. Mongo says
E QUERY [thread1] Error: listCollections failed: {
"ok" : 0,
"errmsg" : "not authorized on test to execute command { listCollections: 1.0, filter: {} }",
"code" : 13,
"codeName" : "Unauthorized"
}
when I type show collections in shell.
What am I missing ?
Strange, I had to type
use test_dbin mongo shell to select the database before trying to list collections or query. Even though I had--authenticationDatabaseparam in my login script, mongo does not automatically choose the mentioned db for you. You have to do it yourself.