MongoDB\Driver\Manager
PHP Manual

MongoDB\Driver\Manager::__construct

(mongodb >=1.0.0)

MongoDB\Driver\Manager::__constructCreate new MongoDB Manager

Описание

final public MongoDB\Driver\Manager::__construct ( string $uri [, array $options [, array $driverOptions ]] )

Constructs a new MongoDB\Driver\Manager object with the specified options.

Подсказка

На Unix, драйвер MongoDB чувствителен к сценариям, которые используют системный вызов fork() без последующего exec(). Пользователям рекомендуется не переиспользовать экземпляр MongoDB\Driver\Manager в дочернем процессе. child process.

Список параметров

uri

A » mongodb:// connection URI:

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

The uri is a URL, hence any special characters in its components need to be URL encoded according to » RFC 3986. This is particularly relevant to the username and password, which can often include special characters such as @, :, or %. When connecting via a Unix domain socket, the socket path may contain special characters such as slashes and must be encoded. The rawurlencode() function may be used to encode constituent parts of the URI.

options

» Connection string options.

Замечание:

Specifying options via the options argument will overwrite any options with the same name in the uri argument.

driverOptions

driverOptions
Option Type Description
allow_invalid_hostname bool

Disables hostname validation if TRUE. Defaults to FALSE.

Allowing invalid hostnames may expose the driver to a » man-in-the-middle attack.

ca_dir string

Path to a correctly hashed certificate directory. The system certificate store will be used by default.

Falls back to the deprecated "capath" SSL context option if not specified.

ca_file string

Path to a certificate authority file. The system certificate store will be used by default.

Falls back to the deprecated "cafile" SSL context option if not specified.

crl_file string Path to a certificate revocation list file.
pem_file string

Path to a PEM encoded certificate to use for client authentication.

Falls back to the deprecated "local_cert" SSL context option if not specified.

pem_pwd string

Passphrase for the PEM encoded certificate (if applicable).

Falls back to the deprecated "passphrase" SSL context option if not specified.

context resource

SSL context options to be used as fallbacks for other driver options (as specified). Note that the driver does not consult the default stream context.

This option is supported for backwards compatibility, but should be considered deprecated.

weak_cert_validation bool

Disables certificate validation if TRUE. Defaults to FALSE

Falls back to the deprecated "allow_self_signed" SSL context option if not specified.

Ошибки

Список изменений

Версия Описание
1.2.0

Added the "allow_invalid_hostname", "ca_file", "ca_dir", "clr_file", "pem_file", "pem_pwd", and "weak_cert_validation" driver options.

The driver no longer supports all SSL context options via the "context" driver option, since the PHP Streams API is no longer used for socket communication.

Примеры

Пример #1 MongoDB\Driver\Manager::__construct() basic examples

Connecting to standalone MongoDB node:

<?php

$manager 
= new MongoDB\Driver\Manager("mongodb://example.com:27017");

?>

Connecting to standalone MongoDB node via a Unix domain socket. The socket path may include special characters such as slashes and should be encoded with rawurlencode().

<?php

$manager 
= new MongoDB\Driver\Manager("mongodb://" rawurlencode("/tmp/mongodb-27017.sock"));

?>

Connecting to a replica set:

<?php

$manager 
= new MongoDB\Driver\Manager("mongodb://rs1.example.com,rs2.example.com/?replicaSet=myReplicaSet");

?>

Connecting to a sharded cluster (i.e. one or more mongos instances):

<?php

$manager 
= new MongoDB\Driver\Manager("mongodb://mongos1.example.com,mongos2.example.com/");

?>

Connecting to MongoDB with authentication credentials for a particular user and database:

<?php

$manager 
= new MongoDB\Driver\Manager("mongodb://myusername:mypassword@example.com/mydatabase");

?>

Connecting to MongoDB with authentication credentials for a particular user and database, where the username or password includes special characters (e.g. @, :, %). In the following example, the password string myp@ss:w%rd has been manually escaped; however, rawurlencode() may also be used to escape URI components that may contain special characters.

<?php

$manager 
= new MongoDB\Driver\Manager("mongodb://myusername:myp%40ss%3Aw%25rd@example.com/mydatabase");

?>

Connecting to MongoDB with X509 authentication:

<?php

$manager 
= new MongoDB\Driver\Manager(
    
"mongodb://example.com/?ssl=true&authMechanism=MONGODB-X509",
    [],
    [
        
"pem_file" => "/path/to/client.pem",
    ]
);
?>

Смотрите также


MongoDB\Driver\Manager
PHP Manual