MongoDB分片集群原理、搭建及測試詳解


隨着技術的發展,目前數據庫系統對於海量數據的存儲和高效訪問海量數據要求越來越高,MongoDB分片機制就是為了解決海量數據的存儲和高效海量數據訪問而生。
MongoDB分片集群由mongos路由進程(輕量級且非持久化進程)、復制集組成的片shards(分片一般基於復制集故障轉移和冗余備份功能)、一組配置服務器(存儲元數據信息,一般冗余3台)構成。

一、部署MongoDB分片集群

mongod參數可以通過"mongod --help"查看。
mongos參數可以通過"mongos --help"查看。

1、配置復制集rs0:

參考文檔:http://www.cnblogs.com/wcwen1990/p/8053860.html

創建rs0復制集數據目錄、日志目錄和配置文件。

rs0配置文件:

cat /home/mongodb/db_rs0/config_rs0/rs0.conf

dbpath = /home/mongodb/db_rs0/data/rs0
logpath = /home/mongodb/db_rs0/logs/rs0.log
logappend = true
journal = true
port = 40000
fork = true
maxConns = 5000
bind_ip = 0.0.0.0
replSet = rs0
shardsvr = true
auth = false

2、配置復制集rs1:

參考文檔:http://www.cnblogs.com/wcwen1990/p/8053860.html

創建rs1復制集數據目錄、日志目錄和配置文件。

rs1配置文件:

cat /home/mongodb/db_rs1/config_rs1/rs1.conf

dbpath = /home/mongodb/db_rs1/data/rs1
logpath = /home/mongodb/db_rs1/logs/rs1.log
logappend = true
journal = true
port = 40001
fork = true
maxConns = 5000
bind_ip = 0.0.0.0
replSet = rs1
shardsvr = true
auth = false

3、配置configura服務器,共3台:

configura服務器也是一個mongod進程,它與我們熟悉的普通mongod進程沒有本質區別,只是它上面的數據庫和集合存儲的是分片集群的元數據信息。
創建configura服務器的數據目錄、日志目錄和配置文件。

configura配置文件:

cat /home/mongodb/db_configs/config_cfgserver/cfgserver.conf

dbpath = /home/mongodb/db_configs/data/db_config
logpath = /home/mongodb/db_configs/logs/config.log
logappend = true
port = 40002
maxConns = 5000
bind_ip = 0.0.0.0
replSet = cfgset
configsvr = true
auth = false
fork = true

4、配置路由服務器:

mongos路由進程功能為整個分片集群構建一個統一的訪問客戶端,使復雜的分片集群對用戶來說是透明的。上文提到過mongos路由進程是一個輕量級且非持久化的進程,其原因是它不需要像其他進程一樣創建數據目錄dbpath,只需要創建一個日志目錄即可。

mongos配置文件內容如下:

cat /home/mongodb/mongos/cfg_mongos.conf

logpath = /home/mongodb/mongos/logs/mongos.log
logappend = true
port = 40003
fork = true
maxConns = 5000
bind_ip = 0.0.0.0
configdb = cfgset/db01:40002,db02:40002,db03:40002

5、分別啟動步驟1、步驟2、步驟3、步驟4配置的9個mongod進程和1個mongos進程:

1)啟動三個rs0復制集:

bin/mongod --config /home/mongodb/db_rs0/config_rs0/rs0.conf

2)啟動三個rs1復制集:

bin/mongod --config /home/mongodb/db_rs1/config_rs1/rs1.conf

3)啟動三個配置服務器,並且初始化配置服務器:

bin/mongod --config /home/mongodb/db_configs/config_cfgserver/cfgserver.conf

登錄配置服務器:

bin/mongo --port 40003

執行初始化操作:

rs.initiate({_id:"cfgset",configsvr:true, members:[{_id:1,host:"db01:40002"},{_id:2,host:"db02:40002"},{_id:3,host:"db03:40002"}]})

4)啟動mongos服務器:

bin/mongos --config /home/mongodb/mongos/cfg_mongos.conf

6、添加各個分片到集群

上面已經完成了兩個片(復制集)、三個配置服務器、一個路由服務器的配置工作。接下來,我們要將各個分片添加到集群中。

1)打開一個mongo客戶端連接mongos服務器:

bin/mongo --port 40003

2)添加兩個分片到集群:

sh.addShard("rs0/db01:40000,db02:40000")
sh.addShard("rs1/db01:40001,db02:40001")

