当前位置:网站首页>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 less9
- SWT / anr problem - how to capture performance trace
- What are the books that have greatly improved the thinking and ability of programming?
- 【商业终端仿真解决方案】上海道宁为您带来Georgia介绍、试用、教程
- [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
- Research Report on the development trend and competitive strategy of the global pipeline robot inspection camera industry
- WebSocket(简单体验版)
- Leetcode(69)——x 的平方根
- Quelle valeur le pdnp peut - il apporter aux gestionnaires de produits? Vous savez tout?
猜你喜欢
What are the books that have greatly improved the thinking and ability of programming?
关于重载运算符的再整理
111. Minimum depth of binary tree
【15. 区间合并】
首届技术播客月开播在即
[Verilog quick start of Niuke question series] ~ use functions to realize data size conversion
One of the data Lake series | you must love to read the history of minimalist data platforms, from data warehouse, data lake to Lake warehouse
Today, with the popularity of micro services, how does service mesh exist?
Tdengine connector goes online Google Data Studio app store
Salesforce, Johns Hopkins, Columbia | progen2: exploring the boundaries of protein language models
随机推荐
[零基础学IoT Pwn] 复现Netgear WNAP320 RCE
Salesforce、约翰霍普金斯、哥大 | ProGen2: 探索蛋白语言模型的边界
Research Report on the development trend and competitive strategy of the global powder filling machine industry
Opencv mat class
Research Report on the development trend and competitive strategy of the global pipeline robot inspection camera industry
户外LED显示屏应该考虑哪些问题?
【牛客网刷题系列 之 Verilog快速入门】~ 多功能数据处理器、求两个数的差值、使用generate…for语句简化代码、使用子模块实现三输入数的大小比较
[stage life summary] I gave up the postgraduate entrance examination and participated in the work. I have successfully graduated and just received my graduation certificate yesterday
Websocket (simple experience version)
Why did you win the first Taosi culture award of 20000 RMB if you are neither a top R & D expert nor a sales Daniel?
使用net core 6 c# 的 NPOI 包,讀取excel..xlsx單元格內的圖片,並存儲到指定服務器
qt捕获界面为图片或label显示
博文推荐 | 深入研究 Pulsar 中的消息分块
微服务大行其道的今天,Service Mesh是怎样一种存在?
Quelle valeur le pdnp peut - il apporter aux gestionnaires de produits? Vous savez tout?
Basic knowledge of C language
C 语言基础
Research Report on the development trend and competitive strategy of the global chemical glassware industry
2022-2-15 learning the imitation Niuke project - Section 3 post details
Leetcode(69)——x 的平方根