MongoDB分片技術原理和高可用集群配置方案


一、Sharding分片技術

  1、分片概述

  當數據量比較大的時候,我們需要把數分片運行在不同的機器中,以降低CPU、內存和Io的壓力,Sharding就是數據庫分片技術。

  MongoDB分片技術類似MySQL的水平切分和垂直切分,數據庫主要由倆種方式做Sharding:垂直擴展和橫向切分。

  垂直擴展的方式就是進行集群擴展,添加更多的CPU,內存,磁盤空間等。

  橫向切分則是通過數據分片的方式,通過集群統一提供服務:

 

 

 二、MongoDB分片架構原理

  (1)MongoDB的Sharding架構

  

 其中,Router負責接受訪問,然后去config服務器中查詢元數據,將數據存儲信息返回給Router,Router服務器根據元數據的存儲信息在分片服務器上讀取或者寫入數據。

 (2)MongoDB分片架構中的角色

 a、數據分片(Shards)

 用來保存數據,保證數據的高可用性和一致性。可以是一個單獨的mongod實例,也可以是一個副本集。在生產環境下Shard一般是一個Replica Set,以防止該數據片的單點故障。所有Shard中有一個PrimaryShard,里面包含未進行划分的數據集合:

 

 

 b、查詢路由(Query Routers)

   路由就是mongos的實例,客戶端直接連接mongos,由mongos把讀寫請求路由到指定的Shard上去。

  一個Sharding集群,可以有一個mongos,也可以有多個mongos以減輕客戶端請求的壓力。

c、配置服務器(Config servers)

 保存集群的元數據(metadata),包含各個Shard的路由規則。

Sharding分片技術(混合模式)高可用方案的架構圖如下:

 

 

    1)Sharding分片技術(混合模式)高可用架構下MongoDB數據寫入流程

 

 

  第一步:客戶端訪問路由服務器(Mongos),路由服務器接收到請求。

  第二步:路由服務器將客戶端訪問配置服務器,配置服務器(Config)根據根據請求信息將數據信息寫入到元數據中。

  第三步:配置服務器將根據自身記錄的元數據信息,指定數據存儲位置,並將元數據信息(數據存儲位置)返回給路由服務器。

  第四步:路由服務器根據Config服務器返回的配置信息,將數據存儲到mongod中(數據存儲服務器)。存儲服務器對數據進行備份和分片存儲。

 2)高可用集群下從MongoDB讀取數據流程

 

 

   第一步:Mongos接受客戶端請求,根據並判斷請求是否合理(數據庫地址是否正確,連接數是否超過最大)

  第二步:路由服務器與客戶端建立連接,並根據請求信息,去Config服務器中查詢元數據信息。

 第三步:config服務器將數信息返回給路由服務器。

 第四步: config服務器根據元數據信息(數據存儲位置),去MongoD服務器中,查詢數據。

 第五步:數據服務器將數據查詢結果返回給路由服務器。

三、Sharding分片高可用方案搭建過程

1、MongoDB機器信息

192.168.86.131 192.168.86.132 192.168.86.133
mongos mongos mongos
config server config server config server
shard server1 主節點 shard server1 副節點 shard server1 仲裁
shard server2 仲裁 shard server2 主節點 shard server2 副節點
shard server3 副節點 shard server3 仲裁 shard server3 主節點

 2、端口分配

mongos:20000
config:21000
shard1:27001
shard2:27002
shard3:27003

3、下載並且安裝

wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-amazon-4.0.9.tgz
tar -xzvf mongodb-linux-x86_64-amazon-4.0.9.tgz  -C /usr/local/

4、建立軟連接並配置path信息

cd /usr/local/
ln -s  /usr/local/mongodb-linux-x86_64-amazon-3.6.2 /usr/local/mongodb

 進入到/etc/profile中配置MongoDB的全局變量信息

vi /etc/profile
# MongoDB 環境變量內容
export MONGODB_HOME=/usr/local/mongodb
export PATH=$MONGODB_HOME/bin:$PATH

 生效環境變量

 source  /etc/profile

5、為服務器建立日志和數據目錄

分別在每台機器建立conf、mongos、config、shard1、shard2、shard3六個目錄,因為mongos不存儲數據,只需要建立日志文件目錄即可。

mkdir -p /usr/local/mongodb/conf \
mkdir -p /usr/local/mongodb/mongos/log \
mkdir -p /usr/local/mongodb/config/data \
mkdir -p /usr/local/mongodb/config/log \
mkdir -p /usr/local/mongodb/shard1/data \
mkdir -p /usr/local/mongodb/shard1/log \
mkdir -p /usr/local/mongodb/shard2/data \
mkdir -p /usr/local/mongodb/shard2/log \
mkdir -p /usr/local/mongodb/shard3/data \

