MongoDB works only on user is assigned to admin database
0
votes
0
answers
327
views
I am using mongo db with authentication and it is working as expected when I map the user to the admin database like below,
use admin;
db.getUsers();
{
"_id" : "admin.test1",
"userId" : UUID("abee0e0c-1d0b-44fa-84ad-594159b66aab"),
"user" : "test1",
"db" : "admin",
"roles" : [
{
"role" : "readWrite",
"db" : "test_database"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
I am able to connect to the database using '
test1
' user and access the 'test_database
'
but when I create user with actual test_database
, It is not allowing me to connect,
use test_database;
db.getUsers();
{
"_id" : "test_database.test2",
"userId" : UUID("abee0e0c-1d0b-44fa-84ad-594159b66aab"),
"user" : "test2",
"db" : "test_database",
"roles" : [
{
"role" : "readWrite",
"db" : "test_database"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
When I connect using the 'test1
' user to connect 'test_database', I am able to connect and It is working as expected (weird how the admin database access helps 'test_database' though) - why?
but when I connect using the user 'test2
' it fails with the following error
**pymongo.errors.OperationFailure: Authentication failed.**
Asked by Coder
(71 rep)
Mar 16, 2020, 06:40 PM