在搭建叢集之前,需要首先了解幾個概念:路由,分片、副本集、配置伺服器等。
相關概念
mongodb叢集架構圖:
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAzNfRHLGZkRGZkRfJ3bs92YsYTMfVmepNHL1EFROh3Y61kMNpHW4Z0MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL1IjMyEzMwgTMyIDNwEjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
從圖中可以看到有四個元件:mongos、config server、shard、replica set。
- mongos,資料庫叢集請求的入口,所有的請求都通過mongos進行協調,不需要在應用程式添加一個路由選擇器,mongos自己就是一個請求分發中心,它負責把對應的資料請求請求轉發到對應的shard伺服器上。在生産環境通常有多mongos作為請求的入口,防止其中一個挂掉所有的mongodb請求都沒有辦法操作。
- config server,顧名思義為配置伺服器,存儲所有資料庫元資訊(路由、分片)的配置。mongos本身沒有實體存儲分片伺服器和資料路由資訊,隻是緩存在記憶體裡,配置伺服器則實際存儲這些資料。mongos第一次啟動或者關掉重新開機就會從 config server 加載配置資訊,以後如果配置伺服器資訊變化會通知到所有的 mongos 更新自己的狀态,這樣 mongos 就能繼續準确路由。在生産環境通常有多個 config server 配置伺服器,因為它存儲了分片路由的中繼資料,防止資料丢失!
- shard,分片(sharding)是指将資料庫拆分,将其分散在不同的機器上的過程。将資料分散到不同的機器上,不需要功能強大的伺服器就可以存儲更多的資料和處理更大的負載。基本思想就是将集合切成小塊,這些塊分散到若幹片裡,每個片隻負責總資料的一部分,最後通過一個均衡器來對各個分片進行均衡(資料遷移)。
- replica set,中文翻譯副本集,其實就是shard的備份,防止shard挂掉之後資料丢失。複制提供了資料的備援備份,并在多個伺服器上存儲資料副本,提高了資料的可用性, 并可以保證資料的安全性。
- 仲裁者(Arbiter),是複制集中的一個MongoDB執行個體,它并不儲存資料。仲裁節點使用最小的資源并且不要求硬體裝置,不能将Arbiter部署在同一個資料集節點中,可以部署在其他應用伺服器或者監視伺服器中,也可部署在單獨的虛拟機中。為了確定複制集中有奇數的投票成員(包括primary),需要添加仲裁節點做為投票,否則primary不能運作時不會自動切換primary。
簡單了解之後,我們可以這樣總結一下,應用請求mongos來操作mongodb的增删改查,配置伺服器存儲資料庫元資訊,并且和mongos做同步,資料最終存入在shard(分片)上,為了防止資料丢失同步在副本集中存儲了一份,仲裁在資料存儲到分片的時候決定存儲到哪個節點。
環境準備
系統系統 centos 7.0
- 三台伺服器:node_1(192.168.12.121)、node_2(192.168.12.121)、node_3(192.168.12.121)
伺服器node1(192.168.12.121) | 伺服器node2(192.168.12.122) | 伺服器node3(192.168.12.123 |
---|---|---|
mongos | mongos | mongos |
config server | config server | config server |
shard server1 主節點 | shard server1 副節點 | shard server1 仲裁 |
shard server2 仲裁 | shard server2 主節點 | shard server1 副節點 |
shard server3 副節點 | shard server3 仲裁 | shard server3 主節點 |
端口配置設定:
mongos:20000 config:21000 shard1:27001 shard2:27002 shard3:27003
分别在每台機器建立conf、mongos、config、shard1、shard2、shard3六個目錄,因為mongos不存儲資料,隻需要建立日志檔案目錄即可。
mkdir -p /export/mongodb/conf
mkdir -p /export/mongodb/server
mkdir -p /export/mongodb/mongos/log
mkdir -p /export/mongodb/config/data
mkdir -p /export/mongodb/config/log
mkdir -p /export/mongodb/shard1/data
mkdir -p /export/mongodb/shard1/log
mkdir -p /export/mongodb/shard2/data
mkdir -p /export/mongodb/shard2/log
mkdir -p /export/mongodb/shard3/data
mkdir -p /export/mongodb/shard3/log
#上傳安裝包到伺服器
tar -zxvf mongodb-linux-x86_64-rhel62-4.0.2.tgz
#複制到另外兩台機
scp mongodb-linux-x86_64-rhel62-4.0.2.tgz [email protected]:/export/mongodb/server/
環境變量
為了後續友善操作,配置mongodb的環境變量,需要切到root使用者下面
vim /etc/profile
# 内容
export MONGODB_HOME=/export/mongodb/server/mongodb
export PATH=$MONGODB_HOME/bin:$PATH
# 使立即生效,在安裝使用者下(youknow)執行
source /etc/profile
config server配置伺服器
mongodb 以後要求配置伺服器也建立副本集,不然叢集搭建不成功。
添加配置檔案
vim /export/mongodb/conf/config.conf
## content
systemLog:
destination: file
logAppend: true
path: /export/mongodb/config/log/config.log
# Where and how to store data.
storage:
dbPath: /export/mongodb/config/data
journal:
enabled: true
# how the process runs
processManagement:
fork: true
pidFilePath: /export/mongodb/config/log/configsrv.pid
# network interfaces
net:
port: 21000
bindIp: 0.0.0.0
#operationProfiling:
replication:
replSetName: config
sharding:
clusterRole: configsvr
啟動三台伺服器的config server
mongod -f /export/mongodb/conf/config.conf
登入任意一台配置伺服器,初始化配置副本集
連接配接
mongo 192.168.12.121:21000
#config變量
config = {_id : "config",members : [{_id : 0, host : "192.168.12.121:21000" },{_id : 1, host : "192.168.12.122:21000" },{_id : 2, host : "192.168.12.123:21000" }]}
#初始化副本集
rs.initiate(config)
其中,”_id” : “configs”應與配置檔案中配置的 replicaction.replSetName 一緻,”members” 中的 “host” 為三個節點的ip和port
這樣配置伺服器就配置好了
配置分片副本集(三台機器)
-
設定第一個分片副本集
配置檔案
vim /export/mongodb/conf/shard1.conf
#配置檔案内容
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /export/mongodb/shard1/log/shard1.log
# Where and how to store data.
storage:
dbPath: /export/mongodb/shard1/data
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 20
# how the process runs
processManagement:
fork: true
pidFilePath: /export/mongodb/shard1/log/shard1.pid
# network interfaces
net:
port: 27001
bindIp: 0.0.0.0
#operationProfiling:
replication:
replSetName: shard1
sharding:
clusterRole: shardsvr
複制到另外兩台機
修改對應IP
scp shard1.conf [email protected]:/export/mongodb/conf
啟動三台伺服器的shard1 server
mongod -f /export/mongodb/conf/shard1.conf
登陸任意一台伺服器,初始化副本集
mongo 192.168.12.121:27001
#使用admin資料庫
use admin
#定義副本集配置,第三個節點的 "arbiterOnly":true 代表其為仲裁節點。
config = { _id : "shard1",members : [{_id : 0, host : "192.168.12.121:27001" },{_id : 1, host : "192.168.12.122:27001" },{_id : 2, host : "192.168.12.123:27001" , arbiterOnly: true }]}
#初始化副本集配置
rs.initiate(config);
-
設定第二個分片副本集
配置檔案
vim /export/mongodb/conf/shard2.conf
#配置檔案内容
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /export/mongodb/shard2/log/shard2.log
# Where and how to store data.
storage:
dbPath: /export/mongodb/shard2/data
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 20
# how the process runs
processManagement:
fork: true
pidFilePath: /export/mongodb/shard2/log/shard2.pid
# network interfaces
net:
port: 27002
bindIp: 0.0.0.0
#operationProfiling:
replication:
replSetName: shard2
sharding:
clusterRole: shardsvr
複制到另外兩台機
修改對應IP
scp shard2.conf [email protected]:/export/mongodb/conf
啟動三台伺服器的shard1 server
mongod -f /export/mongodb/conf/shard2.conf
登陸任意一台伺服器,初始化副本集
mongo 192.168.12.121:27002
#使用admin資料庫
use admin
#定義副本集配置,第三個節點的 "arbiterOnly":true 代表其為仲裁節點。
config = {
... _id : "shard2",
... members : [
... {_id : 0, host : "192.168.12.121:27002" },
... {_id : 1, host : "192.168.12.122:27002", arbiterOnly: true },
... {_id : 2, host : "192.168.12.123:27002" }
... ]
... }
#初始化副本集配置
rs.initiate(config);
-
設定第三個分片副本集
配置檔案
vim /export/mongodb/conf/shard3.conf
#配置檔案内容
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /export/mongodb/shard3/log/shard3.log
# Where and how to store data.
storage:
dbPath: /export/mongodb/shard3/data
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 20
# how the process runs
processManagement:
fork: true
pidFilePath: /export/mongodb/shard3/log/shard3.pid
# network interfaces
net:
port: 27003
bindIp: 0.0.0.0
#operationProfiling:
replication:
replSetName: shard3
sharding:
clusterRole: shardsvr
複制到另外兩台機
修改對應IP
scp shard3.conf [email protected]:/export/mongodb/conf
啟動三台伺服器的shard1 server
mongod -f /export/mongodb/conf/shard3.conf
登陸任意一台伺服器,初始化副本集
mongo 192.168.12.122:27003
#使用admin資料庫
use admin
#定義副本集配置,第三個節點的 "arbiterOnly":true 代表其為仲裁節點。
config = {
... _id : "shard3",
... members : [
... {_id : 0, host : "192.168.12.121:27003" , arbiterOnly: true },
... {_id : 1, host : "192.168.12.122:27003" },
... {_id : 2, host : "192.168.12.123:27003" }
... ]
... }
#初始化副本集配置
rs.initiate(config);
配置路由伺服器 mongos
先啟動配置伺服器和分片伺服器,後啟動路由執行個體:(三台機器)
vim /export/mongodb/conf/mongos.conf
#内容
systemLog:
destination: file
logAppend: true
path: /export/mongodb/mongos/log/mongos.log
processManagement:
fork: true
# pidFilePath: /export/mongodb/mongos/log/mongos.pid
# network interfaces
net:
port: 20000
bindIp: 0.0.0.0
#監聽的配置伺服器,隻能有1個或者3個 configs為配置伺服器的副本集名字
sharding:
configDB: config/192.168.12.121:21000,192.168.12.122:21000,192.168.12.123:21000
複制到另外兩台機
修改對應IP
scp /export/mongodb/conf/mongos.conf [email protected]:/export/mongodb/conf
啟動三台伺服器的mongos server
mongos -f /export/mongodb/conf/mongos.conf
啟用分片
目前搭建了mongodb配置伺服器、路由伺服器,各個分片伺服器,不過應用程式連接配接到mongos路由伺服器并不能使用分片機制,還需要在程式裡設定分片配置,讓分片生效。
登陸任意一台mongos
mongo 192.168.12.121:20000
#使用admin資料庫
use admin
#串聯路由伺服器與配置設定副本集
sh.addShard("shard1/192.168.12.121:27001,192.168.12.122:27001,192.168.12.123:27001")
sh.addShard("shard2/192.168.12.121:27002,192.168.12.122:27002,192.168.12.123:27002")
sh.addShard("shard3/192.168.12.121:27003,192.168.12.122:27003,192.168.12.123:27003")
#檢視叢集狀态
sh.status()
測試
目前配置服務、路由服務、分片服務、副本集服務都已經串聯起來了,但我們的目的是希望插入資料,資料能夠自動分片。連接配接在mongos上,準備讓指定的資料庫、指定的集合分片生效。
# 指定testdb分片生效
db.runCommand( { enablesharding :"testdb"});
#指定資料庫裡需要分片的集合和片鍵
db.runCommand( { shardcollection : "testdb.table1",key : {id: 1} } )
mongos> use config
switched to db config
mongos> db.settings.save({"_id":"chunksize","value":1}) //設定塊大小為1M是友善實驗,不然就需要插入海量資料
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : "chunksize" })
#使用testdb
use testdb;
#插入測試資料
for (var i = 1; i <= 100000; i++)
db.table1.save({id:i,"test1":"testval1"});
#檢視分片情況如下,部分無關資訊省掉了
db.table1.stats();
後期運維
啟動關閉
mongodb的啟動順序是,先啟動配置伺服器,在啟動分片,最後啟動mongos.
mongod -f /export/mongodb/conf/config.conf
mongod -f /export/mongodb/conf/shard1.conf
mongod -f /export/mongodb/conf/shard2.conf
mongod -f /export/mongodb/conf/shard3.conf
mongod -f /export/mongodb/conf/mongos.conf
ps -aux | grep mongodb;
tail -n2200 shard2.log |sed -n ‘/2020-11-30 15:20:00/,/2020-11-30 16:20:00/p’