使用认证数据库的mongoengine

Rob*_*Rob 1 python mongodb mongoengine

我不知道如何连接到使用mongoengine的身份验证数据库的mongodb数据库.

在我需要的命令提示符上mongo hostname:27017/myApp -u "test" -p "test" --authenticationDatabase admin,但是我没有看到我将其作为mongoengine的参数传递给我,所以我使用admin数据库进行身份验证但是为我的模型连接到myApp数据库?

我相信这是在PyMongo指南中解释的地方:

https://api.mongodb.com/python/current/examples/authentication.html

>>> from pymongo import MongoClient
>>> client = MongoClient('example.com')
>>> db = client.the_database
>>> db.authenticate('user', 'password', source='source_database')
Run Code Online (Sandbox Code Playgroud)

我找到了将此添加到mongoengine的pull请求:

https://github.com/MongoEngine/mongoengine/pull/590/files

看起来你只是authentication_source作为一个参数添加connect喜欢connect(authentication_source='admin').如果记录得更好,那就太好了.

http://docs.mongoengine.org/apireference.html?highlight=authentication_source

Wan*_*iar 5

根据mongoengine连接指南,该connect()方法支持URI样式连接.即

connect(
   'project1'
   host='mongodb://username:password@host1:port1/databaseName'
)
Run Code Online (Sandbox Code Playgroud)

从这个意义上讲,您还可以指定身份验证源数据库,如下所示:

"mongodb://username:password@host1:port1/database?authSource=source_database"
Run Code Online (Sandbox Code Playgroud)

有关更多MongoDB URI示例,另请参阅MongoDB连接字符串URI.还通过连接字符串验证选项


E.B*_*och 5

API 已更新,因此这是现在执行此操作的正确方法:

connect('mydb',
        host="localhost",
        username="admin",
        password="secret",
        authentication_source='your_auth_db')
Run Code Online (Sandbox Code Playgroud)