gpt4 book ai didi

python - Mongoengine - 命令查找需要身份验证

转载 作者:行者123 更新时间:2023-12-01 08:17:28 29 4
gpt4 key购买 nike

我刚开始使用 Mongoengine 和经过身份验证的 MongoDB。

我有一个名为“gambit_test”的 Mongo 数据库。我已经启用了身份验证,并且我有一个正确的用户,我可以通过身份验证进入数据库以执行读取和写入。

我在 Mongo 客户端中使用以下命令创建了“gambit_test”用户:

use gambit_test
db.createUser({
user: "gambit_admin",
pwd: "xxxxxx",
roles:[{role: "userAdmin" , db:"admin"}]
})

当我登录 MongoDB 查看用户状态时,我使用如下命令(以 super 管理员身份登录):

mongo -u sup_admin -p yyyyyyyy

为了验证我的用户状态,我运行以下命令:

use gambit_test
show users

它返回以下内容:

{
"_id" : "gambit_test.gambit_admin",
"user" : "gambit_admin",
"db" : "gambit_test",
"roles" : [
{
"role" : "readWrite",
"db" : "gambit_test"
},
{
"role" : "dbAdmin",
"db" : "gambit_test"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}

在我的 Python test.py 代码中,这是我正在做的事情:

from mongoengine import *

class History(Document):
name = StringField(required=True)

if __name__ == "__main__":
try:
connect('gambit_test')
History.objects().get()
except Exception as e:
connect(host='mongodb://gambit_admin:xxxxxx@localhost' +
':27017/gambit_test')
History.objects().get()

当我尝试运行上面的 python 代码时,出现以下错误:

Traceback (most recent call last):
File "test.py", line 12, in <module>
History.objects().get()
File "/home/ubuntu/gambit/genv/lib/python3.6/site-packages/mongoengine/queryset/base.py", line 267, in get
result = six.next(queryset)
File "/home/ubuntu/gambit/genv/lib/python3.6/site-packages/mongoengine/queryset/base.py", line 1484, in __next__
raw_doc = six.next(self._cursor)
File "/home/ubuntu/gambit/genv/lib/python3.6/site-packages/pymongo/cursor.py", line 1189, in next
if len(self.__data) or self._refresh():
File "/home/ubuntu/gambit/genv/lib/python3.6/site-packages/pymongo/cursor.py", line 1104, in _refresh
self.__send_message(q)
File "/home/ubuntu/gambit/genv/lib/python3.6/site-packages/pymongo/cursor.py", line 982, in __send_message
helpers._check_command_response(first)
File "/home/ubuntu/gambit/genv/lib/python3.6/site-packages/pymongo/helpers.py", line 155, in _check_command_response
raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: command find requires authentication

我不知道为什么会发生这种情况。原因是我可以使用 Mongo 客户端执行正常的 find() 和 insert() 操作。但是,当我使用 Mongoengine 时,事情就崩溃了......

任何帮助将不胜感激。

最佳答案

为了进一步引用,问题已修复 ( https://github.com/MongoEngine/mongoengine/issues/2010 )问题是第二个 connect 调用没有覆盖第一个调用设置的 default 连接,它只是被默默地忽略了

关于python - Mongoengine - 命令查找需要身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54897134/

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