Add user to mongodb via python

I want to be able to add users to MongoDB so that we can automate installations of MongoDB with already processed authentication. I can successfully add users using pymongo that are read-only, or are dbOwner, by doing this:

from pymongo import MongoClient

client = MongoClient('localhost:27017')   
client.admin.authenticate('siteRootAdmin', 'Test123')
client.testdb.add_user('newTestUser', 'Test123', True)

but when I do this following code to indicate roles, it fails:

from pymongo import MongoClient

client = MongoClient('localhost:27017')
client.admin.authenticate('siteRootAdmin', 'Test123')
client.testdb.add_user('newTestUser', 'Test123', False, 'readWrite')

with an error:

line 10, in <module>
    client.admin.add_user('newTestUser', 'Test123', False, 'readWrite')
TypeError: add_user() takes at most 4 arguments (5 given)

In documents, this means that you can have optional fields for a custom document, such as other roles. Has anyone been able to install them correctly? Namely, I want to have readWrite service accounts that can add data to collections, but do not have full dbOwner privileges.

+4
1

:

client.testdb.add_user('newTestUser', 'Test123', roles=[{'role':'readWrite','db':'testdb'}])

: "", (read_only).

+6

Source: https://habr.com/ru/post/1547985/


All Articles