Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongoDB sharding example

Tags:

mongodb

Newbie using mongo 2.0.1 32-bit on windows tried testing out shards as follows:

(4) processes: 2 shards + config srver + mongos w tiny chunksize

mongod.exe --shardsvr  --port 10001  --dbpath <folder1>       > shard1.log
mongod.exe --shardsvr  --port 10002  --dbpath <folder2>       > shard2.log
mongod.exe --configsvr --port 20000  --dbpath <configfolder>  > config.log
mongos.exe --configdb localhost:20000 --chunkSize 1           > mongos.log

I ran the shell and set up 2 shards:

mongos> use admin
switched to dbadmin

mongos> db.runCommand( { addshard : "localhost:10001" } );
{ "shardAdded" : "shard0000", "ok" : 1 }

mongos> db.runCommand( { addshard : "localhost:10002" } );
{ "shardAdded" : "shard0001", "ok" : 1 }

Then I enabled sharding for a test database (dbTest) and collection (cTest):

mongos> db.runCommand( { enablesharding  : "dbTest" } );
{ "ok" : 1 }

mongos> db.runCommand( { shardcollection : "dbTest.cTest", key : { Name : 1 } } );
{ "collectionssharded" : "dbTest.cTest", "ok" : 1 }

Finally I populated the cTest collection (indexed by Name) with 1,000,005 sample records:

mongos> use dbTest
switched to db dbTest

db.cTest.drop();
db.cTest.ensureIndex({ Name : 1 });
db.cTest.save({Name: "Frank", Age:56, Job: "Accountant", State: "NY"});
db.cTest.save({Name: "Bill" , Age:23,                    State: "CA"});
db.cTest.save({Name: "Janet", Age:34, Job: "Dancer"                 });
db.cTest.save({Name: "Andy",  Age:44                                });
db.cTest.save({Name: "Zach",  Age:23, Job: "Fireman",    State: "CA"});
i=1;
while(i<=1000) 
{ 
  j=1;
  while (j<=1000) 
  { 
    db.cTest.save({Name:"Person("+i+","+j+")", Age:i+j}); 
    j = j+1
  }; 
  i=i+1; 
};

HOWEVER ...

It appears that nothing actually got sharded. In the config database, db.chunks.count() is zero, and I can see from windows explorer file sizes that all the data went into the physical file setup setup for the first shard, and none to the second.

Can anyone spot what I've done wrong, and also provide some tips on how to admin & debug this type of thing & see what's going on ?

Thanks

like image 992
tpascale Avatar asked May 04 '26 07:05

tpascale


1 Answers

Once you "shardcollection", don't drop it. It will remove metadata about sharded collection.

like image 100
Nat Avatar answered May 05 '26 21:05

Nat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!