文档菜单
文档首页
/ / /
C++ 驱动程序

连接到 MongoDB

本页内容

  • 概述
  • 示例应用
  • 连接
  • Atlas
  • 本地部署
  • 副本集
  • 传输层安全性(TLS)
  • 启用TLS
  • 指定证书颁发机构(CA)文件
  • 禁用OCSP检查
  • 指定证书吊销列表(CRL)
  • 提供客户端证书
  • 提供证书密钥文件密码
  • 允许不安全的TLS
  • 禁用证书验证
  • 禁用主机名验证
  • 网络压缩
  • 压缩算法
  • zlib压缩级别
  • 稳定的API

此页面包含代码示例,展示了如何使用各种设置将C++应用程序连接到MongoDB。

提示

要了解更多关于此页面的连接选项,请参阅每个部分中提供的链接。

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

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

  1. 确保您已将 C++ 驱动程序安装在项目可以从中导入的位置。

  2. 复制以下代码,并将其粘贴到您项目中的新 .cpp 文件中。

  3. 从本页复制一个代码示例,并将其粘贴到文件的突出显示部分。

1#include <bsoncxx/json.hpp>
2
3#include <mongocxx/client.hpp>
4#include <mongocxx/exception/exception.hpp>
5#include <mongocxx/instance.hpp>
6#include <mongocxx/uri.hpp>
7
8#include <iostream>
9
10int main()
11{
12 mongocxx::instance instance;
13
14 try
15 {
16 // Start example code here
17
18 // End example code here
19
20 auto admin = client["admin"];
21 admin.run_command(bsoncxx::from_json(R"({ "ping": 1 })"));
22
23 std::cout << "Successfully pinged the MongoDB server." << std::endl;
24 }
25 catch (const mongocxx::exception &e)
26 {
27 std::cout << "An exception occurred: " << e.what() << std::endl;
28 return EXIT_FAILURE;
29 }
30
31 return EXIT_SUCCESS;
32}

以下代码展示了如何连接到 MongoDB Atlas 部署

mongocxx::uri uri("<Atlas connection string>");
mongocxx::client client(uri);

要了解更多有关连接到 Atlas 部署的信息,请参阅Atlas 在连接目标指南中。

以下代码展示了如何连接到本地 MongoDB 部署

mongocxx::uri uri("mongodb://:27017/");
mongocxx::client client(uri);

要了解更多有关连接到本地部署的信息,请参阅 本地部署 在连接目标指南中。

以下代码展示了如何连接到副本集部署

mongocxx::uri uri("mongodb://<replica set member>:<port>/?replicaSet=<replica set name>");
mongocxx::client client(uri);

要了解更多关于连接到副本集的信息,请参阅《连接目标指南》中的副本集

以下代码展示了如何为您的MongoDB实例连接启用TLS

mongocxx::uri uri("mongodb://<hostname>:<port>/?tls=true");
mongocxx::client client(uri);

要了解更多关于启用TLS的信息,请参阅《TLS配置指南》中的启用TLS

以下代码示例展示了如何指定您的CA文件以连接到MongoDB实例的路径

mongocxx::options::client client_options;
mongocxx::options::tls tls_options;
tls_options.pem_file("/path/to/file.pem");
client_options.tls_opts(tls_options);
mongocxx::uri uri("mongodb://<hostname>:<port>/?tls=true");
mongocxx::client client(uri, client_options);
mongocxx::uri uri("mongodb://<hostname>:<port>/?tls=true&tlsCAFile=/path/to/file.pem");

要了解有关指定CA文件的更多信息,请参阅TLS配置指南中的指定CA文件

以下代码示例展示了如何防止驱动程序联系OCSP端点

mongocxx::uri uri("mongodb://<hostname>:<port>/?tls=true&tlsDisableOCSPEndpointCheck=true");
mongocxx::client client(uri);

要了解有关禁用OCSP检查的更多信息,请参阅TLS配置指南中的OCSP

以下代码示例展示了如何指导驱动程序使用CRL验证服务器的证书

