简体   繁体   中英

Cannot connect to mongodb usign mongoengine with read(only) access

I have working CLI tool created with mongoengine running on linux environment

MongoDB = 3.4.1
Python = 2.7.5
PyMongo = 3.4.0
MongoEngine = 0.11.0

I am connecting to database with information in .ini file that looks like so:

[DATABASE]
uri=mongodb://%(user)s:%(password)s@%(host)s/%(dbname)s
dbname=myapp
user=
host=localhost
password=

In Python:

DB_CONN = mongoengine.connect(conf['dbname'], host=conf['uri'])

There are currently two users in database. User usrRO that have read role and user usrRW that have readWrite role. When connecting to db using user usrRW name and password in .ini file everything works. But connecting with user usrRO credentials (user usrRO can read data from mongo CLI interface) leads to:

pymongo.errors.OpeartionFailure: not authorized on myapp to execute command 
{ createIndexes: <collection_name>,
  indexes: [ { unique: true, 
               backgroung: false, 
               sparse: false, 
               key: { name: 1 },
               name: "name_1" } ],
  writeConcern: {} }

Is there any way to use users usrRO credentials, or any other way to connect to database with ready only privileges using mongoengine ?

I found the answer in the mongo-engine gitlab issues. Simply add

'auto_create_index': False

to your objects meta. This might not work for complex object structures.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM