python-使用readWrite的mongoengine用户无法创建索引
作者:互联网
我正在运行python 2.7和mongodb 2.6.5.
我无法让mongoengine连接并发出请求,但是我可以与用户登录mongo并发出相同的请求.
我像这样在mongodb中创建了3个用户(密码不是空白,但我已将其删除):
$mongo campaigns
db.createUser({"user": "admin", "pwd": "", "roles": [
{"role": "dbOwner", "db":"campaigns" },
{"role": "dbOwner", "db":"admin" },
{"role": "readAnyDatabase", "db": "admin" },
"readWrite" ]},
{ "w": "majority" , "wtimeout": 5000 })
db.createUser({"user": "look", "pwd": "", "roles": ["read"]})
db.createUser({ "user": "write", "pwd": "", "roles": ["readWrite"]})
然后,我使用python和mongoengine连接到我的本地mongo(尝试使用用户写入和用户admin):
self.db = connect(setting.DB["db"], "db", host=setting.DB["urli"],
port=setting.DB["port"], username=setting.DB["user"],
password=setting.DB["pwd"])
set.DB的打印(同样pwd不是空白,我只是不写在那里).
{'urli': 'mongodb://localhost/', 'pwd':, 'db': 'campaigns', 'user': 'melu-write', 'port': 27017}
那里一切都很好,但是当我在python中尝试类似的东西时:
l = [x.name for x in campaign.objects.only("name").all()]
MongoEngine给我这个错误:
OperationFailure: database error: not authorized for query on campaigns.campaigns
当我像这样从mongo登录时:
mongo campaigns -u write -p
> password:
> db.campaigns.find()
{ "_id":....
错误的痕迹很少:
pymongo.errors.OperationFailure: command SON([('createIndexes', u'campaigns'), ('indexes', [{'name': u'offers.product_name_1', 'key': SON([('offers.product_name', 1)]), 'unique': True, 'background': False, 'sparse': False, 'dropDups': False}])])
on namespace campaigns.$cmd failed: not authorized on campaigns to execute command { createIndexes: "campaigns", indexes: [ { name: "offers.product_name_1", key: { offers.product_name: 1 }, unique: true, background: false, sparse: false, dropDups: false } ] }
并在mongo日志上出现相同的错误:
2015-03-05T13:43:33.696+0000 [conn14] Unauthorized not authorized on campaigns to execute command { createIndexes: "campaigns", indexes: [ { name: "offers.product_name_1", key: { offers.product_name: 1 }, unique: true, background: false, sparse: false, dropDups: false } ] }
您对发生的事情有想法吗,为什么我的具有readWrite角色的用户无法执行简单的findAll,为什么我不能编写索引?
(以及为什么我们需要编写索引以进行简单的查找查询的地狱?)
解决方法:
我在pymongo的bugtracker上发布后发现了麻烦.
我使用的版本不是最新的,但最糟糕的是,mongoengine无法正确地对db进行身份验证.
不要像我一样在connect上进行身份验证,但是rathen做这样的事情:
db = connect("magic", "alias", host="my.url.com", port=port)
db["magic"].authenticate("user", password="pwd")
您可以尝试在urli中提供此信息,但我认为mongoengine会严重使用它们.
谢谢你所做的一切.
标签:mongodb,authentication,pymongo,python,mongoengine 来源: https://codeday.me/bug/20191120/2045546.html