人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動態(tài)

MongoDB 主分片(primary shard)相關總結(jié)

發(fā)布日期:2022-01-29 08:06 | 文章來源:源碼中國

01 主分片是什么?

分片集群中的每一個數(shù)據(jù)庫都有一個主分片,這個主分片上保存了當前數(shù)據(jù)庫中沒有被分片的集合的數(shù)據(jù),主分片(primary shard)和主節(jié)點(primary)之間沒有任何關聯(lián)。

主分片是由mongos選擇出來的,選擇的依據(jù)是每當創(chuàng)建新數(shù)據(jù)庫的時候,mongos會從集群中選擇包含數(shù)據(jù)最少的分片作為新數(shù)據(jù)庫的主分片。具體的選擇方式是:

選擇listDatabase命令返回的totalSize字段作為選擇的準則。如下:

mongos> db.adminCommand("listDatabases")
{
  "databases" : [
    {
      xxxx
    },
    {
      xxxx
    },
    {
      xxxx
    }
  ],
  "totalSize" : 2842624,
  "totalSizeMb" : 2,
  "ok" : 1,
  "operationTime" : Timestamp(1610982469, 1),
  "$clusterTime" : {
    "clusterTime" : Timestamp(1610982469, 1),
    "signature" : {
      "hash" : BinData(0,"knqdZrxpyGFdSi0gljxCQG4LJ9U="),
      "keyId" : NumberLong("6894922308364795934")
    }
  }
}

如果我們想手工的選擇某個數(shù)據(jù)庫的主分片,可以使用movePrimary命令,遷移主分片的過程會耗費一定的時間,在遷移完成之前,不應該對數(shù)據(jù)庫或者對應的集合進行訪問。遷移過程可能會影響整個集群的操作,所以這個命令一般不要主動去使用,如果必須使用,請務必考慮對網(wǎng)絡負載的影響。

如果你部署的一個分片集群是由一個副本集修改配置而來的,那么副本集上原來的那些數(shù)據(jù)庫的主分片將會繼續(xù)留在原來的副本集上,之后創(chuàng)建的數(shù)據(jù)庫才可以將主分片設置在其他分片上。

02 如何遷移主分片

下面我們演示如何使用movePrimary命令來遷移數(shù)據(jù)庫的主分片。

首先,我們來看,當前new數(shù)據(jù)庫的test集合的主分片在sharding_yeyz這個分片上。它就是new.test的主分片,如下:

mongos> sh.status()
--- Sharding Status --- 
 shards:
  { "_id" : "sharding_yeyz", "host" : "sharding_yeyz/127.0.0.1:27018,127.0.0.1:27019,127.0.0.1:27020", "state" : 1, "tags" : [ "1_1000" ] }
  { "_id" : "sharding_yeyz1", "host" : "sharding_yeyz1/127.0.0.1:27024,127.0.0.1:27025,127.0.0.1:27026", "state" : 1, "tags" : [ "1000_", "1000_2000" ] }
 
 databases:
   ...
  { "_id" : "new", "primary" : "sharding_yeyz", "partitioned" : true, "version" : { "uuid" : UUID("68c70c64-f732-4478-8851-06dad4b94d6b"), "lastMod" : 1 } }
    new.test
      shard key: { "number" : 1 }
      unique: false
      balancing: true
      chunks:
        sharding_yeyz 3
        sharding_yeyz1 1
      { "number" : { "$minKey" : 1 } } -->> { "number" : 1 } on : sharding_yeyz Timestamp(2, 1) 
      { "number" : 1 } -->> { "number" : 1000 } on : sharding_yeyz Timestamp(1, 2) 
      { "number" : 1000 } -->> { "number" : 2000 } on : sharding_yeyz1 Timestamp(2, 0) 
      { "number" : 2000 } -->> { "number" : { "$maxKey" : 1 } } on : sharding_yeyz Timestamp(1, 5) 
       tag: 1_1000 { "number" : 1 } -->> { "number" : 1000 }
       tag: 1000_2000 { "number" : 1000 } -->> { "number" : 2000 }

接下來我們使用movePrimary命令,使用之前,我們需要知道它的執(zhí)行過程:

1、首先在集群元數(shù)據(jù)中修改主分片信息

2、然后移動所有的沒有分片的集合到指定的主分片中

**它只能在mongos上執(zhí)行。

命令的用法是:

db.adminCommand( { movePrimary: <databaseName>, to: <newPrimaryShard> } )
舉例:
db.adminCommand( { movePrimary : "new", to : "sharding_yeyz1" } )

我們執(zhí)行完:

db.adminCommand( { movePrimary : "new", to : "sharding_yeyz1" } )

之后,結(jié)果如下:

mongos> sh.status()
--- Sharding Status --- 
 shards:
  { "_id" : "sharding_yeyz", "host" : "sharding_yeyz/127.0.0.1:27018,127.0.0.1:27019,127.0.0.1:27020", "state" : 1, "tags" : [ "1_1000" ] }
  { "_id" : "sharding_yeyz1", "host" : "sharding_yeyz1/127.0.0.1:27024,127.0.0.1:27025,127.0.0.1:27026", "state" : 1, "tags" : [ "1000_", "1000_2000" ] }
 databases:
  { "_id" : "new", "primary" : "sharding_yeyz1", "partitioned" : true, "version" : { "uuid" : UUID("68c70c64-f732-4478-8851-06dad4b94d6b"), "lastMod" : 2 } }
    new.test
      shard key: { "number" : 1 }
      unique: false
      balancing: true
      chunks:
        sharding_yeyz 3
        sharding_yeyz1 1
      { "number" : { "$minKey" : 1 } } -->> { "number" : 1 } on : sharding_yeyz Timestamp(2, 1) 
      { "number" : 1 } -->> { "number" : 1000 } on : sharding_yeyz Timestamp(1, 2) 
      { "number" : 1000 } -->> { "number" : 2000 } on : sharding_yeyz1 Timestamp(2, 0) 
      { "number" : 2000 } -->> { "number" : { "$maxKey" : 1 } } on : sharding_yeyz Timestamp(1, 5) 
       tag: 1_1000 { "number" : 1 } -->> { "number" : 1000 }
       tag: 1000_2000 { "number" : 1000 } -->> { "number" : 2000 }

可以看到,主分片已經(jīng)遷移完成了。

以上就是MongoDB 主分片(primary shard)相關總結(jié)的詳細內(nèi)容,更多關于MongoDB 主分片(primary shard)的資料請關注本站其它相關文章!

香港服務器租用

版權聲明:本站文章來源標注為YINGSOO的內(nèi)容版權均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權,請聯(lián)系alex-e#qq.com處理。

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務

7x24全年不間斷在線

專屬顧問服務

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務熱線

關注
微信

關注官方微信
頂部