在 mongocxx驱动程序中,大多数配置都是通过 连接 URI 完成的。通过mongocxx::options::客户端类可以使用一些附加连接选项。
配置 TLS/SSL
要启用 TLS (SSL),请在 URI 中设置 tls=true :
mongodb://mongodb.example.com/?tls=true
默认下,mongocxx 将根据本地系统 CA 列表验证服务器证书。您可以通过在连接字符串中指定不同的设置,或创建 mongocxx::options::tls对象并将其传递给 mongocxx::options::client 上的 tls_opts 来覆盖该设置。
示例,要使用自定义 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 。 要创建无论服务器版本如何都能正确进行身份验证的档案,请使用连接string ,直接在 URI 中包含用户和密码,并附带指定要从中进行身份验证的数据库的参数:
auto client = mongocxx::client{uri{"mongodb://user1:pwd1@host1/?authSource=db1"}};
SCRAM-SHA-1
要显式创建类型为 SCRAM-SHA-1 的档案,请使用上述连接string ,但使用参数将身份验证机制指定为 "SCRAM-SHA-1":
auto client = mongocxx::client{ uri{"mongodb://user1:pwd1@host1/?authSource=db1&authMechanism=SCRAM-SHA-1"} };
MONGODB-CR
MONGODB-CR authMechanism 已弃用,将不再在MongoDB 4.0 中运行。相反,不指定 authMechanism,驱动程序将使用与服务器兼容的身份验证机制。
X.509
X.509 机制对名称源自X.驱动程序在 TLS 协商期间提供的 X.509 证书的标识主题名称的用户进行身份验证。此身份验证方法需要使用带证书验证的 TLS 连接,并且在 MongoDB 2.6 及更高版本中可用。要创建此类凭证,请使用带有参数的连接字符串,该参数将身份验证机制指定为 "MONGODB-X509"、指定包含客户端私钥和证书的 PEM文件的路径并启用 TLS:
auto client = mongocxx::client{ uri{"mongodb://host1/?authMechanism=MONGODB-X509&tlsCertificateKeyFile=client.pem&tls=true"} };
有关从证书确定主题名称的更多信息,请参阅MongoDB 服务器 X. 509教程。
还可以使用mongocxx::options::tls类指定 PEM文件,请参阅默认TLS/SSL 配置示例。
Kerberos (GSSAPI)
MongoDB Enterprise支持通过Kerberos服务进行代理身份验证。 要创建Kerberos (GSSAPI)类型的档案,请使用在 URI 中包含用户名和域的连接string以及将身份验证机制指定为 "GSSAPI" 的参数:
auto client = mongocxx::client{ uri{"mongodb://username%40REALM.COM@host1/?authMechanism=GSSAPI"} };
注意
URI string中的 @ 符号必须转义为 %40,如上例所示。
LDAP
MongoDB Enterprise支持通过轻量级目录访问协议 ( LDAP ) 服务进行代理身份验证。 要创建LDAP类型的档案,请使用指定用户的连接string以及将身份验证源指定为 "$external" 并将身份验证机制指定为 "PLAIN" 的参数:
auto client = mongocxx::client{ uri{"mongodb://user1:pwd1@host1/?authSource=$external&authMechanism=PLAIN"} };
配置连接池
要配置连接池,请首先创建一个mongocxx::pool ,并将 URI 作为参数传递。 池的大小可以在 URI 中配置。 然后,调用mongocxx::pool::acquire以从池中接收客户端。 当客户端超出范围时,将自动返回到池中。
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 或从 MongoDB 压缩数据
MongoDB 3.4添加了 Snappy 压缩支持,而3.6中添加了 zlib 压缩, 4.2中添加了 zstd 压缩。
您可以通过相应的 URI 选项启用数据压缩。学习有关这些选项的信息,请参阅MongoDB C 驱动程序 API 文档。