当前位置:网站首页>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入門詳解以及運行時數據區分析
書山有路勤為徑,學海無涯苦作舟
边栏推荐
- sqlilabs less13
- 【R语言数据科学】:机器学习常见评估指标
- 一波三折,终于找到src漏洞挖掘的方法了【建议收藏】
- Research Report on development trend and competitive strategy of global consumer glassware industry
- sqlilabs less-8
- Semiconductor foundation of binary realization principle
- QT capture interface is displayed as picture or label
- Phpcms realizes the direct Alipay payment function of orders
- Research Report on the development trend and competitive strategy of the global indexable milling cutter industry
- [dynamic programming] interval dp:p1005 matrix retrieval
猜你喜欢

2022-2-15 learning xiangniuke project - Section 1 filtering sensitive words

JVM performance tuning and practical basic theory part II

How to view the state-owned enterprises have unloaded Microsoft office and switched to Kingsoft WPS?

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

Today, with the popularity of micro services, how does service mesh exist?

2022-2-15 learning the imitation Niuke project - post in Section 2

sqlilabs less-8
![[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

241. Design priorities for operational expressions

首届技术播客月开播在即
随机推荐
What are the requirements for NPDP product manager international certification registration?
对于编程思想和能力有重大提升的书有哪些?
tensorflow2-savedmodel convert to pb(frozen_graph)
关于重载运算符的再整理
音乐播放器开发实例(可毕设)
sqlilabs less9
首届技术播客月开播在即
Opencv interpolation mode
Details of appium key knowledge
博文推荐 | 深入研究 Pulsar 中的消息分块
Generate random numbers (4-bit, 6-bit)
Use of Oracle database objects
Research Report on the development trend and competitive strategy of the global traditional computer industry
NPDP产品经理国际认证报名有什么要求?
Research Report on the development trend and competitive strategy of the global CCTV robot industry
Error-tf. function-decorated function tried to create variables on non-first call
En utilisant le paquet npoi de net Core 6 c #, lisez Excel.. Image dans la cellule xlsx et stockée sur le serveur spécifié
Basic concepts of programming
Buuctf reinforcement question ezsql
互联网医院系统源码 医院小程序源码 智慧医院源码 在线问诊系统源码