6、config server配置服務器

mongodb3.4版本以后要求配置服務器也創建副本集,不然集群搭建不成功。
(三台機器)添加配置文件

vi /usr/local/mongodb/conf/config.conf

## 配置文件內容
pidfilepath = /usr/local/mongodb/config/log/configsrv.pid
dbpath = /usr/local/mongodb/config/data
logpath = /usr/local/mongodb/config/log/congigsrv.log
logappend = true
 
bind_ip = 0.0.0.0
port = 21000
fork = true
 
#declare this is a config db of a cluster;
configsvr = true

#副本集名稱
replSet = configs
#設置最大連接數
maxConns = 20000

啟動三台服務器的config server

mongod -f /usr/local/mongodb/conf/config.conf

登錄任意一台配置服務器,初始化配置副本集
連接 MongoDB

mongo --port 21000

配置config變量

config = {
    _id : "configs",
    members : [
    {_id : 0, host : "192.168.86.131:21000" },
    {_id : 1, host : "192.168.86.132:21000" },
    {_id : 2, host : "192.168.86.133:21000" }
    ]
}

  初始化副本集

rs.initiate(config) 
 
  其中,"_id" : "configs"應與配置文件中配置的 replicaction.replSetName 一致,"members" 中的 "host" 為三個節點的 ip 和 port
