개요
이 페이지에는 C 운전자 사용하여 다양한 설정을 지정하여 애플리케이션 MongoDB 에 연결하는 방법을 보여주는 코드 예제가 포함되어 있습니다.
팁
연결 옵션
이 페이지의 연결 옵션에 대해 자세히 알아보려면 각 섹션에 제공된 링크를 참조하세요.
이 페이지의 연결 예제를 사용하려면 코드 예제를 샘플 애플리케이션 또는 자체 애플리케이션에 복사하세요. 코드 예제의 모든 자리 표시자(예: <hostname>)를 MongoDB 배포에 대한 관련 값으로 바꿔야 합니다.
샘플 애플리케이션
다음 샘플 애플리케이션을 사용하여 이 페이지의 코드 예제를 테스트할 수 있습니다. 샘플 애플리케이션을 사용하려면 다음 단계를 수행하세요.
C 운전자 설치되어 있는지 확인합니다.
다음 코드를 복사하여 새
.c파일에 붙여넣습니다.이 페이지에서 코드 예제를 복사하여 파일의 지정된 줄에 붙여넣습니다.
<mongoc/mongoc.h> <bson/bson.h>
int main(void) {
mongoc_uri_t* uri = NULL; mongoc_client_t *client = NULL; mongoc_database_t *database = NULL; bson_t *ping = NULL, reply = BSON_INITIALIZER; bson_error_t error;
mongoc_init();
// Start example code here
// End example code here
if (!client) { fprintf(stderr, "client initialization failure\n"); goto cleanup; }
database = mongoc_client_get_database(client, "admin"); ping = BCON_NEW("ping", BCON_INT32(1)); if (!mongoc_client_command_simple(client, "admin", ping, NULL, &reply, &error)) { fprintf(stderr, "%s\n", error.message); goto cleanup; } printf("Pinged your deployment. You successfully connected to MongoDB!\n");
cleanup: bson_destroy(&reply); bson_destroy(ping); mongoc_database_destroy(database); mongoc_client_destroy(client); mongoc_uri_destroy(uri); mongoc_cleanup();}연결
다음 섹션에서는 MongoDB 의 로컬 인스턴스 또는 Atlas 의 클라우드 호스팅 인스턴스 와 같은 다양한 대상에 연결하는 방법을 설명합니다.
로컬 배포
다음 코드는 string MongoDB의 로컬 인스턴스 에 연결하기 위한 연결 을 보여줍니다.
client = mongoc_client_new("mongodb://localhost:27017");로컬 배포에 연결하는 방법에 대해 자세히 학습 연결 대상 선택 가이드 의 로컬 배포를 참조하세요.
Atlas
다음 코드는 에서 호스팅되는 string 배포서버 에 연결하기 위한 연결 을 Atlas 보여줍니다.
client = mongoc_client_new("mongodb+srv://<db_username>:<db_password>@<hostname>/?<options>");Atlas 에 연결하는 방법에 대해 자세히 학습 연결 대상 선택 가이드 에서 Atlas 참조하세요.
복제본 세트
다음 코드는 복제본 세트 에 연결하기 위한 연결 string 을 보여줍니다.
client = mongoc_client_new("mongodb+srv://<replica-set-member>/?replicaSet=<replica_set_name>");복제본 세트에 연결하는 방법에 대해 자세히 학습 연결 대상 선택 가이드 의 복제본 세트를 참조하세요.
TLS(전송 계층 보안)
다음 섹션에서는 TLS 프로토콜 활성화하면서 MongoDB 에 연결하는 방법을 설명합니다.
C 운전자 에서 TLS를 사용하는 방법에 대해 자세히 학습 TLS(전송 계층 보안) 구성을 참조하세요.
TLS 활성화
다음 탭은 연결에서 TLS를 활성화 하는 방법을 보여줍니다.
client = mongoc_client_new("mongodb://<db_username>:<db_password>@<hostname>/?tls=true");uri = mongoc_uri_new("mongodb://localhost:27017");mongoc_uri_set_option_as_bool(uri, MONGOC_URI_TLS, true);
client = mongoc_client_new_from_uri(uri);TLS 활성화에 대해 자세히 알아보려면 TLS 구성 가이드에서 TLS 활성화 를 참조하세요.
호스트 이름 확인 사용 안 함
다음 탭은 TLS를 사용하여 연결할 때 호스트 이름 확인을 비활성화하는 방법을 보여줍니다.
client = mongoc_client_new("mongodb://<db_username>:<db_password>@<hostname>/?tls=true&tlsAllowInvalidHostnames=true");uri = mongoc_uri_new("mongodb://localhost:27017");mongoc_uri_set_option_as_bool(uri, MONGOC_URI_TLS, true);mongoc_uri_set_option_as_bool(uri, MONGOC_URI_TLSALLOWINVALIDHOSTNAMES, true);
client = mongoc_client_new_from_uri(uri);호스트 이름 확인 비활성화에 대해 자세히 학습 TLS 구성 가이드 의 서버 인증서 확인을 참조하세요.
네트워크 압축
다음 섹션에서는 네트워크 압축 알고리즘을 지정하면서 MongoDB 에 연결하는 방법을 설명합니다.
압축 알고리즘
다음 탭은 MongoDB 에 연결하는 동안 사용 가능한 모든 압축기를 지정하는 방법을 보여줍니다.
client = mongoc_client_new("mongodb://<db_username>:<db_password>@<hostname>/?compressors=snappy,zlib,zstd");uri = mongoc_uri_new("mongodb://localhost:27017");mongoc_uri_set_compressors(uri, "snappy,zlib,zstd");
client = mongoc_client_new_from_uri(uri);zlibCompressionLevel
다음 탭은 zlib 압축기의 압축 수준을 지정하는 방법을 보여줍니다.
client = mongoc_client_new("mongodb://<db_username>:<db_password>@<hostname>/?compressors=zlib&zlibCompressionLevel=<zlib_compression_level");uri = mongoc_uri_new("mongodb://localhost:27017");mongoc_uri_set_option_as_int32(uri, MONGOC_URI_ZLIBCOMPRESSIONLEVEL, <zlib-compression-level>);
client = mongoc_client_new_from_uri(uri);서버 선택
다음 코드는 서버 선택 함수를 지정하는 연결 string 을 보여줍니다.
client = mongoc_client_new("mongodb://<db_username>:<db_password>@<hostname>/?server_selector=<selector_function>");서버 선택 사용자 지정에 대해 자세히 학습하려면 데이터베이스 및 컬렉션 가이드의 읽기 및 쓰기 작업 구성 섹션을 참조하세요.
Stable API
다음 코드는 mongoc_client_t 인스턴스 내에서 Stable API 설정을 지정하는 방법을 보여줍니다.
client = mongoc_client_new("mongodb+srv://<db_username>:<db_password>@<hostname>/?<options>");
// Set the version of the Stable API on the clientmongoc_server_api_t *api = mongoc_server_api_new(MONGOC_SERVER_API_V1);mongoc_client_set_server_api(client, api, &error);
// Do database work here
mongoc_server_api_destroy(api);안정적인 API에 대해 자세히 알아보려면 stable API 를 참조하세요 stable API