Skip to content

Instantly share code, notes, and snippets.

@k-k
Created September 24, 2013 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k-k/6691721 to your computer and use it in GitHub Desktop.
Save k-k/6691721 to your computer and use it in GitHub Desktop.
Quick test to confirm that two instances of MongoClient to separate hosts but using the same replicaSet will direct all queries to the cluster which was first instantiated.
<?php
echo "<pre>";
echo " ######### Both instances connect to the same set of servers: \n\n";
$mc1 = new MongoClient('mongodb://rs1.example.com,rs2.example.com,rs3.example.com', [ 'replicaSet' => 'my_set' ] );
$mc2 = new MongoClient('mongodb://rep1.example.com,rep2.example.com,rep3.example.com', [ 'replicaSet' => 'my_set' ] );
echo " ######### MongoClient - ReplicaSet 1: \n";
print_r( $mc1->listDBs() ); // Shows the correct Dbs
echo " ######### MongoClient - ReplicaSet 2: \n";
print_r( $mc2->listDBs() ); // Incorrectly shows the Dbs from $mc1
echo " ######### Both still show the same Hosts and Connections to all hosts: \n\n";
$mc1 = new MongoClient('mongodb://rs1.example.com,rs2.example.com,rs3.example.com', [ 'replicaSet' => 'my_set' ] );
$mc2 = new MongoClient('mongodb://rep1.example.com');
echo " ######### MongoClient - ReplicaSet 1: \n";
print_r( $mc1->getHosts() );
print_r( $mc1->getConnections() );
print_r( $mc1->listDBs() ); // Shows the correct Dbs
echo " ######### MongoClient - Primary Only: \n";
print_r( $mc2->getHosts() );
print_r( $mc2->getConnections() );
print_r( $mc2->listDBs() ); // Now shows correct Dbs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment