mongodb分片集羣(sharding with replica set)配置 4servers

一共有4臺機器,各自掛接一個存儲,希望實現:

1,儘量節約存儲

2,高可用性

3,存儲大量數據

配置方案:

1,每一臺機器做一個分片的主數據庫

2,每一臺機器做一個分片的後備數據庫

3,每一臺機器做一個分片的仲裁服務

4,兩個兩個一組交叉作對方的後備

5,有三臺機器開配置服務

6,有一臺機器開路由服務(生產環境可以在每一臺Windows App服務器上開路由服務,App服務器集羣做負載均衡)

這樣的話,任何一臺服務器完全離線(或者交叉的兩臺服務器離線),都能保證整個系統正常運行。不過在服務器和服務器起來之後需要:

1,在升級成爲主數據庫的那個後備數據庫上運行rs.StepDown(100)來讓它讓出主數據庫的位置,因爲每一個分片的活動數據庫都應該由獨立服務器擔當,以獲得最好的性能

2,配置服務和後備數據庫起來之後不需要做任何調整

 

192.168.129.142

mkdir -p /usr/data/shard1 
mkdir -p /usr/data/shard2 
mkdir -p /usr/data/shard3 
mkdir -p /usr/data/config1 
/usr/local/mongodb-linux-x86_64-1.6.4/bin/mongod --fork --shardsvr --port 10001 --dbpath /usr/data/shard1/ --logpath /usr/data/shard1/log.log --replSet shard1 --rest 
/usr/local/mongodb-linux-x86_64-1.6.4/bin/mongod --fork --shardsvr --port 10002 --dbpath /usr/data/shard2/ --logpath /usr/data/shard2/log.log --replSet shard2 --rest 
/usr/local/mongodb-linux-x86_64-1.6.4/bin/mongod --fork --shardsvr --port 10003 --dbpath /usr/data/shard3/ --logpath /usr/data/shard3/log.log --replSet shard3 --rest 
/usr/local/mongodb-linux-x86_64-1.6.4/bin/mongod --fork --configsvr --port 20001 --dbpath /usr/data/config1/ --logpath /usr/data/config1/log.log --rest

ps aux | grep mongodb | grep -v grep 
/usr/local/mongodb-linux-x86_64-1.6.4/bin/mongo --port 10001

   config = {_id: 'shard1', members: [ 
        {_id: 0, host: '192.168.129.142:10001'}, 
        {_id: 1, host: '192.168.129.172:10001'}, 
        {_id: 2, host: '192.168.129.173:10001', arbiterOnly: true} 
    ]} 
    rs.initiate(config) 
    rs.status()


192.168.129.172

mkdir -p /usr/data/shard2 
mkdir -p /usr/data/shard1 
mkdir -p /usr/data/shard4 
mkdir -p /usr/data/config2 
/usr/local/mongodb-linux-x86_64-1.6.4/bin/mongod --fork --shardsvr --port 10002 --dbpath /usr/data/shard2/ --logpath /usr/data/shard2/log.log --replSet shard2 --rest 
/usr/local/mongodb-linux-x86_64-1.6.4/bin/mongod --fork --shardsvr --port 10001 --dbpath /usr/data/shard1/ --logpath /usr/data/shard1/log.log --replSet shard1 --rest 
/usr/local/mongodb-linux-x86_64-1.6.4/bin/mongod --fork --shardsvr --port 10004 --dbpath /usr/data/shard4/ --logpath /usr/data/shard4/log.log --replSet shard4 --rest 
/usr/local/mongodb-linux-x86_64-1.6.4/bin/mongod --fork --configsvr --port 20002 --dbpath /usr/data/config2/ --logpath /usr/data/config2/log.log --rest

ps aux | grep mongodb | grep -v grep

/usr/local/mongodb-linux-x86_64-1.6.4/bin/mongo --port 10002

  config = {_id: 'shard2', members: [ 
        {_id: 0, host: '192.168.129.172:10002'}, 
        {_id: 1, host: '192.168.129.142:10002'}, 
        {_id: 2, host: '192.168.129.175:10002', arbiterOnly: true} 
    ]} 
    rs.initiate(config) 
    rs.status()


192.168.129.173

