From 11801d977439ebabdbd6113285d8db70399ee3b7 Mon Sep 17 00:00:00 2001 From: Kristina Chodorow Date: Thu, 26 Aug 2010 15:34:16 +0000 Subject: [PATCH] added replica set docs git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@302812 c90b9560-bf6c-de11-be94-00142212c4b1 --- reference/mongo/connecting.xml | 87 +++++++++++++++----- reference/mongo/mongoconnectionexception.xml | 9 ++ 2 files changed, 75 insertions(+), 21 deletions(-) diff --git a/reference/mongo/connecting.xml b/reference/mongo/connecting.xml index 75cb43b38f..2afc542e4e 100644 --- a/reference/mongo/connecting.xml +++ b/reference/mongo/connecting.xml @@ -32,18 +32,16 @@ $db->authenticate($username, $password); There is a major disadvantage to this method: if the database connection is - dropped and then autoreconnects, the connection will no longer be - authenticated. + dropped and then reconnected, the connection will no longer be authenticated. If you use the connection string format described by - Mongo::__construct, the database will authenticate the - connection as soon as it connects and reauthenticate if the connection is - re-esetablished. + Mongo::__construct, the database will authenticate on + connection and reauthenticate if the connection is re-established. This is equivalent to the code above, except that reconnections to the - database will automatically be authenticated: + database will be authenticated automatically:
- Replica Pairs + Replica Sets - To connect to a replica pair, specify both sides of the pair. + To connect to a replica set, specify one or more members of the set and use + the replicaSet option. true)); ?> ]]> - Order is irrelevant, the PHP driver will query the database servers to figure - out which one is master. So long as one of them is up and master, the - connection will succeed. If neither is up or there is no master, a + Version 1.0.9+ of the driver is required to connect to a replica set + (earlier versions of the driver will not autodetect the master or reconnect + correctly). + + + The PHP driver will query the database server(s) listed to figure out who is + master. So long as it can connect to at least one host listed and find a + master, the connection will succeed. If it cannot make a connection to any + servers listed or cannot find a master, a MongoConnectionException will be thrown. - If the master becomes unavailable, the slave will not become master for a few - seconds. During that time, the driver will not be able to perform any - database operations because the master database is gone and the other - database is a slave. Thus, if you attempt to do any sort of query (including - "safe" operations) they will throw exceptions. + If the master becomes unavailable, the slaves will not promote a new master + for a few seconds. During that time, this connection will not be able to + perform any database operations (connections to slaves will still be able to + perform reads). Thus, if you attempt to do any sort of read or write on this + connection, it will throw an exception. - The slave will eventually realize that the master is gone and become master - itself. At this point, the driver will make this its primary database - connection and continue operating normally. + Once a master is elected, attempting to perform a read or write will allow + the driver to detect the new master. The driver will make this its primary + database connection and continue operating normally. - For more information on replica pairs, see the + For more information on replica sets, see the core documentation.
+
+ Distributing Reads + + One way of using replica sets is to perform reads on the slaves to take some + load off of the master. The driver will not do this automatically for you, + but you can set it up yourself. + + + First, connect to the replica set using the replicaSet + option. This will give you a connection to the replica set's master. Use + this connection to call the ismaster command, which lists + all of the slaves in the replica set. + + + true)); +$result = $m->admin->command(array("ismaster" => 1)); + +?> +]]> + + + Now, create connections to the hosts listed in the "hosts" and "passives" + fields (passives are members of the set that can never become master). Do + not use replicaSet => true, or the driver will + automatically find and use the master.) Then you can distribute reads among + these connections. Make sure to set + MongoCursor::slaveOkay to &true; before attempting to read + from a slave. + +
+
Persistent Connections @@ -163,6 +202,12 @@ for ($i=0; $i<1000; $i++) { match an existing persistent connection. Otherwise, a new connection will be created with this identifying information. + + Persistent connections are highly recommended and should + always be used in production unless there is a compelling reason not to. + Most of the reasons that they are not recommended for relational databases + are irrelevant to MongoDB. +
diff --git a/reference/mongo/mongoconnectionexception.xml b/reference/mongo/mongoconnectionexception.xml index 0d3447dfe2..728c109ef6 100644 --- a/reference/mongo/mongoconnectionexception.xml +++ b/reference/mongo/mongoconnectionexception.xml @@ -52,6 +52,15 @@ Connecting to the database timed out. + + + Transport endpoint is not connected + + + Generally means that the connection string isn't correct, the driver + couldn't even find the database server. + + couldn't determine master