mongocxx::options::client client_options;
mongocxx::options::tls tls_options;
tls_options.crl_file("<path to your CRL file>");
client_options.tls_opts(tls_options);
mongocxx::uri uri("mongodb://<hostname>:<port>/?tls=true");
mongocxx::client client(uri, client_options);

要了解有关指定CRL的更多信息,请参阅TLS配置指南中的证书吊销列表

以下代码演示了如何指定驱动程序向您的MongoDB部署提供的客户端证书。

mongocxx::options::client client_options;
mongocxx::options::tls tls_options;
tls_options.pem_file("/path/to/file.pem");
client_options.tls_opts(tls_options);
mongocxx::uri uri("mongodb://<hostname>:<port>/?tls=true");
mongocxx::client client(uri, client_options);
mongocxx::uri uri("mongodb://<hostname>:<port>/?tls=true&tlsCertificateKeyFile=/path/to/file.pem");

要了解有关指定客户端证书的更多信息,请参阅TLS配置指南中的展示客户端证书

以下代码演示了如何指定客户端证书的密码。

mongocxx::options::client client_options;
mongocxx::options::tls tls_options;
tls_options.pem_file("/path/to/file.pem");
tls_options.pem_password("<password>");
client_options.tls_opts(tls_options);
mongocxx::uri uri("mongodb://<hostname>:<port>/?tls=true");
mongocxx::client client(uri, client_options);
mongocxx::uri uri("mongodb://<hostname>:<port>/?tls=true&tlsCertificateKeyFile=/path/to/file.pem&tlsCertificateKeyFilePassword=<password>");

要了解有关提供密钥文件密码的更多信息,请参阅TLS配置指南中的提供密钥密码

以下代码演示了如何禁用证书验证。

mongocxx::uri uri("mongodb://<hostname>:<port>/?tls=true&tlsInsecure=true");
mongocxx::client client(uri);

要了解有关允许不安全的TLS的更多信息,请参阅TLS配置指南中的允许不安全的TLS

以下代码展示了如何禁用证书验证

mongocxx::options::client client_options;
mongocxx::options::tls tls_options;
tls_options.allow_invalid_certificates(true);
client_options.tls_opts(tls_options);
mongocxx::uri uri("mongodb://<hostname>:<port>/?tls=true");
mongocxx::client client(uri, client_options);
mongocxx::uri uri("mongodb://<hostname>:<port>/?tls=true&tlsAllowInvalidCertificates=true");

要了解更多关于禁用证书验证的信息,请参阅TLS配置指南中的允许不安全TLS

以下代码展示了如何禁用主机名验证

mongocxx::uri uri("mongodb://<hostname>:<port>/?tls=true&tlsAllowInvalidHostnames=true");
mongocxx::client client(uri);

要了解更多关于禁用主机名验证的信息,请参阅TLS配置指南中的允许不安全TLS

以下代码展示了如何通过指定每个压缩算法来启用与您的 MongoDB 实例的连接压缩。

mongocxx::uri uri("mongodb://<hostname>:<port>/?compressors=snappy,zstd,zlib");
mongocxx::client client(uri);

要了解更多关于指定压缩算法的信息,请参阅网络压缩指南中的指定压缩算法

以下代码展示了如何指定 zlib 压缩算法并设置其压缩级别。

mongocxx::uri uri("mongodb://<hostname>:<port>/?compressors=zlib&zlibCompressionLevel=1");
mongocxx::client client(uri);

要了解更多关于设置 zlib 压缩级别的信息,请参阅网络压缩指南中的指定压缩算法

以下代码展示了如何启用与您的 MongoDB 实例的连接的稳定 API。

mongocxx::uri uri("<connection string>");
mongocxx::options::client client_options;
mongocxx::options::server_api server_api_options(mongocxx::options::server_api::version::k_version_1);
client_options.server_api_opts(server_api_options);
mongocxx::client client(uri, client_options);

要了解更多关于稳定 API 的信息,请参阅稳定 API指南。

返回

下一步

© . All rights reserved.