gpt4 book ai didi

python - 具有 readWrite 的 mongoengine 用户无法创建索引

转载 作者:可可西里 更新时间:2023-11-01 09:36:13 26 4
gpt4 key购买 nike

我正在运行 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(尝试使用用户写入和用户管理):

self.db = connect(setting.DB["db"], "db", host=setting.DB["urli"],
port=setting.DB["port"], username=setting.DB["user"],
password=setting.DB["pwd"])

setting.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 } ] }

您对发生的事情有什么想法吗,为什么我的具有读写角色的用户不能执行简单的 findAll,为什么我不能写索引?(以及为什么我们需要编写索引来进行简单的查找查询?)

最佳答案

我在 pymongo 的 bugtracker 上发帖后发现了问题。

我使用的版本不是最新的,但最糟糕的是,mongoengine 没有正确地向 db 进行身份验证。不要像我一样在连接时进行身份验证,而是做这样的事情:

db = connect("magic", "alias", host="my.url.com", port=port)
db["magic"].authenticate("user", password="pwd")

您可以尝试在 url 中提供此信息,但我认为 mongoengine 会错误地使用它们。

谢谢你所做的一切。

关于python - 具有 readWrite 的 mongoengine 用户无法创建索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28878147/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com