3)通過sh.status()檢查上面配置是否正確:

MongoDB Enterprise mongos> sh.status()
--- Sharding Status ---
   sharding version: {
       "_id" : 1,
       "minCompatibleVersion" : 5,
       "currentVersion" : 6,
       "clusterId" : ObjectId("5a37e16fd7903ab63ab90ebf")
   }
   shards:
         {  "_id" : "rs0",  "host" : "rs0/db01:40000,db02:40000",  "state" : 1 }
         {  "_id" : "rs1",  "host" : "rs1/db01:40001,db02:40001",  "state" : 1 }
   active mongoses:
         "3.6.0" : 1
   autosplit:
         Currently enabled: yes
   balancer:
         Currently enabled:  yes
         Currently running:  no
         Failed balancer rounds in last 5 attempts:  0
         Migration Results for the last 24 hours:
                 No recent migrations
   databases:
         {  "_id" : "config",  "primary" : "config",  "partitioned" : true }

4)查看分片集群數據庫信息:

MongoDB Enterprise mongos> show dbs
admin   0.000GB
config  0.000GB
MongoDB Enterprise mongos> db
test
MongoDB Enterprise mongos> use config
switched to db config
MongoDB Enterprise mongos> show collections
changelog
chunks
lockpings
locks
migrations
mongos
shards
tags
transactions
version

至此,MongoDB分片集群部署成功,生產部署還需要調整一些參數,這部門內容可以通過--help查看參數詳情。

三、測試MongoDB分片集群

1、向集群插入文檔:

MongoDB Enterprise mongos> use chavin
switched to db chavin
MongoDB Enterprise mongos> db.users.insert({userid:1,username:"ChavinKing",city:"beijing"})
WriteResult({ "nInserted" : 1 })

MongoDB Enterprise mongos> db.users.find()
{ "_id" : ObjectId("5a37eabafa5fcca8c960e893"), "userid" : 1, "username" : "ChavinKing", "city" : "beijing" }

2、查看分片集群狀態:

