Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs 菜单
Docs 主页
/ / /
Pymongo 驱动程序
/ /

LDAP (PLAIN SASL)

The PLAIN authentication mechanism allows you to use your Lightweight Directory Access Protocol (LDAP) username and password to authenticate to MongoDB. LDAP authentication uses the PLAIN Simple Authentication and Security Layer (SASL) defined in RFC-4616.

只有在向MongoDB Atlas或MongoDB Enterprise Advanced进行身份验证时,才能使用此机制。

重要

PLAIN SASL 是一种明文身份验证机制。 在使用 PLAIN SASL 对 MongoDB 进行身份验证时,强烈建议您将 TLS/SSL 与证书验证结合使用。

要详细学习;了解如何为连接启用TLS,请参阅 配置传输层安全性 (TLS)。

本页上的代码示例使用以下占位符:

  • +srv:仅当您连接到MongoDB Atlas 群集时,才在连接字符串前缀中包含此选项。 要学习;了解有关+srv 选项的更多信息,请参阅MongoDB Server手册中的连接字符串格式。

  • <username>:您的LDAP用户名。

  • <password>:您的LDAP密码。

  • <hostname>: MongoDB 部署的网络解决。

  • <port>: MongoDB 部署的端口号。 如果省略此参数,驾驶员将使用默认端口号 (27017)。 连接MongoDB Atlas 群集时无需指定端口。

  • <authenticationDb>:包含用户LDAP凭证的MongoDB 数据库。 如果省略此参数,驾驶员将使用默认数据库(admin)。

要使用本页上的代码示例,请将这些占位符替换为您自己的值。

重要

百分比编码

您必须先用户名和密码进行百分号编码,然后才能将其包含在MongoDB URI 中。quote_plus() urllib.parse 模块中提供的 方法是执行此任务的一种方法。示例,调用quote_plus("and / or") 会返回字符串and+%2F+or

将用户名或密码作为参数传递给MongoClient时,不要对用户名或密码进行百分号编码。

要使用 PLAIN 进行身份验证,请将 authMechanism 连接选项设立为 PLAIN。 您可以通过两种方式设立此选项:将参数传递给 MongoClient 构造函数,或通过连接字符串中的参数。

注意

如果authMechanismProperties值包含逗号,则必须使用MongoClient构造函数来设立身份验证选项。

client = pymongo.MongoClient("mongodb[+srv]://<hostname>:<port>",
username="<username>",
password="<password>",
authSource="<authenticationDb>",
authMechanism="PLAIN",
tls=True)
uri = ("mongodb[+srv]://<username>:<password>@<hostname>:<port>/?"
"authSource=<authenticationDb>"
"&authMechanism=PLAIN"
"&tls=true")
client = pymongo.MongoClient(uri)

要学习;了解有关在PyMongo中使用 PLAIN SASL身份验证机制的更多信息,请参阅以下API文档:

后退

OIDC

在此页面上