当前位置:网站首页>Mongodb second call -- implementation of mongodb high availability cluster
Mongodb second call -- implementation of mongodb high availability cluster
2022-07-01 14:44:00 【Procedural ape with hair!】
Main records of this paper MongoDB Cluster building
Bad environment preparation :liunx centos7,docker-compose, Mirror image mongo:4.2.21.
One master and one slave Arbitration , In one machine
project | Value |
---|---|
0.221:27017 | node 1 |
0.221:27018 | node 2 |
0.221:27019 | Arbitration node |
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 Define replica set name Can be used after the path ?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:
# Console Only after the cluster takes effect can the connection succeed You can only connect to master Can be pulled out alone
- ME_CONFIG_MONGODB_SERVER=mongo01
# - ME_CONFIG_BASICAUTH_USERNAME=admin
# - ME_CONFIG_BASICAUTH_PASSWORD=admin
- TZ=Asia/Shanghai
networks:
- my-net
networks:
# New network Internal service name call
my-net:
external: true
2. Start the container
[[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. Initialize cluster
# Enter any container perform mongo command
[[email protected] mongo-cluster]# docker exec -it mongo02 mongo
MongoDB shell version v4.2.21
# Perform the initial configuration of the cluster , If you need program connection , You need to fill in the host 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: Corresponding replSet name
host: If you need program connection , You need to fill in the host IP
arbiterOnly:true Indicates that this node is an arbitration node
4. View the cluster status
newset:SECONDARY> rs.status()
# find members Node information
"members" : [
{
"_id" : 0,
"name" : "192.168.0.221:27017",
"health" : 1,
"state" : 1,
# colony 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,
# colony 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,
# Cluster arbitration
"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. simulation master Node down
Stop master node , Get into slave View the status in the node container
"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,
# The original slave The node has been changed to 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
}
],
You can see the original slave The node has become PRIMARY Master node
6.mongo-express Console
- Connection points needing attention
environment:
# No ports Go directly to the container name
- ME_CONFIG_MONGODB_SERVER=mongo01
- The cluster is not initialized or the cluster master Has changed , The console will report the following error
(node:6) UnhandledPromiseRejectionWarning: MongoError: not master and slaveOk=false
You need to determine master After the node, restart the console
7.springboot Cluster configuration in
spring:
data:
mongodb:
# There is a password connection If the account password contains special characters, you need to use URLEncoder code Database name is required
#uri: mongodb://admin:[email protected]:27017/imgdb
# The cluster approach Replica set replicaSet Take it or not
uri: mongodb://192.168.0.221:27017,192.168.0.221:27018/imgdb?replicaSet=newset
# uri: mongodb://192.168.0.221:27017/imgdb
That's all for this chapter .
Last one :MongoDB First words – Docker install MongoDB as well as Springboot Integrate MongoDB
Next :JVM First words – JVM Introduction details and runtime data area analysis
There is a good way to study , There's no end to learning how to make a boat
边栏推荐
- Research Report on development trend and competitive strategy of global consumer glassware industry
- QT capture interface is displayed as picture or label
- Fundamentals of C language
- SQLAchemy 常用操作
- C 语言进阶
- 数据湖系列之一 | 你一定爱读的极简数据平台史,从数据仓库、数据湖到湖仓一体
- What value can NPDP bring to product managers? Do you know everything?
- In depth cooperation | Taosi data cooperates with changhongjia Huawei customers in China to provide tdengine with powerful enterprise level products and perfect service guarantee
- 微服务开发步骤(nacos)
- Research Report on the development trend and competitive strategy of the global ultrasonic scalpel system industry
猜你喜欢
Build your own website (14)
JVM performance tuning and practical basic theory part II
数据湖系列之一 | 你一定爱读的极简数据平台史,从数据仓库、数据湖到湖仓一体
Yyds dry goods inventory hcie security day13: firewall dual machine hot standby experiment (I) firewall direct deployment, uplink and downlink connection switches
Guess lantern riddles, not programmers still can't understand?
Take you to API development by hand
【牛客网刷题系列 之 Verilog快速入门】~ 使用函数实现数据大小端转换
手把手带你入门 API 开发
Minimum spanning tree and bipartite graph in graph theory (acwing template)
既不是研发顶尖高手,也不是销售大牛,为何偏偏获得 2 万 RMB 的首个涛思文化奖?
随机推荐
Research Report on the development trend and competitive strategy of the global indexable milling cutter industry
博文推荐 | 深入研究 Pulsar 中的消息分块
2022-2-15 learning the imitation Niuke project - post in Section 2
Is it reasonable and safe for securities companies to open accounts for 10000 free securities? How to say
Basic operation of database
Salesforce, Johns Hopkins, Columbia | progen2: exploring the boundaries of protein language models
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é
tensorflow2-savedmodel convert to pb(frozen_graph)
Research Report on the development trend and competitive strategy of the global facial wrinkle removal and beauty instrument industry
用对场景,事半功倍!TDengine 的窗口查询功能及使用场景全介绍
[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
JVM performance tuning and practical basic theory part II
[leetcode 324] 摆动排序 II 思维+排序
【14. 区间和(离散化)】
Vnctf2022 open web gocalc0
Research Report on the development trend and competitive strategy of the global chemical glassware industry
Research Report on the development trend and competitive strategy of the global axis measurement system industry
C learning notes (5) class and inheritance
Build your own website (14)
qt捕获界面为图片或label显示