#### WARNING: This documenmt needs a run through and a few things left too. # I am going to assume we are not using thetemporary # Mongo servers and they are not running. # This will delete them. # If restarting, kill the mogo processes and then do rm -rf mongo/d1 mongo/d2 mongo/d3 mongo/logs # Setup 3 directories: mkdir -p mongo/d1 mongo/d2 mongo/d3 mongo/logs # If not AWS, ignore or already did it. sudo -s -- bash -c ' echo "127.0.0.1 localhost ">> /etc/hosts ' # Start the mongod with shard option. mongod --shardsvr --dbpath mongo/d2 --smallfiles --oplogSize 50 --bind_ip 127.0.0.1 > mongo/logs/2.log 2>&1 & # Start a mongo config server --- in the Sharding2 document, we will # have 3 in a replica set. mongod --configsvr --replSet c --dbpath mongo/d1 --smallfiles --oplogSize 50 --bind_ip 127.0.0.1 > mongo/logs/1.log 2>&1 & # Stupid but single config server still has to be in a replica set. mongo --port 27019 --eval "rs.initiate({ _id: 'c', version: 1, members: [ {_id :0, host: 'localhost:27019' } ] })" # Start a mongos -- this connects to the mongod, since the default for a config server is 27019 mongos --configdb "c/127.0.0.1:27019" --bind_ip 127.0.0.1 > mongo/logs/ms_1.log 2>&1 & # Start a mongo, which will connect to mongos to execute commands # Change the data server to a shard server. mongo --eval 'sh.addShard( "localhost:27018" )' # Lets add some data. echo " function fillData() { db = db.getSiblingDB('test') db.no.insert({valiue: 1}); db.no.drop(); for( var i = 0; i < 1000; i++ ) { db.no.insert( {value: i } ); } } fillData() " > mongo/fill.js mongo mongo/fill.js #Shard the database. mongo --eval 'sh.enableSharding("test")' # Add an index which is required for sharding. mongo --eval 'db.no.createIndex( { value : 1} )' test # shard the collection. mongo --eval 'sh.shardCollection("test.no", {value: 1})' #Now add a 2nd shard. mongod --shardsvr --dbpath mongo/d3 --smallfiles --oplogSize 50 --bind_ip 127.0.0.1 --port 4000 > mongo/logs/3.log 2>&1 & sleep 1 mongo --eval 'sh.addShard( "localhost:4000" )' # Move some chunks over to 2nd server mongo --quiet --eval 'sh.status()' | egrep '\-\->>|state' # Move chunk mongo --quiet --eval 'sh.moveChunk("test.no", { value: "500" }, "shard0001")' mongo --quiet --eval 'sh.status()' | egrep '\-\->>|state' # Move it bak mongo --quiet --eval 'sh.moveChunk("test.no", { value: "500" }, "shard0000")' # Now split it so it is uneven. Made a Mistake? mongo --quiet --eval 'sh.splitAt( "test.no", { "value": "100" } )' # move the 2nd half to second server. mongo --quiet --eval 'sh.status()' | egrep '\-\->>|state' mongo --quiet --eval 'sh.moveChunk("test.no", { value: "500" }, "shard0001")' mongo --quiet --eval 'sh.status()' | egrep '\-\->>|state' mongo --quiet --eval 'sh.splitAt( "test.no", { "value": "200" } )' mongo --quiet --eval 'sh.splitAt( "test.no", { "value": "300" } )' mongo --quiet --eval 'sh.splitAt( "test.no", { "value": "400" } )' mongo --quiet --eval 'sh.splitAt( "test.no", { "value": "500" } )' mongo --quiet --eval 'sh.splitAt( "test.no", { "value": "600" } )' mongo --quiet --eval 'sh.splitAt( "test.no", { "value": "700" } )' mongo --quiet --eval 'sh.splitAt( "test.no", { "value": "800" } )' mongo --quiet --eval 'sh.splitAt( "test.no", { "value": "900" } )' # Turn on the balancer. mongo --quiet --eval 'sh.getBalancerHost()' mongo --quiet --eval 'sh.isBalancerRunning()' mongo --quiet --eval 'sh.setBalancerState(true)' mongo --quiet --eval 'sh.isBalancerRunning()' # sleep for a few seconds echo "Sleep for 5 seconds to let the balancer do its stuff". sleep 5 mongo --quiet --eval 'sh.status()' | egrep '\-\->>|state'