mkdir -p /usr/data/shard3 
mkdir -p /usr/data/shard4 
mkdir -p /usr/data/shard1 
mkdir -p /usr/data/config3 
/usr/local/mongodb-linux-x86_64-1.6.4/bin/mongod --fork --shardsvr --port 10003 --dbpath /usr/data/shard3/ --logpath /usr/data/shard3/log.log --replSet shard3 --rest 
/usr/local/mongodb-linux-x86_64-1.6.4/bin/mongod --fork --shardsvr --port 10004 --dbpath /usr/data/shard4/ --logpath /usr/data/shard4/log.log --replSet shard4 --rest 
/usr/local/mongodb-linux-x86_64-1.6.4/bin/mongod --fork --shardsvr --port 10001 --dbpath /usr/data/shard1/ --logpath /usr/data/shard1/log.log --replSet shard1 --rest 
/usr/local/mongodb-linux-x86_64-1.6.4/bin/mongod --fork --configsvr --port 20003 --dbpath /usr/data/config3/ --logpath /usr/data/config3/log.log --rest

ps aux | grep mongodb | grep -v grep 
/usr/local/mongodb-linux-x86_64-1.6.4/bin/mongo --port 10003

  config = {_id: 'shard3', members: [ 
        {_id: 0, host: '192.168.129.173:10003'}, 
        {_id: 1, host: '192.168.129.175:10003'}, 
        {_id: 2, host: '192.168.129.142:10003', arbiterOnly: true} 
    ]} 
    rs.initiate(config) 
    rs.status()


192.168.129.175

mkdir -p /usr/data/shard4 
mkdir -p /usr/data/shard3 
mkdir -p /usr/data/shard2 
mkdir -p /usr/data/master 
/usr/local/mongodb-linux-x86_64-1.6.4/bin/mongod --fork --shardsvr --port 10004 --dbpath /usr/data/shard4/ --logpath /usr/data/shard4/log.log --replSet shard4 --rest 
/usr/local/mongodb-linux-x86_64-1.6.4/bin/mongod --fork --shardsvr --port 10003 --dbpath /usr/data/shard3/ --logpath /usr/data/shard3/log.log --replSet shard3 --rest 
/usr/local/mongodb-linux-x86_64-1.6.4/bin/mongod --fork --shardsvr --port 10002 --dbpath /usr/data/shard2/ --logpath /usr/data/shard2/log.log --replSet shard2 --rest 
/usr/local/mongodb-linux-x86_64-1.6.4/bin/mongos --fork --port 40000 --logpath /usr/data/master/log.log  --chunkSize 1 

"192.168.129.142:20001,192.168.129.172:20002,192.168.129.173:20003"

ps aux | grep mongodb | grep -v grep 
/usr/local/mongodb-linux-x86_64-1.6.4/bin/mongo --port 10004

config = {_id: 'shard4', members: [ 
        {_id: 0, host: '192.168.129.175:10004'}, 
        {_id: 1, host: '192.168.129.173:10004'}, 
        {_id: 2, host: '192.168.129.172:10004', arbiterOnly: true} 
    ]} 
    rs.initiate(config) 
    rs.status()

/usr/local/mongodb-linux-x86_64-1.6.4/bin/mongo --port 40000 
use admin 
db.runCommand({ addshard:'shard1/192.168.129.142:10001,192.168.129.172:10001' }) 
db.runCommand({ addshard:'shard2/192.168.129.172:10002,192.168.129.142:10002' }) 
db.runCommand({ addshard:'shard3/192.168.129.173:10003,192.168.129.175:10003' }) 
db.runCommand({ addshard:'shard4/192.168.129.175:10004,192.168.129.173:10004' }) 
db.runCommand({ listshards:1 })

db.runCommand({ enablesharding:'test' }) 
db.runCommand({ shardcollection:'test.data', key:{_id:1} }) 
printShardingStatus()

 

插入大量數據後的結果:

