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

保护您的数据

MongoDB支持多种可用于对应用程序进行身份验证的机制。 本页包含演示每种机制的代码示例。

提示

要学习;了解有关此页面上显示的任何机制的更多信息,请参阅每个部分中提供的链接。

要使用此页面中的身份验证示例,请将代码示例复制到示例应用程序或您自己的应用程序中。 确保将代码示例中的所有占位符(例如<hostname> )替换为MongoDB 部署的相关值。

您可以使用以下示例应用程序来测试本页上的代码示例。 要使用示例应用程序,请执行以下步骤:

  1. 确保已安装C驱动程序。

  2. 复制以下代码并将其粘贴到新的.c文件中。

  3. 从此页面复制代码示例,并将其粘贴到文件中的指定行。

#include <mongoc/mongoc.h>#include <bson/bson.h> int main(void) { mongoc_uri_t* uri = NULL; mongoc_client_t *client = NULL; mongoc_database_t *database = NULL; bson_t *ping = NULL, reply = BSON_INITIALIZER; bson_error_t error; mongoc_init(); // Start example code here // End example code here if (!client) { fprintf(stderr, "client initialization failure\n"); goto cleanup; } database = mongoc_client_get_database(client, "admin"); ping = BCON_NEW("ping", BCON_INT32(1)); if (!mongoc_client_command_simple(client, "admin", ping, NULL, &reply, &error)) { fprintf(stderr, "%s\n", error.message); goto cleanup; } printf("Pinged your deployment. You successfully connected to MongoDB!\n"); cleanup: bson_destroy(&reply); bson_destroy(ping); mongoc_database_destroy(database); mongoc_client_destroy(client); mongoc_uri_destroy(uri); mongoc_cleanup();}

以下代码展示了如何使用SCRAM-SHA-256身份验证机制进行身份验证:

const char *uri = "mongodb://<percentEncodedUsername>:<percentEncodedPassword>@<hostname>:<port>/?authMechanism=SCRAM-SHA-256&authSource=<authenticationDatabase>";mongoc_client_t *client = mongoc_client_new(uri);

要了解有关 SCRAM-SHA- 256身份验证的更多信息,请参阅身份验证指南中的SCRAM-SHA- 256 。

以下代码展示了如何使用SCRAM-SHA-1身份验证机制进行身份验证:

const char *uri = "mongodb://<percentEncodedUsername>:<percentEncodedPassword>@<hostname>:<port>/?authMechanism=SCRAM-SHA-1&authSource=<authenticationDatabase>";mongoc_client_t *client = mongoc_client_new(uri);

要了解有关 SCRAM-SHA- 1身份验证的更多信息,请参阅身份验证指南中的SCRAM-SHA- 1 。

以下代码演示如何创建连接 URI,以使用X.509身份验证机制进行身份验证:

mongoc_client_t *client;mongoc_ssl_opt_t ssl_opts = {0}; ssl_opts.pem_file = "mycert.pem"; const char *uri = "mongodb://<percentEncodedUsername>@<hostname>:<port>/?authMechanism=MONGODB-X509";mongoc_client_t *client = mongoc_client_new(uri);mongoc_client_set_ssl_opts(client, &ssl_opts);

要学习;了解有关 X. 509身份验证的更多信息,请参阅身份验证指南中的MONGODB-X 509 。

以下部分介绍如何使用 MONGODB-AWS身份验证机制连接到MongoDB。当您使用 MONGODB-AWS 机制时, C驱动程序会尝试按列出的顺序从以下源检索您的 AWS凭证:

  1. 传递给连接 URI 的命名参数

  2. 环境变量

  3. Amazon Web Services EKS AssumeRoleWithWebIdentity请求

  4. ECS容器元数据

  5. EC 2实例元数据

每节介绍从连接 URI 或备用外部源检索Amazon Web Services凭证时如何使用 MONGODB-AWS 进行身份验证。

要学习;了解有关使用Amazon Web Services进行身份验证的更多信息,请参阅身份验证指南中的MONGODB- Amazon Web Services 。

以下代码演示如何在连接 URI 中传递Amazon Web Services凭证,以便使用 MONGODB-AWS 进行身份验证:

const char *uri = "mongodb://<awsIamAccessKeyId>:<awsIamSecretAccessKey>@<hostname>:<port>/?authMechanism=MONGODB-AWS";mongoc_client_t *client = mongoc_client_new(uri);

要学习;了解通过检索连接 URI凭证来使用Amazon Web Services进行身份验证的更多信息,请参阅身份验证指南中的连接 URI。

以下代码展示了从环境变量、ECS元数据或 EC2实例元数据获取凭证时如何使用 MONGODB-AWS 进行身份验证:

const char *uri = "mongodb://<hostname>:<port>/?authMechanism=MONGODB-AWS";mongoc_client_t *client = mongoc_client_new(uri);

要学习;了解有关通过获取外部凭证来使用Amazon Web Services进行身份验证的更多信息,请参阅身份验证指南中的以下部分: