Docs Menu
Docs Home
/ / /
C++ 드라이버

MongoDB에 연결

이 페이지의 내용

  • 개요
  • 샘플 애플리케이션
  • 연결
  • Atlas
  • 로컬 배포
  • 복제본 세트
  • TLS(전송 계층 보안)
  • TLS 활성화
  • 인증 기관(CA) 파일 지정
  • OCSP 검사 비활성화
  • 인증서 해지 목록(CRL) 지정
  • 클라이언트 인증서 제시
  • 인증서 키 파일 비밀번호 제공
  • 안전하지 않은 TLS 허용
  • 인증서 유효성 검사 비활성화
  • 호스트 이름 확인 사용 안 함
  • 네트워크 압축
  • 압축 알고리즘
  • zlibCompressionLevel
  • Stable 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 deployment 에 연결하는 방법을 보여줍니다.

mongocxx::uri uri("mongodb://localhost: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 활성화 를 참조하세요.

다음 코드는 MongoDB 인스턴스 에 연결하기 위해 CA 파일 의 경로를 지정하는 방법을 보여줍니다.

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 deployment 에 제공하는 클라이언트 인증서를 지정하는 방법을 보여줍니다.

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 인스턴스 에 대한 연결을 위해 Stable 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);

Stable API 에 학습 보려면 Stable API 가이드 를 참조하세요.

돌아가기

다음 단계