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

保护您的数据

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

提示

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

To use an authentication example from this page, copy the code example into the sample application or your own application. Make sure to replace all placeholders in the code examples, such as <hostname>, with the relevant values for your MongoDB deployment.

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

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

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

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

1#include <mongoc/mongoc.h>
2#include <bson/bson.h>
3
4int main(void) {
5
6 mongoc_uri_t* uri = NULL;
7 mongoc_client_t *client = NULL;
8 mongoc_database_t *database = NULL;
9 bson_t *ping = NULL, reply = BSON_INITIALIZER;
10 bson_error_t error;
11
12 mongoc_init();
13
14 // Start example code here
15
16 // End example code here
17
18 if (!client) {
19 fprintf(stderr, "client initialization failure\n");
20 goto cleanup;
21 }
22
23 database = mongoc_client_get_database(client, "admin");
24
25 ping = BCON_NEW("ping", BCON_INT32(1));
26
27 if (!mongoc_client_command_simple(client, "admin", ping, NULL, &reply, &error)) {
28 fprintf(stderr, "%s\n", error.message);
29 goto cleanup;
30 }
31 printf("Pinged your deployment. You successfully connected to MongoDB!\n");
32
33 cleanup:
34 bson_destroy(&reply);
35 bson_destroy(ping);
36 mongoc_database_destroy(database);
37 mongoc_client_destroy(client);
38 mongoc_uri_destroy(uri);
39 mongoc_cleanup();
40}

以下代码展示了如何使用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进行身份验证的更多信息,请参阅身份验证指南中的以下部分: