对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。

SCRAM身份验证机制

Salted 挑战响应身份验证机制 (SCRAM) 是一系列身份验证机制,它们使用质询-响应机制对用户进行身份验证。SCRAM-SHA-256(使用 SHA-256算法对密码进行哈希处理)是MongoDB Server 4.0 及更高版本中的默认身份验证机制。 SCRAM-SHA-1 改用 SHA-1算法,是早于 4.0 的MongoDB Server版本中的默认身份验证机制。

您可以使用SCRAM对MongoDB Atlas、 MongoDB Enterprise Advanced和MongoDB Community Edition进行身份验证。

提示

SCRAM 机制

要学习;了解有关SCRAM系列身份验证机制的更多信息,请参阅 维基百科上的 RFC5802 和 Salted 挑战响应身份验证机制。

有关SCRAM的MongoDB实施的更多信息,请参阅MongoDB Server手册中的SCRAM 。

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

  • <db_username>:要进行身份验证的用户的MongoDB用户名。

  • <db_password>:要进行身份验证的用户的MongoDB密码。

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

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

DEFAULT 身份验证机制是一种后备设置,指示驱动程序按以下优先顺序协商服务器支持的第一个身份验证机制:

  1. SCRAM-SHA-256

  2. SCRAM-SHA-1

  3. MONGODB-CR

如果指定了 DEFAULT 选项,则驱动程序将首先尝试使用 SCRAM-SHA-256 进行身份验证。如果 MongoDB 实例的版本不支持该机制,则驱动程序将尝试使用 SCRAM-SHA-1 进行身份验证。如果实例也不支持该机制,则驱动程序将尝试使用 MONGODB-CR 进行身份验证。

您可以通过在连接字符串中将 authMechanism 参数设置为 DEFAULT 来指定默认身份验证机制,也可以省略该参数,因为它是默认值。

以下示例展示了如何通过在连接字符串中将 authMechanism 设置为 DEFAULT 来将身份验证机制设立为默认:

重要

始终使用 encodeURIComponent 方法对用户名和密码进行 URI 编码,以确保正确解析它们。

const { MongoClient } = require("mongodb"); // Replace the following with values for your environment.const username = encodeURIComponent("<db_username>");const password = encodeURIComponent("<db_password>");const clusterUrl = "<cluster_url>"; const authMechanism = "DEFAULT"; // Replace the following with your MongoDB deployment's connection string.const uri = `mongodb+srv://${username}:${password}@${clusterUrl}/?authMechanism=${authMechanism}`; // Create a new MongoClientconst client = new MongoClient(uri); // Function to connect to the serverasync function run() { try { // Establish and verify connection await client.db("admin").command({ ping: 1 }); console.log("Connected successfully to server"); } finally { // Ensures that the client will close when you finish/error await client.close(); }}run().catch(console.dir);

要学习;了解有关MongoDB支持的SCRAM版本的更多信息,请参阅MongoDB Server手册的SCRAM部分。

注意

SCRAM-SHA-256 是从 MongoDB 4.0 开始使用的默认身份验证方法

SCRAM-SHA-256 是SCRAM版本,它使用通过 SHA-256算法加密的用户名和密码来验证用户身份。

您可通过在连接字符串中将 authMechanism 设为 SCRAM-SHA-256 值来指定此身份验证机制,如以下示例代码所示。

重要

始终使用 encodeURIComponent 方法对用户名和密码进行 URI 编码,以确保正确解析它们。

const { MongoClient } = require("mongodb"); // Replace the following with values for your environment.const username = encodeURIComponent("<db_username>");const password = encodeURIComponent("<db_password>");const clusterUrl = "<cluster_url>"; const authMechanism = "SCRAM-SHA-256"; // Replace the following with your MongoDB deployment's connection string.const uri = `mongodb+srv://${username}:${password}@${clusterUrl}/?authMechanism=${authMechanism}`; // Create a new MongoClientconst client = new MongoClient(uri); // Function to connect to the serverasync function run() { try { // Establish and verify connection await client.db("admin").command({ ping: 1 }); console.log("Connected successfully to server"); } finally { // Ensures that the client will close when you finish/error await client.close(); }}run().catch(console.dir);

注意

SCRAM-SHA-1 是 MongoDB 版本 3.0、3.2、3.4 和 3.6 的默认身份验证方法。

SCRAM-SHA-1 是SCRAM版本,它使用通过 SHA-1算法加密的用户名和密码来验证用户身份。

您可通过在连接字符串中将 authMechanism 参数设为 SCRAM-SHA-1 值来指定此身份验证机制,如以下示例代码所示。

重要

始终使用 encodeURIComponent 方法对用户名和密码进行 URI 编码,以确保正确解析它们。

const { MongoClient } = require("mongodb"); // Replace the following with values for your environment.const username = encodeURIComponent("<db_username>");const password = encodeURIComponent("<db_password>");const clusterUrl = "<cluster_url>"; const authMechanism = "SCRAM-SHA-1"; // Replace the following with your MongoDB deployment's connection string.const uri = `mongodb+srv://${username}:${password}@${clusterUrl}/?authMechanism=${authMechanism}`; // Create a new MongoClientconst client = new MongoClient(uri); // Function to connect to the serverasync function run() { try { // Establish and verify connection await client.db("admin").command({ ping: 1 }); console.log("Connected successfully to server"); } finally { // Ensures that the client will close when you finish/error await client.close(); }}run().catch(console.dir);

要学习;了解有关本页讨论的任何方法或类型的更多信息,请参阅以下API文档: