MongoDB Sharding(二) -- 搭建分片集群


上一篇文章中,我們基本了解了分片的概念,本文將着手實踐,進行分片集群的搭建

 

首先我們再來了解一下分片集群的架構,分片集群由三部分構成:

  • mongos:查詢路由,在客戶端程序和分片之間提供接口。本次實驗部署2個mongos實例
  • config:配置服務器存儲集群的元數據,元數據反映分片集群的內所有數據和組件的狀態和組織方式,元數據包含每個分片上的塊列表以及定義塊的范圍。從3.4版本開始,已棄用鏡像服務器用作配置服務器(SCCC),config Server必須部署為副本集架構(CSRS)。本次實驗配置一個3節點的副本集作為配置服務器
  • shard:每個shard包含集合的一部分數據,從3.6版本開始,每個shard必須部署為副本集(replica set)架構。本次實驗部署3個分片存儲數據。

 

(一)主機信息

 

(二)配置服務器副本集搭建

配置服務器三個實例的基礎規划如下:

member0 192.168.10.80:27017
member1 192.168.10.80:27018
member2 192.168.10.80:27019

其參數規划如下:

 

 接下來,我們一步一步搭建config server的副本集。

STEP1:解壓mongodb安裝包到/mongo目錄

[root@mongosserver mongo]# pwd
/mongo
[root@mongosserver mongo]# ls
bin LICENSE-Community.txt MPL-2 README THIRD-PARTY-NOTICES THIRD-PARTY-NOTICES.gotools

 STEP2:根據上面參數規划,創建數據存放相關路徑

# 創建文件路徑
mkdir -p /replset/repset1/data
mkdir -p /replset/repset1/log
mkdir -p /replset/repset2/data
mkdir -p /replset/repset2/log
mkdir -p /replset/repset3/data
mkdir -p /replset/repset3/log

[root@mongosserver repset1]# tree /replset/
/replset/
├── repset1
│   ├── data
│   ├── log
│   └── mongodb.conf
├── repset2
│   ├── data
│   └── log
└── repset3
    ├── data
    └── log

 STEP3:為3個實例創建參數文件

實例1的參數文件  /replset/repset1/mongodb.conf  :

systemLog:
   destination: file
   logAppend: true
   path: /replset/repset1/log/mongodb.log

storage:
   dbPath: /replset/repset1/data
   journal:
     enabled: true

processManagement:
   fork: true  # fork and run in background
   pidFilePath: /replset/repset1/mongod.pid  # location of pidfile
   timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
   port: 27017
   bindIp: 0.0.0.0  

# shard
sharding:
  clusterRole: configsvr
  
# repliuca set
replication:
  replSetName: conf
View Code

 實例2的參數文件  /replset/repset2/mongodb.conf :

systemLog:
   destination: file
   logAppend: true
   path: /replset/repset2/log/mongodb.log

storage:
   dbPath: /replset/repset2/data
   journal:
     enabled: true

processManagement:
   fork: true  # fork and run in background
   pidFilePath: /replset/repset2/mongod.pid  # location of pidfile
   timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
   port: 27018
   bindIp: 0.0.0.0  

# shard
sharding:
  clusterRole: configsvr
  
# repliuca set
replication:
  replSetName: conf
 
View Code

 實例3的參數文件  /replset/repset3/mongodb.conf :

systemLog:
   destination: file
   logAppend: true
   path: /replset/repset3/log/mongodb.log

storage:
   dbPath: /replset/repset3/data
   journal:
     enabled: true

processManagement:
   fork: true  # fork and run in background
   pidFilePath: /replset/repset3/mongod.pid  # location of pidfile
   timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
   port: 27019
   bindIp: 0.0.0.0  

# shard
sharding:
  clusterRole: configsvr
  
# repliuca set
replication:
  replSetName: conf
View Code

 STEP4:啟動三個mongod實例

mongod -f /replset/repset1/mongodb.conf
mongod -f /replset/repset2/mongodb.conf
mongod -f /replset/repset3/mongodb.conf


# 查看是成功否啟動
[root@mongosserver mongo]# netstat -nltp |grep mongod
tcp 0 0 0.0.0.0:27019 0.0.0.0:* LISTEN 28009/mongod 
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 27928/mongod 
tcp 0 0 0.0.0.0:27018 0.0.0.0:* LISTEN 27970/mongod

 STEP5:進入任意一個實例,初始化配置服務器的副本集

rs.initiate(
  {
    _id: "conf",
    configsvr: true,
    members: [
      { _id : 0, host : "192.168.10.80:27017" },
      { _id : 1, host : "192.168.10.80:27018" },
      { _id : 2, host : "192.168.10.80:27019" }
    ]
  }
)

  STEP6:[可選] 調整節點優先級,以便於確定主節點

cfg = rs.conf()
cfg.members[0].priority = 3
cfg.members[1].priority = 2
cfg.members[2].priority = 1
rs.reconfig(cfg)

 對於members[n]的定義:n是members數組中的數組位置,數組以0開始,千萬不能將其理解為“members[n]._id”的_id值。

查看節點優先級:

conf:PRIMARY> rs.config()

 

(三)分片副本集搭建

分片1副本集成員:
member0 192.168.10.81:27017
member1 192.168.10.81:27018
member2 192.168.10.81:27019

分片2副本集成員:
member0 192.168.10.82:27017
member1 192.168.10.82:27018
member2 192.168.10.82:27019

分片3副本集成員:
member0 192.168.10.83:27017
member1 192.168.10.83:27018
member2 192.168.10.83:27019

 

其參數規划如下:

 這里一共有3個分片,每個分片都是3個節點的副本集,副本集的搭建過程與上面config server副本集搭建過程相似,這里不再重復贅述,唯一不同的是副本集的初始化。shard副本集的初始化與配置副本集初始化過程相比,少了 configsvr: true 的參數配置。

三個shard副本集的初始化:

# shard001
rs.initiate( { _id:
"shard001", members: [ { _id : 0, host : "192.168.10.81:27017" }, { _id : 1, host : "192.168.10.81:27018" }, { _id : 2, host : "192.168.10.81:27019" } ] } )
# shard002
rs.initiate( { _id:
"shard002", members: [ { _id : 0, host : "192.168.10.82:27017" }, { _id : 1, host : "192.168.10.82:27018" }, { _id : 2, host : "192.168.10.82:27019" } ] } ) # shard003 rs.initiate( { _id: "shard003", members: [ { _id : 0, host : "192.168.10.83:27017" }, { _id : 1, host : "192.168.10.83:27018" }, { _id : 2, host : "192.168.10.83:27019" } ] } )

 

(四)配置並啟動mongos

本次試驗在192.168.10.100服務器上啟動2個mongos進程,分別使用端口27000和28000。

STEP1:配置mongos實例的參數

端口27000參數配置,特別注意,需要先創建涉及到的路徑:

systemLog:
   destination: file
   logAppend: true
   path: /mongo/log/mongos-27000.log
   
processManagement:
   fork: true  # fork and run in background
   pidFilePath: /mongo/mongod-27000.pid  # location of pidfile
   timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
   port: 27000
   bindIp: 0.0.0.0  

sharding:
  configDB: conf/192.168.10.80:27017,192.168.10.80:27018,192.168.10.80:27019

 端口28000參數配置,特別注意,需要先創建涉及到的路徑:

systemLog:
   destination: file
   logAppend: true
   path: /mongo/log/mongos-28000.log
   
processManagement:
   fork: true  # fork and run in background
   pidFilePath: /mongo/mongod-28000.pid  # location of pidfile
   timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
   port: 28000
   bindIp: 0.0.0.0  

sharding:
  configDB: conf/192.168.10.80:27017,192.168.10.80:27018,192.168.10.80:27019

 STEP2:啟動mongos實例

# 啟動mongos實例
[root@mongosserver mongo]# mongos -f /mongo/mongos-27000.conf 
[root@mongosserver mongo]# mongos -f /mongo/mongos-28000.conf 

# 查看實例信息
[root@mongosserver mongo]# netstat -nltp|grep mongos
tcp 0 0 0.0.0.0:27000 0.0.0.0:* LISTEN 2209/mongos 
tcp 0 0 0.0.0.0:28000 0.0.0.0:* LISTEN 2241/mongos

 

(五)添加分片到集群配置服務器

STEP1:使用mongo連接到mongos

mongo --host 192.168.10.100 --port 27000 # 或者
mongo --host 192.168.10.100 --port 28000

 STEP2:添加分片到集群

sh.addShard( "shard001/192.168.10.81:27017,192.168.10.81:27018,192.168.10.81:27019")
sh.addShard( "shard002/192.168.10.82:27017,192.168.10.82:27018,192.168.10.82:27019")
sh.addShard( "shard003/192.168.10.83:27017,192.168.10.83:27018,192.168.10.83:27019")

 STEP3:查看分片信息

mongos> sh.status() --- Sharding Status --- 
  sharding version: {
      "_id" : 1,
      "minCompatibleVersion" : 5,
      "currentVersion" : 6,
      "clusterId" : ObjectId("5ffc0709b040c53d59c15c66")
  }
  shards:
        {  "_id" : "shard001",  "host" : "shard001/192.168.10.81:27017,192.168.10.81:27018,192.168.10.81:27019",  "state" : 1 }
        {  "_id" : "shard002",  "host" : "shard002/192.168.10.82:27017,192.168.10.82:27018,192.168.10.82:27019",  "state" : 1 }
        {  "_id" : "shard003",  "host" : "shard003/192.168.10.83:27017,192.168.10.83:27018,192.168.10.83:27019",  "state" : 1 }
  active mongoses:
        "4.2.10" : 2
  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 }

mongos> 

 

 

(六)啟用分片

(6.1)對數據庫啟用分片
分片是以集合為單位進行的,在對一個集合進行分片之前,需要先對其數據庫啟用分片,對數據庫啟用分片並不會重新分發數據,只是說明該數據庫上的集合可以進行分片操作。

sh.enableSharding("lijiamandb");

 

(6.2)對集合啟用分片

如果集合已經存在數據,必須手動創建在分片鍵上創建索引,然后再對集合進行分片,如果集合為空,MongoDB會在分片的時候自動在分片鍵上創建索引。
mongodb提供了2種策略來對集合進行分片:

  • 哈希(hash)分片,對單列使用hash索引作為分片鍵
sh.shardCollection("<database>.<collection>",{shard key field : "hashed"}) 
  • 范圍(range)分片,可以使用多個字段作為分片鍵,並將數據划分為由分片鍵確定的連續范圍
sh.shardCollection("<database>.<collection>",{<shard key field>:1,...} )

 

例子:對集合user進行hash分片

// 連接到mongos,進入lijiamandb數據庫,對新集合users插入10萬條數據
use lijiamandb

for (i=1;i<100000;i++){
  db.user.insert({
  "id" : i,
  "name" : "name"+i,
  "age" : Math.floor(Math.random()*120),
  "created" : new Date()
  });
}


// 使用mongostat可以看到,所有數據都寫入到了主節點(shard2),每個數據庫的主節點可能不同,可以使用sh.status()查看。
[root@mongosserver ~]# mongostat --port 27000 5 --discover
           host insert query update delete getmore command dirty used flushes mapped vsize   res faults qrw arw net_in net_out conn set repl                time
localhost:27000    352    *0     *0     *0       0   704|0                  0     0B  356M 32.0M      0 0|0 0|0   224k    140k   10      RTR Jan 15 10:52:32.046

               host insert query update delete getmore command dirty  used flushes mapped vsize   res faults qrw arw net_in net_out conn      set repl                time
192.168.10.81:27017     *0    *0     *0     *0       0     2|0  0.3%  0.8%       0        1.90G  133M    n/a 0|0 1|0   417b   9.67k   23 shard001  SEC Jan 15 10:52:32.061
192.168.10.81:27018     *0    *0     *0     *0       0     3|0  0.3%  0.8%       1        1.93G  132M    n/a 0|0 1|0  1.39k   11.0k   28 shard001  PRI Jan 15 10:52:32.067
192.168.10.81:27019     *0    *0     *0     *0       0     2|0  0.3%  0.8%       0        1.95G  148M    n/a 0|0 1|0   942b   10.2k   26 shard001  SEC Jan 15 10:52:32.070
192.168.10.82:27017    352    *0     *0     *0     407  1192|0  2.5% 11.7%       1        1.99G  180M    n/a 0|0 1|0  1.52m   1.15m   29 shard002  PRI Jan 15 10:52:32.075
192.168.10.82:27018   *352    *0     *0     *0     409   441|0  4.5%  8.9%       0        1.96G  163M    n/a 0|0 1|0   566k    650k   25 shard002  SEC Jan 15 10:52:32.085
192.168.10.82:27019   *352    *0     *0     *0       0     2|0  4.4%  9.7%       0        1.92G  168M    n/a 0|0 1|0   406b   9.51k   24 shard002  SEC Jan 15 10:52:32.093
192.168.10.83:27017     *0    *0     *0     *0       0     1|0  0.2%  0.6%       1        1.89G  130M    n/a 0|0 1|0   342b   9.17k   22 shard003  SEC Jan 15 10:52:32.099
192.168.10.83:27018     *0    *0     *0     *0       0     2|0  0.2%  0.6%       0        1.95G  139M    n/a 0|0 1|0   877b   9.92k   28 shard003  PRI Jan 15 10:52:32.107
192.168.10.83:27019     *0    *0     *0     *0       0     1|0  0.2%  0.6%       0        1.90G  133M    n/a 0|0 1|0   342b   9.17k   21 shard003  SEC Jan 15 10:52:32.113
    localhost:27000    365    *0     *0     *0       0   731|0                   0     0B  356M 32.0M      0 0|0 0|0   233k    145k   10           RTR Jan 15 10:52:37.047



// 使用分片鍵id創建hash分片,因為id上沒有hash索引,會報錯
sh.shardCollection("lijiamandb.user",{"id":"hashed"})
/* 1 */
{
    "ok" : 0.0,
    "errmsg" : "Please create an index that starts with the proposed shard key before sharding the collection",
    "code" : 72,
    "codeName" : "InvalidOptions",
    "operationTime" : Timestamp(1610679762, 4),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1610679762, 4),
        "signature" : {
            "hash" : { "$binary" : "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "$type" : "00" },
            "keyId" : NumberLong(0)
        }
    }
}

// 需要手動創建hash索引
db.user.ensureIndex()

// 查看索引
/* 1 */
[
    {
        "v" : 2,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "lijiamandb.user"
    },
    {
        "v" : 2,
        "key" : {
            "id" : "hashed"
        },
        "name" : "id_hashed",
        "ns" : "lijiamandb.user"
    }
]

# 最后再重新分片即可
sh.shardCollection("lijiamandb".user,{"id":"hashed"})

 

到這里,我們分片集群環境已經搭建完成,接下來我們將會學習分片鍵的選擇機制。

 

【完】

 

 

相關文檔合集:

1. MongoDB Sharding(一) -- 分片的概念
2. MongoDB Sharding(二) -- 搭建分片集群
3. MongoDB Sharding(三) -- zone
4. MongoDB Sharding(四) -- 分片集群的維護管理


免責聲明!

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



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