db.data.stats() 

    "sharded" : true, 
    "ns" : "test.data", 
    "count" : 1992002, 
    "size" : 2103555152, 
    "avgObjSize" : 1056.0005220878293, 
    "storageSize" : 2464232960, 
    "nindexes" : 1, 
    "nchunks" : 23, 
    "shards" : { 
        "shard1" : { 
            "ns" : "test.data", 
            "count" : 1271788, 
            "size" : 1343008448, 
            "avgObjSize" : 1056.0002516142627, 
            "storageSize" : 1568785152, 
            "numExtents" : 25, 
            "nindexes" : 1, 
            "lastExtentSize" : 267987712, 
            "paddingFactor" : 1, 
            "flags" : 1, 
            "totalIndexSize" : 52658176, 
            "indexSizes" : { 
                "_id_" : 52658176 
            }, 
            "ok" : 1 
        }, 
        "shard2" : { 
            "ns" : "test.data", 
            "count" : 98494, 
            "size" : 104009808, 
            "avgObjSize" : 1056.001462017991, 
            "storageSize" : 111137536, 
            "numExtents" : 12, 
            "nindexes" : 1, 
            "lastExtentSize" : 25047552, 
            "paddingFactor" : 1, 
            "flags" : 1, 
            "totalIndexSize" : 4087808, 
            "indexSizes" : { 
                "_id_" : 4087808 
            }, 
            "ok" : 1 
        }, 
        "shard3" : { 
            "ns" : "test.data", 
            "count" : 487410, 
            "size" : 514705248, 
            "avgObjSize" : 1056.000590878316, 
            "storageSize" : 607047424, 
            "numExtents" : 20, 
            "nindexes" : 1, 
            "lastExtentSize" : 107698688, 
            "paddingFactor" : 1, 
            "flags" : 1, 
            "totalIndexSize" : 20250624, 
            "indexSizes" : { 
                "_id_" : 20250624 
            }, 
            "ok" : 1 
        }, 
        "shard4" : { 
            "ns" : "test.data", 
            "count" : 134310, 
            "size" : 141831648, 
            "avgObjSize" : 1056.0021442930533, 
            "storageSize" : 177262848, 
            "numExtents" : 14, 
            "nindexes" : 1, 
            "lastExtentSize" : 36068352, 
            "paddingFactor" : 1, 
            "flags" : 1, 
            "totalIndexSize" : 5570560, 
            "indexSizes" : { 
                "_id_" : 5570560 
            }, 
            "ok" : 1 
        } 
    }, 
    "ok" : 1 
}

 

