When using the mongodb connection string on a replica set, make sure you DO NOT PUT A SPACE between the servers.
IE. mongodb://db1.example.com:27017, db2.example.com:27017 will throw an error, but mongodb://db1.example.com:27017,db2.example.com:27017 will work correctly.
To connect to a replica set, specify one or more members of the set and use the "replicaSet" option. Multiple servers may be delimited by a comma.
Example #1 ReplicaSet seedlist
// Using multiple servers as the seed list (prefered)
$m = new MongoClient("mongodb://rs1.example.com:27017,rs2.example.com:27017/?replicaSet=myReplSetName"));
// Using one server as the seed list
$m = new MongoClient("mongodb://rs1.example.com:27017", array("replicaSet" => "myReplSetName"));
// Using multiple servers as the seed list
$m = new MongoClient("mongodb://rs1.example.com:27017,rs2.example.com:27017", array("replicaSet" => "myReplSetName"));
The PHP driver will query the database server(s) listed to determine the primary. So long as it can connect to at least one host listed and find a primary, the connection will succeed. If it cannot make a connection to any servers listed or cannot find a primary, a MongoConnectionException will be thrown.
You should always provide a seedlist with more than one member of the ReplicaSet. For highest availability you should seed with at least one server from each of your datacenters.
The host names that you specify here must match the names as given in the replica set configuration because the driver uses the name as specified in the replica set configuration as identifier in its internal mapping. Specifying (for example) the IP address instead of the configured host name makes that the driver keeps a connection for both the host name and its accompanying IP address.
If the primary becomes unavailable, an election will take place and a secondary will be promoted to the role of primary (unless a majority vote cannot be established). During this time (» 20-60 seconds), the connection will not be able to perform any write operations and attempts to do so will result in an exception. Connections to secondaries will still be able to perform reads.
The default Read Preference is to only read from the primary. During the election process there is no primary, and all read will therefore fail.
It is recommended to use
MongoClient::RP_PRIMARY_PREFERREDRead Preference for applications that require high availability for reads, as reads will only be executed on the secondaries when there simply isn't a primary available.
Once a primary is elected, attempting to perform a read or write will allow the driver to detect the new primary. The driver will make this its primary database connection and continue operating normally.
The health and state of a secondary is checked every 5 seconds (configurable with mongo.ping_interval) or when the next operation occurs after 5 seconds. It will also recheck the configuration when the driver has a problem reaching a server.
Replica set failovers are checked every 60 seconds (configurable with mongo.is_master_interval), and whenever a write operation fails when using acknowledged writes.
Secondaries may be behind the primary in operations, so
your application must be able to handle out-of-date data when using
Read Preferences other then
For more information on replica sets, see the » core documentation.
|1.0.9||Added support for connecting to ReplicaSet and automatic failover.|