[MongoDB]Shard
建置流程簡述
- 建置ConfigService
- 建置RouteService
- 新增分片(Mongod)
- 於Shell下 添加分片至RouteService
- 開啟分片功能
- 新增Shard key
此篇文章實作目標 如圖
1.建置 ConfigService
-
1: $mongod --dbpath=C:\ShardDBsForMongoDB\Cofigdata\CofigdataA --port 20000
2.建置RouteService
-
1: mongos --port 3000 --configdb localhost:20000
3.新增分片(Mongod)
-
ShardA建置
-
1: $mongod.exe --dbpath C:\ShardDBsForMongoDB\ShardAdata\ShardA1data --port 15000 --replSet shard0/localhost:15001
-
1: $mongod.exe --dbpath C:\ShardDBsForMongoDB\ShardAdata\ShardA2data --port 15001 --replSet shard0/localhost:15000
-
1: mongos>db.runCommand(
2: {
3: "replSetInitiate":
4: {
5: "_id":"shard0","members":
6: [
7: {
8: "_id":1,
9: "host":"localhost:15000"
10: },
11: {
12: "_id":2,
13: "host":"localhost:15001"
14: }
15: ]
16: }
17: }
18: )
-
-
ShardB建置
-
1: $mongod.exe --dbpath C:\ShardDBsForMongoDB\ShardBdata\ShardB1data --port 16000 --replSet shard1/localhost:16001
-
1: $mongod.exe --dbpath C:\ShardDBsForMongoDB\ShardBdata\ShardB2data --port 16001 --replSet shard1/localhost:16000
-
1: mongos>db.runCommand(
2: {
3: "replSetInitiate":
4: {
5: "_id":"shard1","members":
6: [
7: {
8: "_id":1,
9: "host":"localhost:16000"
10: },
11: {
12: "_id":2,
13: "host":"localhost:16001"
14: }
15: ]
16: }
17: }
18: )
-
-
ShardC建置
-
1: $mongod.exe --dbpath C:\ShardDBsForMongoDB\ShardCdata\ShardC1data --port 18000 --replSet shard2/localhost:18001
-
1: $mongod.exe --dbpath C:\ShardDBsForMongoDB\ShardCdata\ShardC2data --port 18001 --replSet shard2/localhost:18000
-
1: mongos>db.runCommand(
2: {
3: "replSetInitiate":
4: {
5: "_id":"shard2","members":
6: [
7: {
8: "_id":1,
9: "host":"localhost:18000"
10: },
11: {
12: "_id":2,
13: "host":"localhost:18001"
14: }
15: ]
16: }
17: }
18: )
-
4.於Shell下 添加分片至RouteService
-
連線至Mongos
1: $mongo --port 3000
-
添加分片
1: mongos> db.runCommand({addshard:'shard0/localhost:15000',allowLocal:true})
2: mongos> db.runCommand({addshard:'shard1/localhost:16000',allowLocal:true})
3: mongos> db.runCommand({addshard:'shard2/localhost:18000',allowLocal:true})
5.開啟分片功能
-
1: mongos>db.runCommand({"enablesharding":"wfm"})
6.新增Shard key
-
1: db.runCommand( { shardCollection: "wfm.VoiceRecords", key: { CreatAt:1,Site:1 } } )
7.檢驗Shard 是否已被建立
-
1: mongos> use config
-
1: mongos>db.shards.find()
-
由下圖可看到Shard0,Shard1,Shard2 已被加入該config內。
By-藍小伙