printShardingStatus()                   
--- Sharding Status --- 
  sharding version: { "_id" : 1, "version" : 3 }
  shards:
      {
 "_id" : "shard1",
 "host" : "shard1/192.168.129.142:10001,192.168.129.172:10001"
}
      {
 "_id" : "shard2",
 "host" : "shard2/192.168.129.172:10002,192.168.129.142:10002"
}
      {
 "_id" : "shard3",
 "host" : "shard3/192.168.129.173:10003,192.168.129.175:10003"
}
      {
 "_id" : "shard4",
 "host" : "shard4/192.168.129.175:10004,192.168.129.173:10004"
}
  databases:
 { "_id" : "admin", "partitioned" : false, "primary" : "config" }
 { "_id" : "test", "partitioned" : true, "primary" : "shard1" }
  test.data chunks:
   { "_id" : { $minKey : 1 } } -->> { "_id" : ObjectId("4d01c5bc926adb17b8000001") } on : shard2 { "t" : 8000, "i" : 0 }
   { "_id" : ObjectId("4d01c5bc926adb17b8000001") } -->> { "_id" : ObjectId("4d01c620926adb17b800d1dd") } on : shard2 { "t" : 10000, "i" : 0 }
   { "_id" : ObjectId("4d01c620926adb17b800d1dd") } -->> { "_id" : ObjectId("4d01c669926adb17b80180bf") } on : shard4 { "t" : 9000, "i" : 0 }
   { "_id" : ObjectId("4d01c669926adb17b80180bf") } -->> { "_id" : ObjectId("4d01c6b6926adb17b8022fa1") } on : shard2 { "t" : 12000, "i" : 0 }
   { "_id" : ObjectId("4d01c6b6926adb17b8022fa1") } -->> { "_id" : ObjectId("4d01c6fd926adb17b802de83") } on : shard1 { "t" : 5000, "i" : 1 }
   { "_id" : ObjectId("4d01c6fd926adb17b802de83") } -->> { "_id" : ObjectId("4d01c74c926adb17b8038d65") } on : shard4 { "t" : 11000, "i" : 0 }
   { "_id" : ObjectId("4d01c74c926adb17b8038d65") } -->> { "_id" : ObjectId("4d01c795926adb17b8043c47") } on : shard4 { "t" : 13000, "i" : 0 }
   { "_id" : ObjectId("4d01c795926adb17b8043c47") } -->> { "_id" : ObjectId("4d01c7e6926adb17b804eb29") } on : shard1 { "t" : 6000, "i" : 2 }
   { "_id" : ObjectId("4d01c7e6926adb17b804eb29") } -->> { "_id" : ObjectId("4d01c82f926adb17b8059a0b") } on : shard1 { "t" : 6000, "i" : 4 }
   { "_id" : ObjectId("4d01c82f926adb17b8059a0b") } -->> { "_id" : ObjectId("4d01c8d4926adb17b806f7cf") } on : shard1 { "t" : 12000, "i" : 1 }
   { "_id" : ObjectId("4d01c8d4926adb17b806f7cf") } -->> { "_id" : ObjectId("4d01c963926adb17b8085593") } on : shard3 { "t" : 7000, "i" : 2 }
   { "_id" : ObjectId("4d01c963926adb17b8085593") } -->> { "_id" : ObjectId("4d01ca1c926adb17b809b357") } on : shard3 { "t" : 7000, "i" : 4 }
   { "_id" : ObjectId("4d01ca1c926adb17b809b357") } -->> { "_id" : ObjectId("4d01caf7926adb17b80b306a") } on : shard3 { "t" : 7000, "i" : 6 }
   { "_id" : ObjectId("4d01caf7926adb17b80b306a") } -->> { "_id" : ObjectId("4d01cbc2926adb17b80d09fd") } on : shard3 { "t" : 13000, "i" : 2 }
   { "_id" : ObjectId("4d01cbc2926adb17b80d09fd") } -->> { "_id" : ObjectId("4d01cc54926adb17b80e67c1") } on : shard3 { "t" : 14000, "i" : 1 }
   { "_id" : ObjectId("4d01cc54926adb17b80e67c1") } -->> { "_id" : ObjectId("4d01cec7926adb125c00d1dc") } on : shard1 { "t" : 14000, "i" : 2 }
   { "_id" : ObjectId("4d01cec7926adb125c00d1dc") } -->> { "_id" : ObjectId("4d01ced2926adb125c022fa0") } on : shard1 { "t" : 14000, "i" : 4 }
   { "_id" : ObjectId("4d01ced2926adb125c022fa0") } -->> { "_id" : ObjectId("4d01cedf926adb125c038d64") } on : shard1 { "t" : 14000, "i" : 6 }
   { "_id" : ObjectId("4d01cedf926adb125c038d64") } -->> { "_id" : ObjectId("4d01ceeb926adb125c04eb28") } on : shard1 { "t" : 14000, "i" : 8 }
   { "_id" : ObjectId("4d01ceeb926adb125c04eb28") } -->> { "_id" : ObjectId("4d01cf1a926adb125c07a6ab") } on : shard1 { "t" : 14000, "i" : 10 }
   { "_id" : ObjectId("4d01cf1a926adb125c07a6ab") } -->> { "_id" : ObjectId("4d01cf3c926adb125c0a622e") } on : shard1 { "t" : 14000, "i" : 12 }
   { "_id" : ObjectId("4d01cf3c926adb125c0a622e") } -->> { "_id" : ObjectId("4d01cf52926adb125c0d1db1") } on : shard1 { "t" : 14000, "i" : 14 }
   { "_id" : ObjectId("4d01cf52926adb125c0d1db1") } -->> { "_id" : ObjectId("4d01d58c926adb16480096f4") } on : shard1 { "t" : 14000, "i" : 16 }
   { "_id" : ObjectId("4d01d58c926adb16480096f4") } -->> { "_id" : { $maxKey : 1 } } on : shard1 { "t" : 14000, "i" : 17 }

 

另,這套配置插入1萬條1KB數據的時間大約在1.4秒,如果使用最簡單單進程配置的話速度稍快,在1.2秒,性能下降不是很厲害,可以接受。

在這裏我們配置路由服務1M數據作爲一個分塊,大約在10M數據量的時候,數據開始寫入shard3,在500M數據量的時候,數據開始移到shard2shard4

經過測試發現達到1000萬數據量之後,有sharding的數據庫進行無索引查詢耗時8秒(最大的sharding數據量在350萬),而沒有sharding的數據庫耗時260…………

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章