Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs Menu
Docs Home
/ / /
PHP 라이브러리 매뉴얼
/

연결 옵션 지정

이 페이지에서는 PHP 라이브러리에서 사용할 수 있는 MongoDB 연결 및 인증 옵션에 대해 설명합니다.

연결 URI에 옵션을 지정하거나 옵션을 MongoDB\Client 생성자에 전달하여 연결을 구성할 수 있습니다.

연결 URI를 MongoDB\Client 생성자에 전달하는 경우 URI에 연결 옵션을 <name>=<value> 쌍으로 포함할 수 있습니다. 다음 예시 에서는 연결 URI가 tls 옵션을 true 로 설정하고 tlsCertificateKeyFile 옵션을 /path/to/file.pem 로 설정합니다.

// Replace the placeholders with your actual hostname, port, and path to the certificate key file
$uri = 'mongodb://<hostname>:<port>/?tls=true&tlsCertificateKeyFile=/path/to/file.pem';
// Create a MongoDB client
$client = new MongoDB\Client($uri);

연결 URI에 연결 옵션을 포함하는 대신 MongoDB\Client 생성자에 연결 옵션을 전달할 수 있습니다.

다음 예시 에서는 MongoDB\Client 생성자의 $uriOptions 매개 변수를 사용하여 연결 옵션을 설정하다 하는 방법을 보여 줍니다.

// Replace the placeholders with your actual hostname and port
$uri = 'mongodb://<hostname>:<port>/';
// Set the connection options
// Replace the placeholder with the actual path to the certificate key file
$uriOptions = [
'tls' => true,
'tlsCertificateKeyFile' => '/path/to/file.pem',
];
// Create a MongoDB client with the URI and options
$client = new Client($uri, $uriOptions);

참고

$uriOptions 매개변수와 연결 URI 모두에 옵션을 지정하면 $uriOptions 의 값이 우선합니다.

다음 섹션에서는 MongoDB 연결에 설정하다 수 있는 옵션에 대해 설명합니다. 각 연결 옵션은 MongoDB Server 매뉴얼의 해당 항목으로 연결됩니다.

중요

백분율 인코딩

연결 옵션 값에 특수 문자가 포함된 경우 해당 값을 연결 URI에 포함하기 전에 값을 퍼센트인코딩 해야 합니다. rawurlencode() 메서드를 사용하여 RFC 3986 에 지정된 URI 구문에 따라 이러한 값을 인코딩할 수 있습니다.

연결 옵션을 $uriOptions 매개변수에 포함할 때는 퍼센트 인코딩하지 마세요.

자세히 알아보려면 다음 리소스를 참조하세요.

  • RFC 3986

  • PHP 매뉴얼의rawurlencode

연결 옵션
설명
Data Type: bool
MongoDB\Client Example: $uriOptions = ['directConnection' => true];
Connection URI Example: directConnection=true
Data Type: string
MongoDB\Client Example: $uriOptions = ['replicaSet' => 'replicaSetName'];
Connection URI Example: replicaSet=replicaSetName

PHP 라이브러리에서 사용할 수 있는 TLS 옵션에 학습 보려면 TLS 페이지를 참조하세요.

연결 옵션
설명
Data Type: int
MongoDB\Client Example: $uriOptions = ['connectTimeoutMS' => 2000];
Connection URI Example: connectTimeoutMS=2000
Data Type: int
MongoDB\Client Example: $uriOptions = ['socketTimeoutMS' => 20000];
Connection URI Example: socketTimeoutMS=20000
연결 옵션
설명
Data Type: string
MongoDB\Client Example: $uriOptions = ['compressors' => 'snappy,zstd,zlib'];
Connection URI Example: compressors=snappy,zstd,zlib
Data Type: int
MongoDB\Client Example: $uriOptions = ['zlibCompressionLevel' => 3];
Connection URI Example: zlibCompressionLevel=3
연결 옵션
설명
Data Type: string
MongoDB\Client Example: $uriOptions = ['w' => 'majority'];
Connection URI Example: w=majority
Data Type: int
MongoDB\Client Example: $uriOptions = ['wTimeoutMS' => 10000];
Connection URI Example: wTimeoutMS=10000
Data Type: bool
MongoDB\Client Example: $uriOptions = ['journal' => true];
Connection URI Example: journal=true
연결 옵션
설명
Data Type: string
MongoDB\Client Example: $uriOptions = ['readConcernLevel' => 'majority'];
Connection URI Example: readConcernLevel=majority
연결 옵션
설명
MongoDB\Client Example: $uriOptions = ['readPreference' => 'secondaryPreferred'];
Connection URI Example: readPreference=secondaryPreferred
Data Type: int
MongoDB\Client Example: $uriOptions = ['maxStalenessSeconds' => 30];
Connection URI Example: maxStalenessSeconds=30
Data Type: array
MongoDB\Client Example:
$uriOptions = [
'readPreferenceTags' => [
['dc' => 'ny', 'rack' => 'r1'],
[],
],
];

연결 URI 예시: readPreferenceTags=dc:ny,rack:r1&readPreferenceTags=

PHP 라이브러리에서 사용할 수 있는 인증 옵션에 학습 보려면 인증 메커니즘을 참조하세요.

연결 옵션
설명
Data Type: int
MongoDB\Client Example: $uriOptions = ['localThresholdMS' => 20];
Connection URI Example: localThresholdMS=20
Data Type: int
MongoDB\Client Example: $uriOptions = ['serverSelectionTimeoutMS' => 5000];
Connection URI Example: serverSelectionTimeoutMS=5000
Data Type: bool
MongoDB\Client Example: $uriOptions = ['serverSelectionTryOnce' => false];
Connection URI Example: serverSelectionTryOnce=false
Data Type: int
MongoDB\Client Example: $uriOptions = ['heartbeatFrequencyMS' => 30000];
Connection URI Example: heartbeatFrequencyMS=30000
Data Type: int
MongoDB\Client Example: $uriOptions = ['socketCheckIntervalMS' => 4000];
Connection URI Example: socketCheckIntervalMS=4000
연결 옵션
설명
Data Type: string
MongoDB\Client Example: $uriOptions = ['appName' => 'myApp'];
Connection URI Example: appName=myApp
Data Type: bool
MongoDB\Client Example: $uriOptions = ['retryReads' => false];
Connection URI Example: retryReads=false
Data Type: bool
MongoDB\Client Example: $uriOptions = ['retryWrites' => false];
Connection URI Example: retryWrites=false
Data Type: bool
MongoDB\Client Example: $uriOptions = ['loadBalanced' => true];
Connection URI Example: loadBalanced=true
Data Type: int
MongoDB\Client Example: $uriOptions = ['srvMaxHosts' => 5];
Connection URI Example: srvMaxHosts=5

MongoDB\Client 클래스에 대한 자세한 내용은 다음 PHP 라이브러리 API 문서를 참조하세요.

MongoDB\Driver\ReadPreference 클래스에 대한 자세한 내용은 다음 PHP 확장 API 문서를 참조하세요.

돌아가기

연결 대상 선택

이 페이지의 내용