当前位置:网站首页>MongoDB第二話 -- MongoDB高可用集群實現
MongoDB第二話 -- MongoDB高可用集群實現
2022-07-01 14:43:00 【有頭發的程序猿!】
本文主要記錄MongoDB集群的搭建
壞境准備:liunx centos7,docker-compose,鏡像mongo:4.2.21。
一主一從一仲裁,在一臺機實現了
| 項目 | Value |
|---|---|
| 0.221:27017 | 節點1 |
| 0.221:27018 | 節點2 |
| 0.221:27019 | 仲裁節點 |
1. docker-compose.yaml
version: '3.7'
services:
mongo01:
image: mongo:4.2.21
container_name: mongo01
ports:
- "27017:27017"
environment:
- TZ=Asia/Shanghai
volumes:
- ./rs1:/data/db
#--replSet newset 定義副本集名稱 可在路徑後面使用?replicaSet=newset
command: mongod --dbpath /data/db --replSet newset --oplogSize 128
networks:
- my-net
mongo02:
image: mongo:4.2.21
container_name: mongo02
ports:
- "27018:27017"
environment:
- TZ=Asia/Shanghai
volumes:
- ./rs2:/data/db
command: mongod --dbpath /data/db --replSet newset --oplogSize 128
networks:
- my-net
myarbiter:
image: mongo:4.2.21
container_name: myarbiter
ports:
- "27019:27017"
environment:
- TZ=Asia/Shanghai
volumes:
- ./rs3:/data/db
command: mongod --dbpath /data/db --replSet newset --oplogSize 128
networks:
- my-net
mongo-console:
image: mongo-express
container_name: mongo-console
ports:
- "8081:8081"
environment:
#控制臺 需要集群生效後才能連接成功 只能連接master 可單獨抽離出來
- ME_CONFIG_MONGODB_SERVER=mongo01
# - ME_CONFIG_BASICAUTH_USERNAME=admin
# - ME_CONFIG_BASICAUTH_PASSWORD=admin
- TZ=Asia/Shanghai
networks:
- my-net
networks:
#新增的網絡 內部服務名調用
my-net:
external: true
2.啟動容器
[[email protected] mongo-cluster]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7588aac35621 mongo:4.2.21 "docker-entrypoint.s…" 15 minutes ago Up 8 minutes 0.0.0.0:27017->27017/tcp mongo01
5adca1bbfa3d mongo:4.2.21 "docker-entrypoint.s…" 15 minutes ago Up 15 minutes 0.0.0.0:27019->27017/tcp myarbiter
5384dac6d37a mongo:4.2.21 "docker-entrypoint.s…" 15 minutes ago Up 15 minutes 0.0.0.0:27018->27017/tcp mongo02
5df1b5d9af4f mongo-express "tini -- /docker-ent…" 15 minutes ago Up 8 minutes 0.0.0.0:8081->8081/tcp mongo-console
3.初始化集群
#進入任意一個容器內部 執行mongo命令
[[email protected] mongo-cluster]# docker exec -it mongo02 mongo
MongoDB shell version v4.2.21
#執行集群初始配置,如果需要程序連接,需要填宿主機IP
>rs.initiate({
_id:"newset",members:[{
_id:0,host:"192.168.0.221:27017"},{
_id:1,host:"192.168.0.221:27018"},{
_id:2,host:"192.168.0.221:27019",arbiterOnly:true}]})
{
"operationTime" : Timestamp(1656417751, 1),
"ok" : 0,
"errmsg" : "already initialized",
"code" : 23,
"codeName" : "AlreadyInitialized",
"$clusterTime" : {
"clusterTime" : Timestamp(1656417751, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
newset:SECONDARY>
_id:對應的replSet 名稱
host:如果需要程序連接,需要填宿主機IP
arbiterOnly:true 錶示該節點為仲裁節點
4.查看集群狀態
newset:SECONDARY> rs.status()
#找到members節點信息
"members" : [
{
"_id" : 0,
"name" : "192.168.0.221:27017",
"health" : 1,
"state" : 1,
#集群master
"stateStr" : "PRIMARY",
"uptime" : 202,
"optime" : {
"ts" : Timestamp(1656417761, 6),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2022-06-28T12:02:41Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1656417761, 1),
"electionDate" : ISODate("2022-06-28T12:02:41Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "192.168.0.221:27018",
"health" : 1,
"state" : 2,
#集群slave
"stateStr" : "SECONDARY",
"uptime" : 10,
"optime" : {
"ts" : Timestamp(1656417751, 1),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(1656417751, 1),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("2022-06-28T12:02:31Z"),
"optimeDurableDate" : ISODate("2022-06-28T12:02:31Z"),
"lastHeartbeat" : ISODate("2022-06-28T12:02:41.778Z"),
"lastHeartbeatRecv" : ISODate("2022-06-28T12:02:41.434Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.0.221:27019",
"health" : 1,
"state" : 7,
#集群仲裁
"stateStr" : "ARBITER",
"uptime" : 10,
"lastHeartbeat" : ISODate("2022-06-28T12:02:41.778Z"),
"lastHeartbeatRecv" : ISODate("2022-06-28T12:02:41.704Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1
}
],
5.模擬master節點掛掉
停掉master節點,進入slave節點容器中查看狀態
"members" : [
{
"_id" : 0,
"name" : "192.168.0.221:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2022-06-28T12:05:49.005Z"),
"lastHeartbeatRecv" : ISODate("2022-06-28T12:05:30.416Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "Error connecting to 192.168.0.221:27017 :: caused by :: Connection refused",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : -1
},
{
"_id" : 1,
"name" : "192.168.0.221:27018",
"health" : 1,
"state" : 1,
#原來的slave節點已經變更為master了
"stateStr" : "PRIMARY",
"uptime" : 390,
"optime" : {
"ts" : Timestamp(1656417940, 2),
"t" : NumberLong(3)
},
"optimeDate" : ISODate("2022-06-28T12:05:40Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1656417940, 1),
"electionDate" : ISODate("2022-06-28T12:05:40Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "192.168.0.221:27019",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 197,
"lastHeartbeat" : ISODate("2022-06-28T12:05:49.006Z"),
"lastHeartbeatRecv" : ISODate("2022-06-28T12:05:48.204Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1
}
],
可以看到原來的slave節點已經變成了PRIMARY主節點
6.mongo-express控制臺
- 連接需要注意的點
environment:
#不用帶端口 直接走容器名稱
- ME_CONFIG_MONGODB_SERVER=mongo01
- 集群未初始化或者集群master已變更,控制臺會報下面錯誤
(node:6) UnhandledPromiseRejectionWarning: MongoError: not master and slaveOk=false
需要確定master節點後然後重啟控制臺
7.springboot中集群配置
spring:
data:
mongodb:
#有密碼連接 賬號密碼包含特殊字符的需要用URLEncoder編碼 庫名必填
#uri: mongodb://admin:[email protected]:27017/imgdb
#集群方式 副本集replicaSet可帶可不帶
uri: mongodb://192.168.0.221:27017,192.168.0.221:27018/imgdb?replicaSet=newset
# uri: mongodb://192.168.0.221:27017/imgdb
以上就是本章的全部內容了。
上一篇:MongoDB第一話 – Docker安裝MongoDB以及Springboot集成MongoDB
下一篇:JVM第一話 – JVM入門詳解以及運行時數據區分析
書山有路勤為徑,學海無涯苦作舟
边栏推荐
- 博文推荐 | 深入研究 Pulsar 中的消息分块
- Use the npoi package of net core 6 C to read excel Pictures in xlsx cells and stored to the specified server
- Error-tf. function-decorated function tried to create variables on non-first call
- 使用net core 6 c# 的 NPOI 包,读取excel..xlsx单元格内的图片,并存储到指定服务器
- Research Report on the development trend and competitive strategy of the global navigation simulator industry
- 关于重载运算符的再整理
- 问题随记 —— Oracle 11g 卸载
- 2022-2-15 learning xiangniuke project - Section 1 filtering sensitive words
- Rearrangement of overloaded operators
- [dynamic programming] interval dp:p1005 matrix retrieval
猜你喜欢

MIT team used graph neural network to accelerate the screening of amorphous polymer electrolytes and promote the development of next-generation lithium battery technology

Music player development example (can be set up)

Using CMD to repair and recover virus infected files

Leetcode (69) -- square root of X

sqlilabs less-8

Internet hospital system source code hospital applet source code smart hospital source code online consultation system source code

Vnctf2022 open web gocalc0
![[Verilog quick start of Niuke series] ~ multi function data processor, calculate the difference between two numbers, use generate... For statement to simplify the code, and use sub modules to realize](/img/30/aea4ae24f418eb971bca77a1d46bef.png)
[Verilog quick start of Niuke series] ~ multi function data processor, calculate the difference between two numbers, use generate... For statement to simplify the code, and use sub modules to realize
![[15. Interval consolidation]](/img/6c/afc46a0e0d14127d2c234ed9a9d03b.png)
[15. Interval consolidation]
![[commercial terminal simulation solution] Shanghai daoning brings you Georgia introduction, trial and tutorial](/img/44/b65aaf11b1e632f2dab55b6fc699f6.jpg)
[commercial terminal simulation solution] Shanghai daoning brings you Georgia introduction, trial and tutorial
随机推荐
Use the npoi package of net core 6 C to read excel Pictures in xlsx cells and stored to the specified server
Research Report on the development trend and competitive strategy of the global facial wrinkle removal and beauty instrument industry
Quelle valeur le pdnp peut - il apporter aux gestionnaires de produits? Vous savez tout?
【阶段人生总结】放弃考研,参与到工作中,已经顺利毕业了,昨天刚领毕业证
30 Devops interview questions and answers
【14. 区间和(离散化)】
Summary of leetcode's dynamic programming 5
[zero basic IOT pwn] reproduce Netgear wnap320 rce
C#学习笔记(5)类和继承
[15. Interval consolidation]
[零基础学IoT Pwn] 复现Netgear WNAP320 RCE
Rearrangement of overloaded operators
SWT / anr problem - how to capture performance trace
tensorflow2-savedmodel convert to tflite
Using CMD to repair and recover virus infected files
微服务大行其道的今天,Service Mesh是怎样一种存在?
Research Report on the development trend and competitive strategy of the global high temperature label industry
C 语言进阶
Effet halo - qui dit qu'il y a de la lumière sur la tête est un héros
Provincial election + noi Part 10 probability statistics and polynomials