Created
September 24, 2013 21:44
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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