|
Mongo::__construct(PECL mongo >=0.9.0) Mongo::__construct — Creates a new database connection object Описание
public Mongo::__construct
([ string $server = "mongodb://localhost:27017"
[, array $options = array("connect" => TRUE)
]] )
If no parameters are passed, this connects to "localhost:27017" (or whatever was specified in php.ini for mongo.default_host and mongo.default_port). server should have the form: mongodb://[username:password@]host1[:port1][,host2[:port2:],...]/db The connection string always starts with mongodb://, to indicate it is a connection string in this form. If username and password are specified, the constructor will attempt to authenticate the connection with the database before returning. Username and password are optional and must be followed by an @, if specified. At least one host must be given (port optional, always defaulting to 27017) and as many hosts as desired may be connected to. Host names are comma-separated and the constructor will return successfully if it connected to at least one host. If it could not connect to any of the hosts, it will throw a MongoConnectionException. Finally, if you specified a username and password, you may specify a database to authenticate with. If db is not specified, "admin" will be used. Список параметров
Возвращаемые значенияReturns a new database connection object. ОшибкиThrows MongoConnectionException if it tries and fails to connect to the database for all hostnames given. It will also throw a MongoConnnectionException if an invalid username or password is given. See MongoConnectionException documentation for common exceptions and their causes. Список изменений
ПримерыПример #1 Mongo::__construct() replica set example This example shows how to connect the driver to a replica set. It assumes that there is a set of three servers: sf1.example.com, sf2.example.com, and ny1.example.com. The master could be any one of these servers.
<?php If the current master fails, the driver will figure out which secondary server became the new master and automatically start using that connection. Automatic failover will not work correctly if replicaSet is not specified. At least one seed in the seed list must be up for the driver to connect to the replica set. If you include seeds from two separate replica sets, behavior is undefined. See the » core documentation on replica sets for more information. Пример #2 Connecting to a domain socket In version 1.0.9+, you can use a UNIX domain socket to connect to an instance of MongoDB running locally. This should be slightly faster than using a network connection. In version 1.5.0, the MongoDB server automatically opens a socket at /tmp/mongodb-<port>.sock. You can connect to this by specifying the path in your connection string:
<?php You can combine this with any other connections you'd like:
<?php Пример #3 Mongo::__construct() authentication example A user must exist in the admin database before attempting to use authentication. You can create one with the Mongo shell by running: > use admin switched to db admin > db.addUser("testUser", "testPass"); { "_id" : ObjectId("4b21272fd9ab21611d19095c"), "user" : "testUser", "pwd" : "03b9b27e0abf1865e2f6fcbd9845dd59" } > After creating a user with, in this case, username "testUser" and password "testPass", you can create an authenticated connection:
<?php |
|||||||||||