문서 메뉴

문서 홈C++ 드라이버

구성

이 페이지의 내용

  • TLS/SSL 구성
  • 인증 구성
  • 연결 풀 구성
  • 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에 직접 사용자 및 비밀번호가 포함된 연결 string을 사용하고 인증할 데이터베이스를 지정하는 매개 변수와 함께 사용합니다.

#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" 로 지정하는 매개 변수와 함께 연결 문자열을 사용하고, 클라이언트 비공개 키와 인증서가 포함된 PEM 파일의 경로를 지정하며, 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 튜토리얼 을 참조하세요.

PEM 파일은 mongocxx::options::tls 를 사용하여 지정할 수도 있습니다. 클래스에 대한 자세한 내용은 위의 기본 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(Lightweight Directory Access Protocol) 서비스를 통해 프록시 인증을 지원합니다. 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 을 호출하여 풀에서 클라이언트를 수신합니다. 클라이언트는 범위를 벗어나면 자동으로 풀로 반환됩니다.

#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.

데이터 압축은 C 드라이버 매뉴얼에설명된 대로 적절한 URI 옵션을 사용하여 활성화할 수 있습니다.

←  고급 구성 및 설치 옵션Client-Side Field Level Encryption →