我想通过MongoEngine给Mongo写信。
在我的/etc/mongod.conf中我有auth=true
我正在连接的用户是,

{
        "user" : "dbexample",
        "roles" : [
                {
                        "role" : "dbOwner",
                        "db" : "dbexample"
                }
        ]
}

不管我怎么想,
>>> john = Employee(name="John Doe", age=25)
>>> john.save()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/mongoengine/document.py", line 394, in save
    raise OperationError(message % unicode(err))
OperationError: Could not save document (command SON([('insert', u'employee'), ('ordered', True), ('documents', [SON([('name', u'John Doe'), ('age', 25), ('_id', ObjectId('56bc94d1f1d55600445bd085'))])]), ('writeConcern', {'w': 1})]) on namespace dbexample.$cmd failed: not authorized on dbexample to execute command { insert: "employee", ordered: true, documents: [ { name: "John Doe", age: 25, _id: ObjectId('56bc94d1f1d55600445bd085') } ], writeConcern: { w: 1 } })

编辑:
这是django mongo配置,
_MONGODB_USER = 'dbexample'
_MONGODB_PASSWD = '####'
_MONGODB_HOST = '172.17.0.1'
_MONGODB_NAME = 'dbexample'
_MONGODB_DATABASE_HOST = 'mongodb://%s/%s' % (_MONGODB_HOST, _MONGODB_NAME)

注意:如果我关闭auth,我可以写信给db。

最佳答案

如本文所述,问题可能出在连接字符串中,请尝试更改:

_MONGODB_USER = 'dbexample'
_MONGODB_PASSWD = '####'
_MONGODB_HOST = '172.17.0.1'
_MONGODB_NAME = 'dbexample'
_MONGODB_DATABASE_HOST = 'mongodb://%s:%s@%s/%s' % (
           _MONGODB_USER, _MONGODB_PASSWD, _MONGODB_HOST, _MONGODB_NAME)

关于python - 未经授权可以写入MongoDB,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35340992/

10-12 00:46