当前位置:网站首页>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入門詳解以及運行時數據區分析
書山有路勤為徑,學海無涯苦作舟
边栏推荐
- 建立自己的网站(14)
- 写在Doris毕业后的第一天
- Use the right scene, get twice the result with half the effort! Full introduction to the window query function and usage scenarios of tdengine
- [dynamic programming] interval dp:p1005 matrix retrieval
- C 语言基础
- Research Report on the development trend and competitive strategy of the global camera filter bracket industry
- Research Report on the development trend and competitive strategy of the global diamond suspension industry
- Use of Oracle database objects
- sqlilabs less9
- Salesforce、约翰霍普金斯、哥大 | ProGen2: 探索蛋白语言模型的边界
猜你喜欢
Vnctf2022 open web gocalc0
C learning notes (5) class and inheritance
[leetcode 324] swing sorting II thinking + sorting
Blog recommendation | in depth study of message segmentation in pulsar
如何看待国企纷纷卸载微软Office改用金山WPS?
111. Minimum depth of binary tree
用对场景,事半功倍!TDengine 的窗口查询功能及使用场景全介绍
JVM performance tuning and practical basic theory part II
JVM performance tuning and practical basic theory part II
既不是研发顶尖高手,也不是销售大牛,为何偏偏获得 2 万 RMB 的首个涛思文化奖?
随机推荐
Is the futures company found on Baidu safe? How do futures companies determine the regularity
What value can NPDP bring to product managers? Do you know everything?
[leetcode 324] 摆动排序 II 思维+排序
生成随机数(4位、6位)
Using CMD to repair and recover virus infected files
Research Report on the development trend and competitive strategy of the global powder filling machine industry
Halo effect - who says that those with light on their heads are heroes
Don't want to knock the code? Here comes the chance
Research Report on the development trend and competitive strategy of the global chemical glassware industry
Build your own website (14)
About the use of HTTP cache validation last modified and Etag
[commercial terminal simulation solution] Shanghai daoning brings you Georgia introduction, trial and tutorial
[R language data science]: common evaluation indicators of machine learning
QT capture interface is displayed as picture or label
Effet halo - qui dit qu'il y a de la lumière sur la tête est un héros
Salesforce、约翰霍普金斯、哥大 | ProGen2: 探索蛋白语言模型的边界
tensorflow2-savedmodel convert to tflite
241. 为运算表达式设计优先级
Quelle valeur le pdnp peut - il apporter aux gestionnaires de produits? Vous savez tout?
【牛客网刷题系列 之 Verilog快速入门】~ 多功能数据处理器、求两个数的差值、使用generate…for语句简化代码、使用子模块实现三输入数的大小比较