MongoDB Enterprise mongos> sh.status()
--- Sharding Status ---
   sharding version: {
       "_id" : 1,
       "minCompatibleVersion" : 5,
       "currentVersion" : 6,
       "clusterId" : ObjectId("5a37e16fd7903ab63ab90ebf")
   }
   shards:
         {  "_id" : "rs0",  "host" : "rs0/db01:40000,db02:40000",  "state" : 1 }
         {  "_id" : "rs1",  "host" : "rs1/db01:40001,db02:40001",  "state" : 1 }
   active mongoses:
         "3.6.0" : 1
   autosplit:
         Currently enabled: yes
   balancer:
         Currently enabled:  yes
         Currently running:  no
         Failed balancer rounds in last 5 attempts:  0
         Migration Results for the last 24 hours:
                 No recent migrations
   databases:
         {  "_id" : "chavin",  "primary" : "rs1",  "partitioned" : false } //數據庫chavin目前不支持分片("partitioned" : false),數據庫文件存儲在rs1片上("primary" : "rs1")
         {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
                 config.system.sessions
                         shard key: { "_id" : 1 }
                         unique: false
                         balancing: true
                         chunks:
                                 rs0    1
                         { "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : rs0 Timestamp(1, 0)

3、MongoDB分片是針對集合的,要想使集合支持分片,首先需要使其數據庫支持分片,為數據庫chavin啟動分片:

MongoDB Enterprise mongos> sh.enableSharding("chavin")
{
     "ok" : 1,
     "$clusterTime" : {
         "clusterTime" : Timestamp(1513614275, 5),
         "signature" : {
             "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
             "keyId" : NumberLong(0)
         }
     },
     "operationTime" : Timestamp(1513614275, 5)
}

4、為分片字段建立索引,同時為集合指定片鍵:

MongoDB Enterprise mongos> db.users.ensureIndex({city:1}) //創建索引
{
     "raw" : {
         "rs1/db01:40001,db02:40001" : {
             "createdCollectionAutomatically" : false,
             "numIndexesBefore" : 1,
             "numIndexesAfter" : 2,
             "ok" : 1
         }
     },
     "ok" : 1,
     "$clusterTime" : {
         "clusterTime" : Timestamp(1513614344, 1),
         "signature" : {
             "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
             "keyId" : NumberLong(0)
         }
     },
     "operationTime" : Timestamp(1513614344, 1)
}
MongoDB Enterprise mongos> sh.shardCollection("chavin.users",{city:1}) //啟用集合分片,為其指定片鍵
{
     "collectionsharded" : "chavin.users",
     "collectionUUID" : UUID("a5de7086-115c-44a3-984e-3db8d945dbab"),
     "ok" : 1,
     "$clusterTime" : {
         "clusterTime" : Timestamp(1513614387, 13),
         "signature" : {
             "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
             "keyId" : NumberLong(0)
         }
     },
     "operationTime" : Timestamp(1513614387, 13)
}

5、再次查看分片集群狀態:

MongoDB Enterprise mongos> sh.status()
--- Sharding Status ---
   sharding version: {
       "_id" : 1,
       "minCompatibleVersion" : 5,
       "currentVersion" : 6,
       "clusterId" : ObjectId("5a37e16fd7903ab63ab90ebf")
   }
   shards:
         {  "_id" : "rs0",  "host" : "rs0/db01:40000,db02:40000",  "state" : 1 }
         {  "_id" : "rs1",  "host" : "rs1/db01:40001,db02:40001",  "state" : 1 }
   active mongoses:
         "3.6.0" : 1
   autosplit:
         Currently enabled: yes
   balancer:
         Currently enabled:  yes
         Currently running:  no
         Failed balancer rounds in last 5 attempts:  0
         Migration Results for the last 24 hours:
                 No recent migrations
   databases:
         {  "_id" : "chavin",  "primary" : "rs1",  "partitioned" : true } //此時chavin數據庫已經支持分片
                 chavin.users
                         shard key: { "city" : 1 }
                         unique: false
                         balancing: true
                         chunks:
                                 rs1    1
                         { "city" : { "$minKey" : 1 } } -->> { "city" : { "$maxKey" : 1 } } on : rs1 Timestamp(1, 0)  //目前存在一個片,存儲在rs1上
         {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
                 config.system.sessions
                         shard key: { "_id" : 1 }
                         unique: false
                         balancing: true
                         chunks:
                                 rs0    1
                         { "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : rs0 Timestamp(1, 0)
                
6、向集群插入測試數據:

MongoDB Enterprise mongos> for(var i=1;i<1000000;i++) db.users.insert({userid:i,username:"chavin"+i,city:"beijing"})           
MongoDB Enterprise mongos> for(var i=1;i<1000000;i++) db.users.insert({userid:i,username:"dbking"+i,city:"changsha"})

7、再次查看分片集群狀態:

MongoDB Enterprise mongos> sh.status()
--- Sharding Status ---
   sharding version: {
       "_id" : 1,
       "minCompatibleVersion" : 5,
       "currentVersion" : 6,
       "clusterId" : ObjectId("5a37e16fd7903ab63ab90ebf")
   }
   shards:
         {  "_id" : "rs0",  "host" : "rs0/db01:40000,db02:40000",  "state" : 1 }
         {  "_id" : "rs1",  "host" : "rs1/db01:40001,db02:40001",  "state" : 1 }
   active mongoses:
         "3.6.0" : 1
   autosplit:
         Currently enabled: yes
   balancer:
         Currently enabled:  yes
         Currently running:  no
         Failed balancer rounds in last 5 attempts:  0
         Migration Results for the last 24 hours:
                 1 : Success
   databases:
         {  "_id" : "chavin",  "primary" : "rs1",  "partitioned" : true }
                 chavin.users
                         shard key: { "city" : 1 }
                         unique: false
                         balancing: true
                         chunks:
                                 rs0    1
                                 rs1    2
                         { "city" : { "$minKey" : 1 } } -->> { "city" : "beijing" } on : rs0 Timestamp(2, 0) //分片1,存儲在rs0中,並且標注了范圍
                         { "city" : "beijing" } -->> { "city" : "guangdong" } on : rs1 Timestamp(2, 1) //分片2,存儲在rs1中,並且標注了范圍
                         { "city" : "guangdong" } -->> { "city" : { "$maxKey" : 1 } } on : rs1 Timestamp(1, 3) //分片3,存儲在rs1中,並且標注了范圍
         {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
                 config.system.sessions
                         shard key: { "_id" : 1 }
                         unique: false
                         balancing: true
                         chunks:
                                 rs0    1
                         { "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : rs0 Timestamp(1, 0)
        
8、更加詳細的分析需要從集合changelog入手分析:

db.changelog.find()可以查看到具體的動作信息,這里不再贅述。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM