#this is a sample scrip to create a sharding MongoDB system that consists of:
#	+Replicaset s0 (192.168.0.227:27000 as primary, 192.168.0.227:27001 as secondary, 192.168.0.227:27009 as arbiter  )
#	+Replicaset s1 (192.168.0.101:27010 as primary, 192.168.0.227:27011 as secondary, 192.168.0.227:27019 as arbiter  )
#	+Configuration servers(192.168.0.227:57040,192.168.0.227:57041,192.168.0.227:57042)
#	+Mongos (a.k.a router) at 192.168.0.227:27100


#Preprocessing: clear folders if any to run a fresh system
rm -rf /data/shard*
rm -rf /data/log*
rm -rf /data/config*


#Step 1: Create folder to store database files
mkdir -p /data/shard0/mem0 /data/shard0/mem1 /data/shard0/arb /data/log
mkdir -p /data/shard1/mem0 /data/shard1/mem1 /data/shard1/arb 
mkdir -p /data/config/cfg1 /data/config/cfg2 /data/config/cfg3 /data/log

#for windows users
mkdir C:\data\shard0\mem0 
mkdir C:\data\shard0\mem1 
mkdir C:\data\shard0\arb
mkdir C:\data\log
mkdir C:\data\shard1\mem0 
mkdir C:\data\shard1\mem1 
mkdir C\data\shard1\arb 


#Step 2: Start mongod deamons for Replicaset 0
mongod --shardsvr --replSet s0 --dbpath /data/shard0/mem0 --port 27000 --bind_ip_all --fork --logpath "/data/log/s0-m0.log" 
mongod --shardsvr --replSet s0 --dbpath /data/shard0/mem1 --port 27001 --bind_ip_all --fork --logpath "/data/log/s0-m1.log" 
mongod --shardsvr --replSet s0 --dbpath /data/shard0/arb --port 27009 --bind_ip_all --fork --logpath "/data/log/s0-ar.log" 

#for windows user
mongod --shardsvr --replSet s0 --dbpath C:\data\shard0\mem0 --port 27000 --bind_ip_all
mongod --shardsvr --replSet s0 --dbpath C:\data\shard0\mem1 --port 27001 --bind_ip_all
mongod --shardsvr --replSet s0 --dbpath C:\data\shard0\arb --port 27009 --bind_ip_all

#Connect to a mongod
mongo --port 27000 

#Inside mongo, let's init the replicaset
rs.initiate( {
   _id : "s0",
   members: [
      { _id: 0, host: "192.168.11.193:27000" },
      { _id: 1, host: "192.168.11.193:27001" }
   ]
})

#now add the arbiter to the replicaset
rs.addArb("192.168.11.193:27009")


#Step 3:Do the same for Replicaset 1
mongod --shardsvr --replSet s1 --dbpath /data/shard1/mem0 --port 27010 --bind_ip_all --fork --logpath "/data/log/s1-m0.log" 
mongod --shardsvr --replSet s1 --dbpath /data/shard1/mem1 --port 27011 --bind_ip_all --fork --logpath "/data/log/s1-m1.log" 
mongod --shardsvr --replSet s1 --dbpath /data/shard1/arb --port 27019 --bind_ip_all --fork --logpath "/data/log/s1-ar.log" 

mongo --port 27010 

rs.initiate( {
   _id : "s1",
   members: [
      { _id: 0, host: "192.168.0.101:27010" },
      { _id: 1, host: "192.168.0.101:27011" }
   ]
})
rs.addArb("192.168.0.101:27019")


#Step 4: Start mongod processes for configuration 
mongod --configsvr --replSet rsConf --dbpath /data/config/cfg1 --port 57040 --bind_ip_all --fork --logpath "/data/log/cfg1.log" 
mongod --configsvr --replSet rsConf --dbpath /data/config/cfg2 --port 57041 --bind_ip_all --fork --logpath "/data/log/cfg2.log"   
mongod --configsvr --replSet rsConf --dbpath /data/config/cfg3 --port 57042 --bind_ip_all --fork --logpath "/data/log/cfg3.log"  

#connect to one cfg server and init the replicaset for configuration
mongo --port 57040
rs.initiate(
  {
    _id: "rsConf",
    configsvr: true,
    members: [
      { _id : 0, host : "192.168.0.227:57040" },
      { _id : 1, host : "192.168.0.227:57041" },
      { _id : 2, host : "192.168.0.227:57042" },
    ]
  }
)

#Step 5:Start mongos (the router)
mongos --logpath "/data/log/mongos.log" --configdb rsConf/192.168.0.227:57040,192.168.0.227:57041,192.168.0.227:57042 --port 27100 --fork --bind_ip_all

#Step 6: Connect to the mongos from any machine on the network
mongo --port 27100

#inside mongo, now add replicaset to the 
sh.addShard( "s0/192.168.0.227:27000")
sh.addShard( "s1/192.168.0.101:27010")

#set chunk size
use config
db.settings.save( { _id:"chunksize", value: 8 } )

sh.enableSharding("shardDb")
use shardDb
sh.shardCollection("shardDb.res", {borough:1} )


################################################################################################################################
#Some useful commands in mongos or mongo
db.res.getShardDistribution()
rs.conf()  //show the replicaset configuration	
rs.status() //show teh replicaset status()

sh.status()


	

