第一步.啟動相應的Mongod進程
mongod --fork --slave --port 27017 --replSet mySet --rest --dbpath /data/dbmongo/shard11 --logpath /data/dbmongo/shard11.logmongod --fork --slave --port 27018 --replSet mySet --rest --dbpath /data/dbmongo/shard12 --logpath /data/dbmongo/shard12.logmongod --fork --slave --port 27019 --replSet mySet --rest --dbpath /data/dbmongo/shard13 --logpath /data/dbmongo/shard13.logmongod --fork --slave --port 27020 --replSet mySet --rest --dbpath /data/dbmongo/shard14 --logpath /data/dbmongo/shard14.log
第二步.初始化 replica set
>config = {_id:'mySet',members:[{_id:0, host:'192.168.1.111:27017'},{_id:1, host:'192.168.1.112:27018'},{_id:1, host:'192.168.1.113:27019'},{_id:2, host:'192.168.1.114:27020'}}]}>rs.initiate(config);>rs.status();
第三步.配置一台Config Servers:
192.168.1.100mongod --fork --configsvr --port 20000 --rest --dbpath /data/dbmongo/config --logpath /data/dbmongo/config/config.log
第四步.配置一台Route Servers
mongos -fork --port 30000 --chunkSize 1 --configdb "192.168.1.100:20000" --logpath /data/dbmongo/mongos.log
第五步.Configuring the shard Cluster 串連到mongos進程,並切換到admin資料庫做以下配置
192.168.1.100>mongo 192.168.1.100:30000>use admin>db.runCommand({addshard:'mySet/192.168.1.115:27021,192.168.1.116:27022'});>db.runCommand({enablesharding:'test'});>db.runCommand({listshards:1});>printShardingStatus();>db.runCommand({shardcollection:'test.test',key:{_id:1},unique:true});
第六步.測試
>for(var i=1;i<=2011;i++)b.test.save({id:i,value1:"1234567890",value2:"1234567890",value3:"1234567890",value4:"1234567890"});