mongodb分片集群(sharding with replica set)配置

一共有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数据量的时候,数据开始移到shard2和shard4。

经过测试发现达到1000万数据量之后,有sharding的数据库进行无索引查询耗时8秒(最大的sharding数据量在350万),而没有sharding的数据库耗时260秒…………

 

作者:lovecindywang
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
上一篇:使用 MongoDB 存储日志数据


下一篇:DBA大牛告诉你,如何让MySQL语句执行加速?