Docs 菜单

Docs 主页C++ 驱动程序

配置

在此页面上

  • 配置 TLS/SSL
  • 配置身份验证
  • 配置连接池
  • 将数据压缩到 MongoDB 或从 MongoDB 压缩数据

在 mongocxx 驱动程序中,大多数配置都是通过 连接 URI 完成的。通过 mongocxx::options::client 可以使用一些附加连接选项 类。

要启用 TLS (SSL),请在 URI 中设置 tls=true

mongodb://mongodb.example.com/?tls=true

默认情况下,mongocxx 将根据本地系统 CA 列表验证服务器证书。您可以通过在连接字符串中指定不同的设置,或创建 mongocxx::options::tls 对象,并将其传递给tls_opts mongocxx::options::client

例如,要使用自定义 CA 或禁用证书验证,请参阅以下示例:

// 1) Using tls_options
mongocxx::options::client client_options;
mongocxx::options::tls tls_options;
// If the server certificate is not signed by a well-known CA,
// you can set a custom CA file with the `ca_file` option.
tls_options.ca_file("/path/to/custom/cert.pem");
// If you want to disable certificate verification, you
// can set the `allow_invalid_certificates` option.
tls_options.allow_invalid_certificates(true);
client_options.tls_opts(tls_options);
auto client1 = mongocxx::client{uri{"mongodb://host1/?tls=true"}, client_options};
// 2) Using the URI
auto client2 = mongocxx::client{uri{"mongodb://host1/?tls=true&tlsAllowInvalidCertificates=true&tlsCAFile=/path/to/custom/cert.pem"}};

MongoDB 3.0将默认身份验证机制从MONGODB-CR更改为SCRAM-SHA-1。要创建无论服务器版本如何都能正确进行身份验证的凭证,请使用连接字符串,直接在 URI 中包含用户和密码,并附带指定要从中进行身份验证的数据库的参数:

#include <mongocxx/client.hpp>
#include <mongocxx/uri.hpp>
auto client = mongocxx::client{uri{"mongodb://user1:pwd1@host1/?authSource=db1"}};

要显式创建类型为SCRAM-SHA-1的档案,请使用上述连接字符串,但使用一个参数将身份验证机制指定为"SCRAM-SHA-1"

#include <mongocxx/client.hpp>
#include <mongocxx/uri.hpp>
auto client = mongocxx::client{
uri{"mongodb://user1:pwd1@host1/?authSource=db1&authMechanism=SCRAM-SHA-1"}
};

MONGODB-CR authMechanism 已弃用,将不再在 MongoDB 4中运行。 0 。相反,不指定 authMechanism,驱动程序将使用与服务器兼容的身份验证机制。

X. 509身份验证机制对名称源自驱动程序在 TLS 协商期间提供的 X. 509证书的标识主题名称的用户进行身份验证。此身份验证方法需要使用带证书验证的 TLS 连接,可在 MongoDB 2.6及更高版本中使用。要创建此类档案,请使用带有参数的连接字符串,该参数将身份验证机制指定为"MONGODB-X509" 、指定包含客户端私钥和证书的文件的路径并启用 TLS:

#include <mongocxx/client.hpp>
#include <mongocxx/uri.hpp>
auto client = mongocxx::client{
uri{"mongodb://host1/?authMechanism=MONGODB-X509&tlsCertificateFile=client.pem&tls=true"}
};

有关从证书确定主题名称的更多信息,请参阅 MongoDB Server X. 509教程

还可以使用 mongocxx::options::tls 指定 PEM 文件 类,请参阅上面的 默认 TLS/SSL 配置 示例。

MongoDB Enterprise支持通过 Kerberos 服务进行代理身份验证。要创建Kerberos (GSSAPI)类型的档案,请使用在 URI 中包含用户名和域的连接字符串以及将身份验证机制指定为"GSSAPI"的参数:

#include <mongocxx/client.hpp>
#include <mongocxx/uri.hpp>
auto client = mongocxx::client{
uri{"mongodb://username%40REALM.COM@host1/?authMechanism=GSSAPI"}
};

注意

URI 字符串中的@符号必须转义为%40 ,如上例所示。

MongoDB Enterprise支持通过轻量级目录访问协议 (LDAP) 服务进行代理身份验证。要创建 LDAP 类型的档案,请使用指定用户的连接字符串以及将身份验证源指定为"$external"并将身份验证机制指定为"PLAIN"的参数:

#include <mongocxx/client.hpp>
#include <mongocxx/uri.hpp>
auto client = mongocxx::client{
uri{"mongodb://user1:pwd1@host1/?authSource=$external&authMechanism=PLAIN"}
};

要配置连接池,请首先创建一个mongocxx::pool ,并将 URI 作为参数传递。池的大小可以在 URI 中配置。然后,调用mongocxx::pool::acquire以从池中接收客户端。当客户端Go out of scope,将自动返回到连接池中。

#include <mongocxx/pool.hpp>
#include <mongocxx/uri.hpp>
auto pool = mongocxx::pool{uri{"mongodb://host1/?minPoolSize=3&maxPoolSize=5"}};
{
// To get a client from the pool, call `acquire()`.
auto client = pool.acquire();
// The client is returned to the pool when it goes out of scope.
}

有关详细信息,请参阅连接池文档

MongoDB 3 。 4添加了 Snappy 压缩支持,而 zlib 压缩则是在3中添加的。 6 ,以及4中的 zstd 压缩。 2 。

可以使用适当的 URI 选项启用数据压缩,如 C 驱动程序手册中所述。

←  高级配置和安装选项客户端字段级加密 (Client-Side Field Level Encryption) →