python – 未经授权写入MongoDB
作者:互联网
我试图通过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,我可以写入数据库.
解决方法:
如https://jira.mongodb.org/browse/PYTHON-855所述,问题可能出在连接字符串中,请尝试更改:
_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,mongoengine 来源: https://codeday.me/bug/20190628/1312811.html