title: How verify if the MongoDb Sharded Replica Set works well?
body: <p>I set up a MongoDb Sharded Replica Set with 2 clusters but I don't know how to test if everything works well.</p>
  <p>I have no error messages on the any of the hosts, which suggest that my setup is ok. All hosts can ping each other and are using the default port 27017.</p>
  <p>Here are the hostnames.</p>
  <p><strong>Shard 1:</strong></p>
  <ul>
  <li>mongo-rs1-1</li>
  <li>mongo-rs1-2</li>
  <li>mongo-rs1-3</li>
  </ul>
  <p><strong>Shard 2:</strong></p>
  <ul>
  <li>mongo-rs2-1</li>
  <li>mongo-rs2-2</li>
  <li>mongo-rs2-3</li>
  </ul>
  <p><strong>3 config servers:</strong></p>
  <ul>
  <li>cfg-1</li>
  <li>cfg-2</li>
  <li>cfg-3</li>
  </ul>
  <p><strong>A router (Mongos):</strong></p>
  <ul>
  <li>mongos-1</li>
  </ul>
  <p>Here is what I have when running sh.status():</p>
  <pre><code>mongos&gt; sh.status()  
                                                         
  --- Sharding Status ---                                                         
    sharding version: {                                                           
          &quot;_id&quot; : 1,                                                              
          &quot;version&quot; : 4,                                                          
          &quot;minCompatibleVersion&quot; : 4,                                             
          &quot;currentVersion&quot; : 5,                                                   
          &quot;clusterId&quot; : ObjectId(&quot;562491c645300851d95d85bb&quot;)                      
  }                                                                               
    shards:                                                                       
          {  &quot;_id&quot; : &quot;rs1&quot;,  &quot;host&quot; : &quot;rs1/mongo-rs1-1:27017,mongo-rs1-2:27017,mon
  go-rs1-3:27017&quot; }                                                               
          {  &quot;_id&quot; : &quot;rs2&quot;,  &quot;host&quot; : &quot;rs2/mongo-rs2-1:27017,mongo-rs2-2:27017,mon
  go-rs2-3:27017&quot; }                                                               
    databases:                                                                    
          {  &quot;_id&quot; : &quot;admin&quot;,  &quot;partitioned&quot; : false,  &quot;primary&quot; : &quot;config&quot; }     
          {  &quot;_id&quot; : &quot;testshard&quot;,  &quot;partitioned&quot; : true,  &quot;primary&quot; : &quot;rs1&quot; }                     
          {  &quot;_id&quot; : &quot;testrs2&quot;,  &quot;partitioned&quot; : false,  &quot;primary&quot; : &quot;rs2&quot; }                              
                                                                
  </code></pre>
  <p>I run my mongos with commands:</p>
  <pre><code>mongos --configdb cfg-1:27017,cfg-2:27017,cfg-3:27017
  </code></pre>
  <p>I run my config servers with commands:</p>
  <pre><code>mongod --noprealloc --smallfiles --configsvr --dbpath /data/db --port 27017
  </code></pre>
  <p>I run my replicat sets (rs1 and rs2) with commands:</p>
  <pre><code>mongod --replSet rs1 --noprealloc --smallfiles
  </code></pre>
  <p>The replica sets are set like this:</p>
  <pre><code>rs1:PRIMARY&gt; rs.status()                                                        
  {                                                                               
          &quot;set&quot; : &quot;rs1&quot;,                                                          
          &quot;date&quot; : ISODate(&quot;2015-10-19T15:45:31Z&quot;),                               
          &quot;myState&quot; : 1,                                                          
          &quot;members&quot; : [                                                           
                  {                                                               
                          &quot;_id&quot; : 0,                                              
                          &quot;name&quot; : &quot;mongo-rs1-1:27017&quot;,                           
                          &quot;health&quot; : 1,                                           
                          &quot;state&quot; : 1,                                            
                          &quot;stateStr&quot; : &quot;PRIMARY&quot;,                                 
                          &quot;uptime&quot; : 28658,                                       
                          &quot;optime&quot; : Timestamp(1445239630, 1),                    
                          &quot;optimeDate&quot; : ISODate(&quot;2015-10-19T07:27:10Z&quot;),         
                          &quot;electionTime&quot; : Timestamp(1445240879, 1),              
                          &quot;electionDate&quot; : ISODate(&quot;2015-10-19T07:47:59Z&quot;),       
                          &quot;self&quot; : true                                           
                  },    
                  {                                                               
                          &quot;_id&quot; : 1,                                              
                          &quot;name&quot; : &quot;mongo-rs1-2:27017&quot;,                           
                          &quot;health&quot; : 1,                                           
                          &quot;state&quot; : 2,                                            
                          &quot;stateStr&quot; : &quot;SECONDARY&quot;,                               
                          &quot;uptime&quot; : 28654,                                       
                          &quot;optime&quot; : Timestamp(1445239630, 1),                    
                          &quot;optimeDate&quot; : ISODate(&quot;2015-10-19T07:27:10Z&quot;),         
                          &quot;lastHeartbeat&quot; : ISODate(&quot;2015-10-19T15:45:30Z&quot;),      
                          &quot;lastHeartbeatRecv&quot; : ISODate(&quot;2015-10-19T15:45:30Z&quot;),  
                          &quot;pingMs&quot; : 0,                                           
                          &quot;syncingTo&quot; : &quot;mongo-rs1-1:27017&quot;                       
                  },  
                  {                                                               
                          &quot;_id&quot; : 2,                                              
                          &quot;name&quot; : &quot;mongo-rs1-3:27017&quot;,                           
                          &quot;health&quot; : 1,                                           
                          &quot;state&quot; : 2,                                            
                          &quot;stateStr&quot; : &quot;SECONDARY&quot;,                               
                          &quot;uptime&quot; : 28654,                                       
                          &quot;optime&quot; : Timestamp(1445239630, 1),                    
                          &quot;optimeDate&quot; : ISODate(&quot;2015-10-19T07:27:10Z&quot;),         
                          &quot;lastHeartbeat&quot; : ISODate(&quot;2015-10-19T15:45:29Z&quot;),      
                          &quot;lastHeartbeatRecv&quot; : ISODate(&quot;2015-10-19T15:45:30Z&quot;),  
                          &quot;pingMs&quot; : 1,                                           
                          &quot;syncingTo&quot; : &quot;mongo-rs1-1:27017&quot;                       
                  }                                                               
          ],                                                                      
          &quot;ok&quot; : 1                                                                
  }                          
  </code></pre>
  <p><strong>Tests I made:</strong></p>
  <ol>
  <li>I connected with my MongoDB client to the Mongos URL.</li>
  <li>I created a DB: &quot;testshard&quot;</li>
  <li>I created a document in &quot;testshard&quot;</li>
  <li>I logged in the 3 replica: mongo-rs1-* and mongo-rs2-* to check where the data was saved</li>
  </ol>
  <p><strong>Result</strong></p>
  <p>I noticed that the DB was created on RS1 (shard 1) and was replicated on the 3 replicas: mongo-rs1-1 , mongo-rs1-2, mongo-rs1-3. The DB was not created on the shard 2; I think this is normal as the sharding feature save on one shard but not both.</p>
  <p>From here, it looks like the replica works. But what about the shard 2? How to check if data can be saved to shard 2?</p>
  <p><strong>Test #2</strong></p>
  <ol>
  <li>I logged in the Mongos and I created the database rs2 which I force to rs2 in the database &quot;config&quot; of the Mongos.</li>
  <li>I create a document in testrs2.</li>
  </ol>
  <p>I was expecting to see the new documents created in rs2, but rs2 is still an empty DB.</p>
  <p><strong>Questions</strong></p>
  <p>Q1: How can I test the &quot;sharding&quot; feature? I would expect to save a document and see it in the right replica set (rs1 or rs2).</p>
  <p>Q2: How does Mongo know where to save the data between rs1 or rs2 ?</p>
  <p>Q3: Is there a way to automatize the sharding? I don't want to tell which database should go where, this should be automatic. I was expecting the data to be saved randomly between the 2 shards.</p>
  <p>Q4: What are the best backup strategies in my case? I guess I just need to do a mongodump of rs1 and rs2 ?</p>
  <p>Q5: Am I right if I say that the only access point to the MongoDB is through the Mongos? Nobody should query the replica set.</p>