設(shè)計(jì)網(wǎng)站價(jià)格網(wǎng)站搜索排名查詢(xún)
mongodb介紹:
是最常用的nosql數(shù)據(jù)庫(kù),在數(shù)據(jù)庫(kù)排名中已經(jīng)上升到了前六。這篇文章介紹如何搭建高可用的mongodb(分片+副本)集群。
環(huán)境準(zhǔn)備
系統(tǒng)系統(tǒng) BC 21.10
三臺(tái)服務(wù)器:192.168.123.247/248/249
安裝包:
mongodb-linux-x86_64-rhel8-7.0.14.tgz
(截止2024年10月11日星期五次新版)
mongosh-2.3.1-linux-x64.tgz
服務(wù)器規(guī)劃
服務(wù)器247 | 服務(wù)器248 | 服務(wù)器249 |
---|---|---|
mongos | mongos | mongos |
config server | config server | config server |
shard1 | 主節(jié)點(diǎn) shard1 副節(jié)點(diǎn) | shard1 仲裁 |
shard2 | 仲裁 shard2 主節(jié)點(diǎn) | shard2 副節(jié)點(diǎn) |
shard3 | 副節(jié)點(diǎn) shard3 仲裁 | shard3 主節(jié)點(diǎn) |
端口規(guī)劃
主機(jī)名 | IP | 組件mongos | 組件config server | 分片shard |
---|---|---|---|---|
server1 | 192.168.123.247 | 端口:27000 | 端口:27017 | shard1:27001 |
server2 | 192.168.123.248 | 端口:27000 | 端口:27017 | shard2:27002 |
server3 | 192.168.123.249 | 端口:27000 | 端口:27017 | shard3:27003 |
時(shí)間同步
略
防火墻
略
集群部署
解壓mongodb
官網(wǎng)下載,根據(jù)操作系統(tǒng)選擇下載相應(yīng)的版本。
Mongodb-download
在三臺(tái)機(jī)器上執(zhí)行解壓
tar zxvf mongodb-linux-x86_64-rhel8-7.0.14.tgz -C /usr/local/mongodb
新建目錄
三臺(tái)機(jī)器建立config、mongos、shard1、shard2、shard3目錄,包括子目錄
mkdir -p /usr/local/mongodb/config/{conf,data,log,run}
mkdir -p /usr/local/mongodb/mongos/{conf,data,log,run}
mkdir -p /usr/local/mongodb/shard{1,2,3}/{conf,data,log}
目錄結(jié)構(gòu)概述
/usr/local/mongodb/config:
用途:存放配置服務(wù)器的相關(guān)文件。
子目錄:
conf:存放配置服務(wù)器的配置文件(如 mongod.conf)。
data:存放配置服務(wù)器的數(shù)據(jù)文件。
log:存放配置服務(wù)器的日志文件。
run: 存儲(chǔ)進(jìn)程PID文件
/usr/local/mongodb/mongos:
用途:存放路由進(jìn)程(Mongos)的相關(guān)文件。
子目錄:
conf:存放路由進(jìn)程的配置文件。
data:因?yàn)?Mongos 不持久化數(shù)據(jù)。(也可以不創(chuàng)建)
log:存放路由進(jìn)程的日志文件。
run: 存儲(chǔ)進(jìn)程PID文件
/usr/local/mongodb/shard{1,2,3}:
用途:存放分片服務(wù)器的相關(guān)文件。
子目錄:
conf:存放分片服務(wù)器的配置文件。
data:存放分片服務(wù)器的數(shù)據(jù)文件。
log:存放分片服務(wù)器的日志文件。
配置環(huán)境變量
vim /etc/profile
export MONGODB_HOME=/usr/local/mongodb
export PATH=$MONGODB_HOME/bin:$PATH
使立即生效
source /etc/profile
安裝依賴(lài)包,缺少就安裝
yum install libcurl openssl xz-libs
創(chuàng)建用戶(hù),指定組標(biāo)識(shí)符
groupadd mongo -g 777
useradd mongo -g 777 -M -s /sbin/nologin
id mongo
開(kāi)機(jī)啟動(dòng)
cat > /etc/systemd/system/mongodb.service << EOF
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target[Service]
Type=simple
ExecStart=/usr/local/mongodb/mongodb/bin/mongod --config /usr/local/mongodb/mongodb.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/mongodb/mongodb/bin/mongod --config /usr/local/mongodb/mongodb.conf --shutdown
PrivateTmp=true[Install]
WantedBy=multi-user.target
EOF
三臺(tái)config server配置:
cat > /usr/local/mongodb/config/conf/mongod.conf << EOF
# 日志設(shè)置
systemLog:destination: file # 日志寫(xiě)入文件path: /usr/local/mongodb/config/log/mongod.log # 日志文件路徑logAppend: true # 追加日志logRotate: rename # 日志輪轉(zhuǎn)方式,支持 rename 或 reopen# 網(wǎng)絡(luò)設(shè)置
net:port: 27017 # MongoDB 默認(rèn)端口bindIp: 0.0.0.0 # 允許從所有 IP 訪問(wèn),生產(chǎn)環(huán)境建議限制# 數(shù)據(jù)目錄
storage:dbPath: /usr/local/mongodb/config/data # 數(shù)據(jù)文件存放路徑wiredTiger:engineConfig:cacheSizeGB: 1 # 根據(jù)情況配置內(nèi)存# 進(jìn)程設(shè)置
processManagement:fork: true # 以后臺(tái)進(jìn)程方式運(yùn)行pidFilePath: /usr/local/mongodb/config/run/mongod.pid # PID 文件路徑#復(fù)制集名稱(chēng)
replication:replSetName: "myconfigset"#作為配置服務(wù)
sharding:clusterRole: configsvrEOF
三臺(tái)mongos配置
cat > /usr/local/mongodb/config/conf/mongod.conf << EOF# 日志設(shè)置
systemLog:destination: file # 日志寫(xiě)入文件path: /usr/local/mongodb/mongos/log/mongos.log # 日志文件路徑logAppend: true # 追加日志logRotate: rename # 日志輪轉(zhuǎn)方式,支持 rename 或 reopen# 網(wǎng)絡(luò)設(shè)置
net:port: 27000 # MongoDB 默認(rèn)端口bindIp: 0.0.0.0 # 允許從所有 IP 訪問(wèn),生產(chǎn)環(huán)境建議限制# 進(jìn)程設(shè)置
processManagement:fork: true # 以后臺(tái)進(jìn)程方式運(yùn)行pidFilePath: /usr/local/mongodb/mongos/run/mongos.pid # PID 文件路徑#網(wǎng)絡(luò)延遲閾值
replication:localPingThresholdMs: 15#關(guān)聯(lián)配置服務(wù)
sharding:configDB: myconfigset/server1:27017,server2:27017,server3:27017EOF
啟動(dòng)三臺(tái)config server (3臺(tái))
mongod --config /usr/local/mongodb/config/conf/mongod.conf
安裝mongosh
安裝好mongosh工具,方便初始化副本集使用,
# mongosh mongodb://server1:27017。成功登錄如下圖:默認(rèn)的提示符是’test>’
大內(nèi)存頁(yè) 關(guān)閉 hugepage
echo "never" > /sys/kernel/mm/transparent_hugepage/enabled
echo "never" > /sys/kernel/mm/transparent_hugepage/defrag
初始化配置副本集(3臺(tái)機(jī)器執(zhí)行相同操作)
啟動(dòng)config server后,開(kāi)始初始化副本集,登錄任意一臺(tái)開(kāi)始初始化配置副本集
#7.0.14版本登錄時(shí)在test>數(shù)據(jù)庫(kù)下,切換到admin數(shù)據(jù)庫(kù)
test> use admin
switched to db admin
admin>#執(zhí)行以下config變量,注意myconfigset,要和mongod.conf里配置一致。
config={_id:"myconfigset",members:[
{_id:0,host:"server1:27017"},
{_id:1,host:"server2:27017"},
{_id:2,host:"server3:27017",},
]}
執(zhí)行結(jié)果如下:
#再執(zhí)行初始化
rs.initiate(config)
rs.status()
3.9 配置分片副本集
每臺(tái)shard1,shard2,shard3配置
Shard1
cat > /usr/local/mongodb/shard1/conf/shard1.conf << EOF
# 日志設(shè)置
systemLog:destination: file # 日志寫(xiě)入文件path: /usr/local/mongodb/shard1/log/shard1.log # 日志文件路徑logAppend: true # 追加日志logRotate: rename # 日志輪轉(zhuǎn)方式,支持 rename 或 reopen# 網(wǎng)絡(luò)設(shè)置
net:port: 27001 # MongoDB shard1端口bindIp: 0.0.0.0 # 允許從所有 IP 訪問(wèn),生產(chǎn)環(huán)境建議限制# 數(shù)據(jù)目錄
storage:dbPath: /usr/local/mongodb/shard1/data # 數(shù)據(jù)文件存放路徑wiredTiger:engineConfig:cacheSizeGB: 1 # 根據(jù)情況配置內(nèi)存# 進(jìn)程設(shè)置
processManagement:fork: true # 以后臺(tái)進(jìn)程方式運(yùn)行pidFilePath: /usr/local/mongodb/shard1/data/shard1.pid # PID 文件路徑#復(fù)制集名稱(chēng)
replication:replSetName: "shard1"#慢查詢(xún)
operationProfiling:slowOpThresholdMs : 100mode: "slowOp"#作為分片服務(wù)
sharding:clusterRole: shardsvrEOF
Shard2
cat > /usr/local/mongodb/shard2/conf/shard2.conf << EOF# 日志設(shè)置
systemLog:destination: file # 日志寫(xiě)入文件path: /usr/local/mongodb/shard2/log/shard2.log # 日志文件路徑logAppend: true # 追加日志logRotate: rename # 日志輪轉(zhuǎn)方式,支持 rename 或 reopen# 網(wǎng)絡(luò)設(shè)置
net:port: 27002 # shard2端口bindIp: 0.0.0.0 # 允許從所有 IP 訪問(wèn),生產(chǎn)環(huán)境建議限制# 數(shù)據(jù)目錄
storage:dbPath: /usr/local/mongodb/shard2/data # 數(shù)據(jù)文件存放路徑wiredTiger:engineConfig:cacheSizeGB: 1 # 根據(jù)情況配置內(nèi)存# 進(jìn)程設(shè)置
processManagement:fork: true # 以后臺(tái)進(jìn)程方式運(yùn)行pidFilePath: /usr/local/mongodb/shard2/data/shard2.pid # PID 文件路徑#復(fù)制集名稱(chēng)
replication:replSetName: "shard2"#慢查詢(xún)
operationProfiling:slowOpThresholdMs : 100mode: "slowOp"#作為分片服務(wù)
sharding:clusterRole: shardsvrEOF
Shard3
cat > /usr/local/mongodb/shard3/conf/shard3.conf << EOF# 日志設(shè)置
systemLog:destination: file # 日志寫(xiě)入文件path: /usr/local/mongodb/shard3/log/shard3.log # 日志文件路徑logAppend: true # 追加日志logRotate: rename # 日志輪轉(zhuǎn)方式,支持 rename 或 reopen# 網(wǎng)絡(luò)設(shè)置
net:port: 27003 # MongoDB shard3 默認(rèn)端口bindIp: 0.0.0.0 # 允許從所有 IP 訪問(wèn),生產(chǎn)環(huán)境建議限制# 數(shù)據(jù)目錄
storage:dbPath: /usr/local/mongodb/shard3/data # 數(shù)據(jù)文件存放路徑wiredTiger:engineConfig:cacheSizeGB: 1 # 根據(jù)情況配置內(nèi)存# 進(jìn)程設(shè)置
processManagement:fork: true # 以后臺(tái)進(jìn)程方式運(yùn)行pidFilePath: /usr/local/mongodb/shard3/data/shard3.pid # PID 文件路徑#復(fù)制集名稱(chēng)
replication:replSetName: "shard3"#慢查詢(xún)
operationProfiling:slowOpThresholdMs : 100mode: "slowOp"#作為分片服務(wù)
sharding:clusterRole: shardsvrEOF
啟動(dòng)三臺(tái)247,248,249服務(wù)器的shard1 server
mongod --config /usr/local/mongodb/shard1/conf/shard1.conf
初始化shard1分片副本集
登錄任意一臺(tái),注意端口號(hào)
mongosh mongodb://server1:27001
初始化三臺(tái)的shard1
#7.0.14版本登錄時(shí)在test>數(shù)據(jù)庫(kù)下,切換到admin數(shù)據(jù)庫(kù)
test> use admin
switched to db admin
admin>
config={_id:“shard1”,members:[
{_id:0,host:“server1:27001”},
{_id:1,host:“server2:27001”},
{_id:2,host:“server3:27001”,arbiterOnly: true},
]}
#再執(zhí)行初始化
rs.initiate(config)
rs.status()
啟動(dòng)三臺(tái)247,248,249服務(wù)器的shard2 server
mongod --config /usr/local/mongodb/shard2/conf/shard2.conf
登錄任意一個(gè)節(jié)點(diǎn),注意端口號(hào),注意安裝了mongosh
mongosh mongodb://server2:27002
初始化三臺(tái)的shard2
#7.0.14版本登錄時(shí)在test>數(shù)據(jù)庫(kù)下,切換到admin數(shù)據(jù)庫(kù)
test> use admin
switched to db admin
admin>config={_id:"shard2",members:[
{_id:0,host:"server1:27002",arbiterOnly: true},
{_id:1,host:"server2:27002"},
{_id:2,host:"server3:27002"},
]}#再執(zhí)行初始化
rs.initiate(config)
rs.status()
3.12.1 初始化三臺(tái)的shard3
#7.0.14版本登錄時(shí)在test>數(shù)據(jù)庫(kù)下,切換到admin數(shù)據(jù)庫(kù)
test> use admin;
switched to db admin
admin>config={_id:"shard3",members:[
{_id:0,host:"server1:27003"},
{_id:1,host:"server2:27003",arbiterOnly: true},
{_id:2,host:"server3:27003"},
]}#再執(zhí)行初始化
rs.initiate(config)
rs.status()
啟動(dòng)mongos路由并分片
啟動(dòng)三臺(tái)247,248,249服務(wù)器的mongos
mongos --config /usr/local/mongodb/mongos/conf/mongos.conf
登錄任意路由節(jié)點(diǎn),注意端口號(hào),注意安裝了mongosh
mongosh mongodb://server1:27000
添加分片
#7.0.14版本登錄時(shí)在test>數(shù)據(jù)庫(kù)下,切換到admin數(shù)據(jù)庫(kù)
test> use admin
switched to db admin
admin>db.adminCommand({ "setDefaultRWConcern" : 1, "defaultWriteConcern" : { "w" : 1 }})
admin>##添加分片
sh.addShard("shard1/server1:27001,server2:27001,server3:27001")
sh.addShard("shard2/server1:27002,server2:27002,server3:27002")
sh.addShard("shard3/server1:27003,server2:27003,server3:27003")
#
Admin> sh.status();
驗(yàn)證分片機(jī)制
指定分片生效:
db.runCommand({enablesharding:"testdb"})
哈希分片
db.runCommand({shardcollection:"testdb.table1",key:{id:"hashed"}})
插入1萬(wàn)個(gè)數(shù)據(jù),測(cè)試
for (var i = 1; i <= 10000; i++){
db.table1.insertOne({id:i,"test1":"testval1"});
Shard1
Shard2
Shard3
匯總內(nèi)容
驗(yàn)證已完成,可以看到數(shù)據(jù)分到3個(gè)分片,各自分片數(shù)量為:
shard1 “count” : 3366,
shard2 “count” : 3284,
shard3 “count” : 3350。
已經(jīng)成功!