先上一张拓扑
所有mongo全部安装到本地(windows)
其中:
分片端口27017
副本1端口27018
仲裁端口27019
路由端口27020
配置端口27021
在各mongo目录下的bin文件里创建配置文件
#shard.conf
dbpath=D:\mongo\shard\mongodb-win32-x86_64-2.6.6\data
logpath=D:\mongo\shard\mongodb-win32-x86_64-2.6.6\log\log.log
pidfilepath=D:\mongo\shard\mongodb-win32-x86_64-2.6.6\pid\pid.pid
directoryperdb=true
logappend=true
replSet=testrs
bind_ip=127.0.0.1
port=27017
oplogSize=10000
noprealloc=true
#slaver.conf
dbpath=D:\mongo\shard\slaver\mongodb-win32-x86_64-2.6.6\data
logpath=D:\mongo\shard\slaver\mongodb-win32-x86_64-2.6.6\log\log.log
pidfilepath=D:\mongo\shard\slaver\mongodb-win32-x86_64-2.6.6\pid\pid.pid
directoryperdb=true
logappend=true
replSet=testrs
bind_ip=127.0.0.1
port=27018
oplogSize=10000
noprealloc=true
#arbiter.conf
dbpath=D:\mongo\shard\arbiter\mongodb-win32-x86_64-2.6.6\data
logpath=D:\mongo\shard\arbiter\mongodb-win32-x86_64-2.6.6\log\log.log
pidfilepath=D:\mongo\shard\arbiter\mongodb-win32-x86_64-2.6.6\pid\pid.pid
directoryperdb=true
logappend=true
replSet=testrs
bind_ip=127.0.0.1
port=27019
oplogSize=10000
noprealloc=true
#mongos.conf
logpath=D:\mongo\mongos\mongodb-win32-x86_64-2.6.6\log\log.log
pidfilepath=D:\mongo\mongos\mongodb-win32-x86_64-2.6.6\pid\pid.pid
bind_ip=127.0.0.1
port=27020
configdb=127.0.0.1:27021
#mongos.conf
dbpath=D:\mongo\config\mongodb-win32-x86_64-2.6.6\data
logpath=D:\mongo\config\mongodb-win32-x86_64-2.6.6\log\log.log
pidfilepath=D:\mongo\config\mongodb-win32-x86_64-2.6.6\pid\pid.pid
bind_ip=127.0.0.1
port=27021
副本:
启动分片、副本、仲裁
mongod -f 配置文件
连接到分片执行如下操作
use admin
cfg={_id:"testrs",members:[{_id:0,host:"127.0.0.1:27017",priority:2},{_id:1,host:"127.0.0.1:27018",priority:1},{_id:2,host:"127.0.0.1:27019",arbiterOnly:true}]};
分片:
启动配置
mongod -f 配置文件
启动路由
mongos -f 配置文件
连接路由,使用admin添加分片
use admin
db.runCommand({"addShard":"testrs/127.0.0.1:27017"})
开启分片及设置片键
db.runCommand({"enablesharding":"test"})
db.runCommand({"shardcollection":"test.person","key":{_id:'hashed'}})
查看状态
printShardingStatus()
--- Sharding Status --- sharding version: { "_id" : 1, "version" : 4, "minCompatibleVersion" : 4, "currentVersion" : 5, "clusterId" : ObjectId("56a9e3c876c1778c5a28ae2b")} shards: { "_id" : "testrs", "host" : "testrs/127.0.0.1:27017,127.0.0.1:27018" } databases: { "_id" : "admin", "partitioned" : false, "primary" : "config" } { "_id" : "test", "partitioned" : true, "primary" : "testrs" } test.person shard key: { "_id" : "hashed" } chunks: testrs 2 { "_id" : { "$minKey" : 1 } } -->> { "_id" : NumberLong(0) } on : testrs Timestamp(1, 1) { "_id" : NumberLong(0) } -->> { "_id" : { "$maxKey" : 1 } } on : testrs Timestamp(1, 2)