0

I am using a hosted MongoDB (v6 @ Digital Ocean), and they provided an admin user that I confirmed as valid to create databases, collections, and documents. I want to add a user that can only 'read' and 'write' in a specific database.

I have found this answer that points to the following solution:

client.testdb.command(
    'createUser', 'newUser', pwd='Test123',
    roles=[{'role': 'readWrite', 'db': 'testdb'}]
)

I can execute such a command using my admin db client and get an ok response: {'ok': 1.0, '$clusterTime': {'clusterTime': ..., 'signature': {...}, 'operationTime':...}. Although that is the case, if I query the database for a list of users (i.e., client.testdb.command('usersInfo')), I get an empty list.

Moreover, if I try to use such newUser to access the testdb, I get an Authentication failed error. (i.e., I tried to count_documents({}) -- which works well with my admin client.)

Putting together the documentation for the PyMongo command database call and the MongoDB createUser, the above call should indeed work. Can anyone see what I am doing wrong?

2
  • 2
    "hosted MongoDB" meaning Atlas? If so you can't create database users via that command there. See the important note here in the documentation, emphasis added: "You must use the Atlas CLI , Atlas Administration API , Atlas UI, or a supported integration to add, modify, or delete database users on Atlas database deployments. Otherwise, Atlas rolls back any user modifications." Commented Jun 8, 2023 at 3:59
  • That is a great point! I am using Digital Ocean, and your comment made me check their documentation, which says You cannot add users to a cluster using the mongo shell. :( Commented Jun 8, 2023 at 19:32

1 Answer 1

0

@user20042973 comment made me realize I should've checked Digital Ocean MongoDB User management documentation.

There they say: You cannot add users to a cluster using the mongo shell. Users must be added to the cluster using the DigitalOcean Control Panel, API, or CLI. 😖

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.