【问题标题】:mongoengine using Authentication Databasemongoengine 使用身份验证数据库
【发布时间】:2016-11-06 01:06:25
【问题描述】:

我不知道如何连接到使用带有 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')

我发现了将它添加到 mongoengine 的拉取请求:

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

看起来您只是将authentication_source 作为参数添加到connect,例如connect(authentication_source='admin')。如果有更好的文档记录就好了。

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

【问题讨论】:

    标签: python mongodb mongoengine


    【解决方案1】:

    根据mongoengine connecting guideconnect() 方法支持 URI 样式连接。即

    connect(
       'project1'
       host='mongodb://username:password@host1:port1/databaseName'
    )
    

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

    "mongodb://username:password@host1:port1/database?authSource=source_database"
    

    有关更多 MongoDB URI 示例,另请参阅 MongoDB connection string URI。 还有Authentication options through connection string

    【讨论】:

    • 这不是我要的,需要明确说明身份验证源,所以你发送的内容不起作用
    • @Rob 已更新。我已经从链接中提取了信息,您可以通过 URI 指定身份验证源。
    【解决方案2】:

    API 已经更新,所以现在是正确的做法:

    connect('mydb',
            host="localhost",
            username="admin",
            password="secret",
            authentication_source='your_auth_db')
    

    【讨论】:

    • 我相信自从我在 2016 年提出这个问题以来,API 已经更新,您能否在回答中指出这一点?
    【解决方案3】:

    建议的解决方案对我不起作用。有什么作用: 只需向 connect 方法添加一个 authSource 参数,就像使用 pymongo MongoClient 方法一样。示例:

    connect('database_name', host='host', username="username", 
    password="password",authSource='authentication_database_name') 
    

    【讨论】:

      【解决方案4】:

      这是一个对我有用的简单解决方案。

      connect(db="database_name", host="localhost", port=27017, username="username", 
      password="password", authentication_source="admin")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-11-02
        • 1970-01-01
        • 1970-01-01
        • 2017-12-16
        • 2019-04-19
        • 2012-05-12
        • 1970-01-01
        相关资源
        最近更新 更多