4

I am trying to create a db user from Python with pymongo. But I keep getting pymongo.errors.OperationFailure: CMD_NOT_ALLOWED: createUser error

client = MongoClient("mongodb+srv://username:[email protected]/admin")
db = client["testDB"]
db.command("createUser", "user", pwd="password")

am I missing something? Thanks.

Note: my db is on Mongo Atlas don't know if this changes anything.

2 Answers 2

5

I asked this problem to Atlas customer support and they gave me this answer hope it helps other people who tries the same thing. In short you cannot add user to Atlas with pymongo that is why it keeps giving error.

Hello, User management in MongoDB Atlas can only be accomplished by creating MongoDB Users in the following ways; via the Web UI or with the API All MongoDB users for Atlas are associated with the admin database; i.e. their authentication database is admin. The authentication database does not determine the user’s roles. Please note that the Admin database is for authentication purposes and is not meant to hold customer data.

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

1 Comment

so how do we do it with the API?
0

UPDATED ANSWER:
Try this.

client.testDB.add_user('user', 'password', roles=[{
    'role':'root',
    'db':'testDB',
}])

4 Comments

this time it gives pymongo.errors.OperationFailure: not authorized on admin to execute command
Please try out my updated answer and see if it works!
still same error. by the my db is on mongo atlas I don't know if it changes anything.
This answer is no longer correct. add_user() was deprecated in 3.6 and removed in 4.0. The replacement method command() is only available for M10 and larger clusters.

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.