從節點默認不對外提供讀服務(由於主從延遲會致使從從節點上讀到髒數據)。服務器
複製集不能容忍一半以上發生故障(爲了確保不會發生一個複製集中出現多個主節點的狀況)。app
Shard1_ReplicaMember1 spa
mongod --port 30001 --logpath cluster/log/Shard1_ReplicaMember1.log --logappend --shardsvr --replSet Shard1 -dbpath cluster/data/Shard1_ReplicaMember1 --directoryperdb --rest
Shard1_ReplicaMember2 rest
mongod --port 30002 --logpath cluster/log/Shard1_ReplicaMember2.log --logappend --shardsvr --replSet Shard1 -dbpath cluster/data/Shard1_ReplicaMember2 --directoryperdb --rest
Shard1_ReplicaMember3 code
mongod --port 30003 --logpath cluster/log/Shard1_ReplicaMember3.log --logappend --shardsvr --replSet Shard1 -dbpath cluster/data/Shard1_ReplicaMember3 --directoryperdb --rest
Shard2_ReplicaMember1 blog
mongod --port 40001 --logpath cluster/log/Shard2_ReplicaMember1.log --logappend --shardsvr --replSet Shard2 -dbpath cluster/data/Shard2_ReplicaMember1 --directoryperdb --rest
Shard2_ReplicaMember2 部署
mongod --port 40002 --logpath cluster/log/Shard2_ReplicaMember2.log --logappend --shardsvr --replSet Shard2 -dbpath cluster/data/Shard2_ReplicaMember2 --directoryperdb --rest
Shard2_ReplicaMember3 it
mongod --port 40003 --logpath cluster/log/Shard2_ReplicaMember3.log --logappend --shardsvr --replSet Shard2 -dbpath cluster/data/Shard2_ReplicaMember3 --directoryperdb --rest
ConfigA io
mongod --port 20001 --logpath cluster/log/ConfigA.log --logappend --configsvr -dbpath cluster/config_data/ConfigA
ConfigB ast
mongod --port 20002 --logpath cluster/log/ConfigB.log --logappend --configsvr -dbpath cluster/config_data/ConfigB
先啓動
mongos --port 10001 --logpath cluster/log/mongos.log --logappend --configdb 127.0.0.1:20001,127.0.0.1:20002
再配置
mongo 127.0.0.1:27017/admindb.runCommand({ addshard : "Shard1/127.0.0.1:30001",name:"Shard1"})db.runCommand({ addshard : "Shard2/127.0.0.1:40001",name:"Shard2"})db.runCommand({ listshards : 1})db.runCommand({ enablesharding : "test"})db.runCommand({ shardcollection : "test.c1",key : {_id: 1} })