当前位置:网站首页>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
边栏推荐
- [零基础学IoT Pwn] 复现Netgear WNAP320 RCE
- After twists and turns, I finally found the method of SRC vulnerability mining [recommended collection]
- 百度上找的期货公司安全吗?期货公司怎么确定正规
- Opencv mat class
- Use the npoi package of net core 6 C to read excel Pictures in xlsx cells and stored to the specified server
- Sqlachemy common operations
- 关于软件测试的一些思考
- Research Report on the development trend and competitive strategy of the global electromagnetic flowmeter industry
- That hard-working student failed the college entrance examination... Don't panic! You have another chance to counter attack!
- Error-tf. function-decorated function tried to create variables on non-first call
猜你喜欢

对于编程思想和能力有重大提升的书有哪些?

【15. 区间合并】

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

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

sqlilabs less-11~12

既不是研发顶尖高手,也不是销售大牛,为何偏偏获得 2 万 RMB 的首个涛思文化奖?
![[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

Guess lantern riddles, not programmers still can't understand?

MIT团队使用图神经网络,加速无定形聚合物电解质筛选,促进下一代锂电池技术开发

微服务大行其道的今天,Service Mesh是怎样一种存在?
随机推荐
Yyds dry goods inventory hcie security day13: firewall dual machine hot standby experiment (I) firewall direct deployment, uplink and downlink connection switches
Details of appium key knowledge
2022-2-15 learning xiangniuke project - Section 4 business management
SQLAchemy 常用操作
Research Report on the development trend and competitive strategy of the global diamond suspension industry
[零基础学IoT Pwn] 复现Netgear WNAP320 RCE
241. 为运算表达式设计优先级
音乐播放器开发实例(可毕设)
JVM performance tuning and practical basic theory part II
[leetcode 324] 摆动排序 II 思维+排序
Sorting learning sorting
SWT / anr problem - how to capture performance trace
Research Report on development trend and competitive strategy of global 4-aminodiphenylamine industry
Some thoughts on software testing
户外LED显示屏应该考虑哪些问题?
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
一波三折,终于找到src漏洞挖掘的方法了【建议收藏】
【14. 区间和(离散化)】
Word2vec yyds dry goods inventory
[zero basic IOT pwn] reproduce Netgear wnap320 rce