天天看點

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()可以檢視到具體的動作資訊,這裡不再贅述。

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