0

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.

  • 1
    How do you connect? Usually in connection string you specify the authentication database (which defaults to "admin") – Wernfried Domscheit Mar 17 at 7:57
  • 1
    Actually I don't know any reason to create users anywhere apart from the admin database. The only situation I can imagine is when you like to provide users some kind of "mongo playground" where they can use their dedicated database as they like. – Wernfried Domscheit Mar 17 at 8:00
  • @WernfriedDomscheit, adding the auth_source in the client connection solved the issue – Harry Mar 17 at 8:03
  • Thanks for your time @WernfriedDomscheit – Harry Mar 17 at 8:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.