MongoDB搭建集群(分片+副本集)

相關(guān)概念

在搭建集群之前,需要首先了解幾個(gè)概念:路由,分片、副本集、配置服務(wù)器等。


相關(guān)概念.png

從圖中可以看到有四個(gè)組件:mongos、config server、shard、replica set。

mongos,數(shù)據(jù)庫集群請求的入口,所有的請求都通過mongos進(jìn)行協(xié)調(diào),不需要在應(yīng)用程序添加一個(gè)路由選擇器,mongos自己就是一個(gè)請求分發(fā)中心,它負(fù)責(zé)把對應(yīng)的數(shù)據(jù)請求請求轉(zhuǎn)發(fā)到對應(yīng)的shard服務(wù)器上。在生產(chǎn)環(huán)境通常有多mongos作為請求的入口,防止其中一個(gè)掛掉所有的mongodb請求都沒有辦法操作。

config server,顧名思義為配置服務(wù)器,存儲(chǔ)所有數(shù)據(jù)庫元信息(路由、分片)的配置。mongos本身沒有物理存儲(chǔ)分片服務(wù)器和數(shù)據(jù)路由信息,只是緩存在內(nèi)存里,配置服務(wù)器則實(shí)際存儲(chǔ)這些數(shù)據(jù)。mongos第一次啟動(dòng)或者關(guān)掉重啟就會(huì)從 config server 加載配置信息,以后如果配置服務(wù)器信息變化會(huì)通知到所有的 mongos 更新自己的狀態(tài),這樣 mongos 就能繼續(xù)準(zhǔn)確路由。在生產(chǎn)環(huán)境通常有多個(gè) config server 配置服務(wù)器,因?yàn)樗鎯?chǔ)了分片路由的元數(shù)據(jù),防止數(shù)據(jù)丟失!

shard,分片(sharding)是指將數(shù)據(jù)庫拆分,將其分散在不同的機(jī)器上的過程。將數(shù)據(jù)分散到不同的機(jī)器上,不需要功能強(qiáng)大的服務(wù)器就可以存儲(chǔ)更多的數(shù)據(jù)和處理更大的負(fù)載?;舅枷刖褪菍⒓锨谐尚K,這些塊分散到若干片里,每個(gè)片只負(fù)責(zé)總數(shù)據(jù)的一部分,最后通過一個(gè)均衡器來對各個(gè)分片進(jìn)行均衡(數(shù)據(jù)遷移)。

replica set,中文翻譯副本集,其實(shí)就是shard的備份,防止shard掛掉之后數(shù)據(jù)丟失。復(fù)制提供了數(shù)據(jù)的冗余備份,并在多個(gè)服務(wù)器上存儲(chǔ)數(shù)據(jù)副本,提高了數(shù)據(jù)的可用性, 并可以保證數(shù)據(jù)的安全性。

仲裁者(Arbiter),是復(fù)制集中的一個(gè)MongoDB實(shí)例,它并不保存數(shù)據(jù)。仲裁節(jié)點(diǎn)使用最小的資源并且不要求硬件設(shè)備,不能將Arbiter部署在同一個(gè)數(shù)據(jù)集節(jié)點(diǎn)中,可以部署在其他應(yīng)用服務(wù)器或者監(jiān)視服務(wù)器中,也可部署在單獨(dú)的虛擬機(jī)中。為了確保復(fù)制集中有奇數(shù)的投票成員(包括primary),需要添加仲裁節(jié)點(diǎn)做為投票,否則primary不能運(yùn)行時(shí)不會(huì)自動(dòng)切換primary。

簡單了解之后,我們可以這樣總結(jié)一下,應(yīng)用請求mongos來操作mongodb的增刪改查,配置服務(wù)器存儲(chǔ)數(shù)據(jù)庫元信息,并且和mongos做同步,數(shù)據(jù)最終存入在shard(分片)上,為了防止數(shù)據(jù)丟失同步在副本集中存儲(chǔ)了一份,仲裁在數(shù)據(jù)存儲(chǔ)到分片的時(shí)候決定存儲(chǔ)到哪個(gè)節(jié)點(diǎn)。

環(huán)境準(zhǔn)備

操作系統(tǒng): centos7
三臺服務(wù)器:192.168.0.128/129/130
安裝包:mongodb-linux-x86_64-rhel70-4.0.10.tgz

服務(wù)器規(guī)劃

服務(wù)器規(guī)劃.png

端口分配

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

集群搭建

一、安裝mongodb(三臺都安裝)

1、解壓安裝包改名

#解壓
tar -xzvf mongodb-linux-x86_64-rhel70-4.0.10.tgz -C /usr/local/
#改名
mv mongodb-linux-x86_64-rhel70-4.0.10 mongodb

2、分別在每臺機(jī)器建立conf、mongos、config、shard1、shard2、shard3六個(gè)目錄,因?yàn)閙ongos不存儲(chǔ)數(shù)據(jù),只需要建立日志文件目錄即可。

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
mkdir -p /usr/local/mongodb/shard3/log

3、配置環(huán)境變量

vim /etc/profile
# 內(nèi)容
export MONGODB_HOME=/usr/local/mongodb
export PATH=$MONGODB_HOME/bin:$PATH
# 使立即生效
source /etc/profile

二、config server副本集搭建

編輯配置文件

touch /usr/local/mongodb/conf/config.conf
vim /usr/local/mongodb/conf/config.conf

內(nèi)容

## 配置文件內(nèi)容
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=configRS

#設(shè)置最大連接數(shù)
maxConns=20000

配置文件拷貝到另外服務(wù)器

#將配置文件 scp 到另外兩臺機(jī)器上
scp /usr/local/mongodb/conf/config.conf root@192.168.10.129:/usr/local/mongodb/conf

啟動(dòng)三臺服務(wù)器的config server

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

登錄任意一臺配置服務(wù)器,初始化配置副本集

mongo --port 21000
#config變量
config = {
...    _id : "configRS",
...    configsvr: true,
...    protocolVersion : 1,
...     members : [
...         {_id : 0, host : "192.168.10.128:21000" },
...         {_id : 1, host : "192.168.10.129:21000" },
...         {_id : 2, host : "192.168.10.130:21000" }
...     ]
... }
#protocolVersion這個(gè)要必須有的,否則會(huì)報(bào)Support for replication protocol version 0 was removed in MongoDB 4.0

#初始化副本集
rs.initiate(config)
#重新配置,當(dāng)執(zhí)行過一次初始化時(shí),再修改配置使用這個(gè)
rs.reconfig(config, { force: true})

其中,”_id” : “configs”應(yīng)與配置文件中配置的 replicaction.replSetName 一致,”members” 中的 “host” 為三個(gè)節(jié)點(diǎn)的 ip 和 port

三、配置分片副本集(三臺服務(wù)器均執(zhí)行)

1、設(shè)置第一個(gè)分片副本集(三臺機(jī)器均設(shè)置第一個(gè)分片副本集shard1)

編輯配置文件

vim /usr/local/mongodb/conf/shard1.conf

內(nèi)容

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
 
#設(shè)置最大連接數(shù)
maxConns=20000

配置文件拷貝到另外服務(wù)器

#將配置文件 scp 到另外兩臺機(jī)器上
scp /usr/local/mongodb/conf/shard1.conf root@192.168.10.129:/usr/local/mongodb/conf

啟動(dòng)三臺服務(wù)器的shard1.server

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

登錄128服務(wù)器,初始化副本集

mongo --port 27001
#使用admin數(shù)據(jù)庫
use admin
#定義副本集配置,第三個(gè)節(jié)點(diǎn)的 arbiterOnly:true 代表其為仲裁節(jié)點(diǎn)
config = {
...    _id : "shard1",
...    protocolVersion : 1,
...     members : [
...         {_id : 0, host : "192.168.10.128:27001" },
...         {_id : 1, host : "192.168.10.129:27001" },
...         {_id : 2, host : "192.168.10.130:27001",arbiterOnly:true }
...     ]
... }
#protocolVersion這個(gè)要必須有的,否則會(huì)報(bào)Support for replication protocol version 0 was removed in MongoDB 4.0

#初始化副本集
rs.initiate(config)
#重新配置,當(dāng)執(zhí)行過一次初始化時(shí),再修改配置使用這個(gè)
rs.reconfig(config, { force: true})

其中,”_id” : “configs”應(yīng)與配置文件中配置的 replicaction.replSetName 一致,”members” 中的 “host” 為三個(gè)節(jié)點(diǎn)的 ip 和 port

2、設(shè)置第二個(gè)分片副本集(三臺機(jī)器均設(shè)置第一個(gè)分片副本集shard2)

配置文件如下

touch /usr/local/mongodb/conf/shard2.conf
vim /usr/local/mongodb/conf/shard2.conf

#配置文件內(nèi)容
#——————————————–
pidfilepath = /usr/local/mongodb/shard2/log/shard2.pid
dbpath = /usr/local/mongodb/shard2/data
logpath = /usr/local/mongodb/shard2/log/shard2.log
logappend = true

bind_ip = 0.0.0.0
port = 27002
fork = true
 
#副本集名稱
replSet=shard2
 
#declare this is a shard db of a cluster;
shardsvr = true
 
#設(shè)置最大連接數(shù)
maxConns=20000

配置文件拷貝到另外服務(wù)器

#將配置文件 scp 到另外兩臺機(jī)器上
scp /usr/local/mongodb/conf/shard2.conf root@192.168.10.129:/usr/local/mongodb/conf

啟動(dòng)三臺服務(wù)器的shard2.server

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

登錄129服務(wù)器,初始化副本集

mongo --port 27002
#使用admin數(shù)據(jù)庫
use admin
#定義副本集配置,第一個(gè)節(jié)點(diǎn)的 arbiterOnly:true 代表其為仲裁節(jié)點(diǎn)
config = {
...    _id : "shard2",
...    protocolVersion : 1,
...     members : [
...         {_id : 0, host : "192.168.10.128:27002" ,arbiterOnly:true},
...         {_id : 1, host : "192.168.10.129:27002" },
...         {_id : 2, host : "192.168.10.130:27002" }
...     ]
... }

#初始化副本集
rs.initiate(config)
#重新配置,當(dāng)執(zhí)行過一次初始化時(shí),再修改配置使用這個(gè)
rs.reconfig(config, { force: true})

3、設(shè)置第三個(gè)分片副本集(三臺機(jī)器均設(shè)置第一個(gè)分片副本集shard3)

配置文件如下

touch /usr/local/mongodb/conf/shard3.conf
vim /usr/local/mongodb/conf/shard3.conf

#配置文件內(nèi)容
#——————————————–
pidfilepath = /usr/local/mongodb/shard3/log/shard3.pid
dbpath = /usr/local/mongodb/shard3/data
logpath = /usr/local/mongodb/shard3/log/shard3.log
logappend = true

bind_ip = 0.0.0.0
port = 27003
fork = true
 
#副本集名稱
replSet=shard3
 
#declare this is a shard db of a cluster;
shardsvr = true
 
#設(shè)置最大連接數(shù)
maxConns=20000

配置文件拷貝到另外服務(wù)器

#將配置文件 scp 到另外兩臺機(jī)器上
scp /usr/local/mongodb/conf/shard3.conf root@192.168.10.129:/usr/local/mongodb/conf

啟動(dòng)三臺服務(wù)器的shard3.server

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

登錄130服務(wù)器,初始化副本集

mongo --port 27003
#使用admin數(shù)據(jù)庫
use admin
#定義副本集配置,第二個(gè)節(jié)點(diǎn)的 arbiterOnly:true 代表其為仲裁節(jié)點(diǎn)
config = {
...    _id : "shard3",
...    protocolVersion : 1,
...     members : [
...         {_id : 0, host : "192.168.10.128:27003"},
...         {_id : 1, host : "192.168.10.129:27003" ,arbiterOnly:true},
...         {_id : 2, host : "192.168.10.130:27003" }
...     ]
... }

#初始化副本集
rs.initiate(config)
#重新配置,當(dāng)執(zhí)行過一次初始化時(shí),再修改配置使用這個(gè)
rs.reconfig(config, { force: true})

四、配置路由服務(wù)器mongos

先啟動(dòng)配置服務(wù)器和分片服務(wù)器,后啟動(dòng)路由實(shí)例:(三臺機(jī)器)

vim /usr/local/mongodb/conf/mongos.conf

#內(nèi)容
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

#監(jiān)聽的配置服務(wù)器,只能有1個(gè)或者3個(gè) configs為配置服務(wù)器的副本集名字
configdb = configRS/192.168.10.128:21000,192.168.10.129:21000,192.168.10.130:21000
 
#設(shè)置最大連接數(shù)
maxConns=20000
#將配置文件 scp 到另外兩臺機(jī)器上
scp /usr/local/mongodb/conf/mongos.conf root@192.168.10.129:/usr/local/mongodb/conf

啟動(dòng)三臺服務(wù)器的mongos server

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

五、啟用分片

目前搭建了mongodb配置服務(wù)器、路由服務(wù)器,各個(gè)分片服務(wù)器,不過應(yīng)用程序連接到mongos路由服務(wù)器并不能使用分片機(jī)制,還需要在程序里設(shè)置分片配置,讓分片生效。
登陸任意一臺mongos

mongo --port 20000
#使用admin數(shù)據(jù)庫
use admin
#串聯(lián)路由服務(wù)器與分配副本集
mongos> sh.addShard("shard1/192.168.10.128:27001,192.168.10.129:27001,192.168.10.130:27001")
mongos> sh.addShard("shard2/192.168.10.128:27002,192.168.10.129:27002,192.168.10.130:27002")
mongos> sh.addShard("shard3/192.168.10.128:27003,192.168.10.129:27003,192.168.10.130:27003")
#查看集群狀態(tài)
sh.status()

六、測試

目前配置服務(wù)、路由服務(wù)、分片服務(wù)、副本集服務(wù)都已經(jīng)串聯(lián)起來了,但我們的目的是希望插入數(shù)據(jù),數(shù)據(jù)能夠自動(dòng)分片。連接在mongos上,準(zhǔn)備讓指定的數(shù)據(jù)庫、指定的集合分片生效。

#指定testdb分片生效
db.runCommand( { enablesharding :"testdb"});
#指定數(shù)據(jù)庫里需要分片的集合和片鍵
db.runCommand( { shardcollection : "testdb.table1",key : {id: 1} } )

我們設(shè)置testdb的 table1 表需要分片,根據(jù) id 自動(dòng)分片到 shard1 ,shard2,shard3 上面去。要這樣設(shè)置是因?yàn)椴皇撬衜ongodb 的數(shù)據(jù)庫和表 都需要分片!

測試分片配置結(jié)果

mongo  192.168.10.128:20000
#使用testdb
use testdb
#插入測試數(shù)據(jù)
for (var i = 1; i <= 100; i++){db.table1.save({id:i,"test1":"testval1"});}

#查看分片情況如下,部分無關(guān)信息省掉了
db.table1.stats()

{
        "sharded" : true,
        "ns" : "testdb.table1",
        "count" : 100000,
        "numExtents" : 13,
        "size" : 5600000,
        "storageSize" : 22372352,
        "totalIndexSize" : 6213760,
        "indexSizes" : {
                "_id_" : 3335808,
                "id_1" : 2877952
        },
        "avgObjSize" : 56,
        "nindexes" : 2,
        "nchunks" : 3,
        "shards" : {
                "shard1" : {
                        "ns" : "testdb.table1",
                        "count" : 42183,
                        "size" : 0,
                        ...
                        "ok" : 1
                },
                "shard2" : {
                        "ns" : "testdb.table1",
                        "count" : 38937,
                        "size" : 2180472,
                        ...
                        "ok" : 1
                },
                "shard3" : {
                        "ns" : "testdb.table1",
                        "count" :18880,
                        "size" : 3419528,
                        ...
                        "ok" : 1
                }
        },
        "ok" : 1
}

可以看到數(shù)據(jù)分到3個(gè)分片,各自分片數(shù)量為: shard1 “count” : 42183,shard2 “count” : 38937,shard3 “count” : 18880。已經(jīng)成功了!

七、后期使用

mongodb的啟動(dòng)順序是,先啟動(dòng)配置服務(wù)器,在啟動(dòng)分片,最后啟動(dòng)mongos.

mongod -f /usr/local/mongodb/conf/config.conf
mongod -f /usr/local/mongodb/conf/shard1.conf
mongod -f /usr/local/mongodb/conf/shard2.conf
mongod -f /usr/local/mongodb/conf/shard3.conf
mongos -f /usr/local/mongodb/conf/mongos.conf  (看清楚是mongos -f ....)

關(guān)閉時(shí),直接killall殺掉所有進(jìn)程

killall mongod
killall mongos

ps:1、在安裝了最簡版的系統(tǒng)時(shí),killall 命令不能使用 需要執(zhí)行 yum install psmisc 即可。
2、關(guān)閉防火墻:systemctl stop firewalld
3、查看防火墻狀態(tài):firewall-cmd --state
4、禁止防火墻開機(jī)啟動(dòng):systemctl disable firewalld.server
5、列表端口號:netstat -ntlp

參考博文:https://blog.csdn.net/weixin_39778417/article/details/88107677

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容