響應內容如下
> config = {
... _id : "configs",
... members : [
... {_id : 0, host : "192.168.86.131:21000" },
... {_id : 1, host : "192.168.86.132:21000" },
... {_id : 2, host : "192.168.86.133:21000" }
... ]
... }
{
    "_id" : "configs",
    "members" : [
        {
            "_id" : 0,
            "host" : "192.168.86.131:21000"
        },
        {
            "_id" : 1,
            "host" : "192.168.86.132:21000"
        },
        {
            "_id" : 2,
            "host" : "192.168.86.133:21000"
        }
    ]
}
> rs.initiate(config);
{
    "ok" : 1,
    "operationTime" : Timestamp(1517369899, 1),
    "$gleStats" : {
        "lastOpTime" : Timestamp(1517369899, 1),
        "electionId" : ObjectId("000000000000000000000000")
    },
    "$clusterTime" : {
        "clusterTime" : Timestamp(1517369899, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}
configs:SECONDARY>

此時會發現終端上的輸出已經有了變化。

//從單個一個
>
//變成了
configs:SECONDARY>

 查詢狀態

configs:SECONDARY> rs.status()

7、配置分片服務器

  7.1設置第一個分片服務器

(三台機器)設置第一個分片副本集
配置文件

vi /usr/local/mongodb/conf/shard1.conf

#配置文件內容
#——————————————–
pidfilepath = /usr/local/mongodb/shard1/log/shard1.pid
dbpath = /usr/local/mongodb/shard1/data
logpath = /usr/local/mongodb/shard1/log/shard1.log
logappend = true

bind_ip = 0.0.0.0
port = 27001
fork = true
 
#副本集名稱
replSet = shard1
 
#declare this is a shard db of a cluster;
shardsvr = true
 
#設置最大連接數
maxConns = 20000

 啟動三台服務器的shard1 server 

mongod -f /usr/local/mongodb/conf/shard1.conf

登陸任意一台服務器,初始化副本集(除了192.168.86.133),因為仲裁服務器沒有訪問權限,負責會初始化失敗。
連接 MongoDB

 mongo --port 27001

使用admin數據庫

 use admin

定義副本集配置

config = {
    _id : "shard1",
     members : [
         {_id : 0, host : "192.168.86.131:27001" },
         {_id : 1, host : "192.168.86.132:27001" },
         {_id : 2, host : "192.168.86.133:27001" , arbiterOnly: true }
     ]
 }

 初始化副本集配置

rs.initiate(config)

響應內容如下
> use admin
switched to db admin
> config = {
...     _id : "shard1",
...      members : [
...          {_id : 0, host : "192.168.86.131:27001" },
...          {_id : 1, host : "192.168.86.132:27001" },
...          {_id : 2, host : "192.168.86.133:27001" , arbiterOnly: true }
...      ]
...  }
{
    "_id" : "shard1",
    "members" : [
        {
            "_id" : 0,
            "host" : "192.168.86.131:27001"
        },
        {
            "_id" : 1,
            "host" : "192.168.86.132:27001"
        },
        {
            "_id" : 2,
            "host" : "192.168.86.133:27001",
            "arbiterOnly" : true
        }
    ]
}
> rs.initiate(config)
{ "ok" : 1 }

 此時會發現終端上的輸出已經有了變化。

//從單個一個
>
//變成了
shard1:SECONDARY>

查詢狀態

shard1:SECONDARY> rs.status()

7.2設置第二和第三個分片服務器 

第二、第三個服務器配置過程同上,只需將配置文件中的服務器名稱改為shard2、shard3,將端口改為27002,27003即可,仲裁服務器分別選擇第二個和第三個。

8、配置路由服務器 mongos

 8.1 配置並初始化

(三台機器)先啟動配置服務器和分片服務器,后啟動路由實例啟動路由實例:

vi /usr/local/mongodb/conf/mongos.conf

#內容
pidfilepath = /usr/local/mongodb/mongos/log/mongos.pid
logpath = /usr/local/mongodb/mongos/log/mongos.log
logappend = true

bind_ip = 0.0.0.0
port = 20000
fork = true

#監聽的配置服務器,只能有1個或者3個 configs為配置服務器的副本集名字
configdb = configs/192.168.86.131:21000,192.168.86.132:21000,192.168.86.133:21000
 
#設置最大連接數
maxConns = 20000

 啟動三台服務器的mongos server

mongos -f /usr/local/mongodb/conf/mongos.conf

8.2 串聯路由服務器

目前搭建了mongodb配置服務器、路由服務器,各個分片服務器,不過應用程序連接到mongos路由服務器並不能使用分片機制,還需要在程序里設置分片配置,讓分片生效。
登陸任意一台mongos

mongo --port 20000

使用admin數據庫
use admin

串聯路由服務器與分配副本集

sh.addShard("shard1/192.168.86.131:27001,192.168.86.132:27001,192.168.86.123:27001");
sh.addShard("shard2/192.168.86.131:27002,192.168.86.132:27002,192.168.86.133:27002");
sh.addShard("shard3/192.168.86.131:27003,192.168.86.132:27003,192.168.86.133:27003");

查看集群狀態

sh.status()

響應內容如下

mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
    "_id" : 1,
    "minCompatibleVersion" : 5,
    "currentVersion" : 6,
    "clusterId" : ObjectId("5a713a37d56e076f3eb47acf")
  }
  shards:
        {  "_id" : "shard1",  "host" : "shard1/192.168.86.131:27001,192.168.86.132:27001",  "state" : 1 }
        {  "_id" : "shard2",  "host" : "shard2/192.168.86.132:27002,192.168.86.133:27002",  "state" : 1 }
        {  "_id" : "shard3",  "host" : "shard3/192.168.86.131:27003,192.168.86.133:27003",  "state" : 1 }
  active mongoses:
        "4.0.9" : 3
  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>

四、啟用集合分片

  目前配置服務、路由服務、分片服務、副本集服務都已經串聯起來了,但我們的目的是希望插入數據,數據能夠自動分片。連接在mongos上,准備讓指定的數據庫、指定的集合分片生效。
登陸任意一台mongos

mongo --port 20000

使用admin數據庫

use admin

指定testdb分片生效,如下圖:

db.runCommand( { enablesharding :"testdb"});

或

mongos> sh.enablesharding("testdb")

指定數據庫里需要分片的集合和片鍵,哈希id分片(注意:分片的字段數據應該是變化的,不然分片不成功),如下圖: 

db.runCommand( { shardCollection : "testdb.table1",key : {"name": "hashed"} } );

或

mongos> sh.shardCollection("testdb.table1", {"name": "hashed"})

通過命令查看mongodb路由服務器上的shards集合會有數據展示,如下圖:

通過命令查看mongodb路由服務器上的chunks集合會有數據展示,如下圖

我們設置testdb的 table3 表需要分片,根據 id 或name自動分片到 shard1 ,shard2,shard3 上面去。要這樣設置是因為不是所有mongodb 的數據庫和表 都需要分片!
測試分片配置結果
切換到 testdb 數據庫
use testdb;
插入測試數據
for(i=1;i<=100000;i++){db.table3.insert({"id":i,"name":"penglei"})};

總條數 

db.table1.aggregate([{$group : {_id : "$name", totle : {$sum : 1}}}])

查看分片情況如下

 

 

  結論數據基本均勻

分組查看總數量是:100000


免責聲明!

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



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