当前位置:网站首页>Mongodb replication (replica set) summary
Mongodb replication (replica set) summary
2022-07-07 13:12:00 【cui_ yonghua】
The basic chapter ( Can solve the problem of 80% The problem of ):
MongoDB data type 、 Key concepts and shell Commonly used instructions
MongoDB Various additions to documents 、 to update 、 Delete operation summary
Advanced :
Other :
One . MongoDB Copy ( Replica set )
MongoDB Replica set (Replica Set) It is a master-slave cluster with automatic fault recovery function , There is a master node and one or more slave nodes . Replica set has no fixed master node , When the primary node fails , The whole cluster will elect a master node to provide services for the system to ensure the high availability of the system .
MongoDB Replication is the process of synchronizing data on multiple servers .
Replication provides redundant backup of data , And store copies of data on multiple servers , Improved data availability , And can guarantee the security of data .
Replication also allows you to recover data from hardware failures and service outages .
Official website reference :MongoDB Replica set
Two . Why copy ?
Secure data
High availability of data (24* 7 )
disaster recovery
No need to shut down for maintenance ( Backup , Rebuild index , Compress )
Read zoom ( Extra copy reads )
Replica sets are transparent to applications
3、 ... and . MongoDB The principle of replication
mongodb Replication of requires at least two nodes . One of them is the master node , Responsible for handling client requests , The rest are from nodes , Responsible for copying the data on the primary node .
mongodb The common collocation mode of each node is : A master from 、 One master, many followers .
All operations recorded on the master node oplog, Periodically polling the master from the node for these operations , Then perform these operations on your own copy of the data , So as to ensure that the data of the slave node is consistent with that of the master node .
MongoDB Copy the structure diagram as follows :
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-UAcjfDeV-1657022462558)(evernotecid://B1CD39FE-B044-413D-A086-0649DB3F0070/appyinxiangcom/26430792/ENResource/p1224)]
In the structure chart above , The client reads data from the master node , When the client writes data to the master node , Data interaction between master node and slave node ensures data consistency .
Replica set features :
- N Cluster of nodes
- Any node can be the master node
- All writes are on the primary node
- Automatic failover
- Automatic recovery
Four . Set up a replica set
Use the same MongoDB To do it MongoDB The master-slave experiment , The operation steps are as follows :
1、 Close running MongoDB The server .
2、 By designation --replSet Option to start mongoDB. The basic syntax is as follows :
mongod --port "PORT" --dbpath "YOUR_DB_DATA_PATH" --replSet "REPLICA_SET_INSTANCE_NAME"
5、 ... and . Case study
4.1 Start each member of the replica set with the appropriate options
sudo rm -rf /MongoDB/node1 /MongoDB/node2 /MongoDB/node3
sudo mkdir -p /MongoDB/node1 /MongoDB/node2 /MongoDB/node3
sudo mongod —bind_ip 192.168.17.129 —port 27020 —dbpath "/MongoDB/node1" —replSet rs0
sudo mongod —bind_ip 192.168.17.129 —port 27021 —dbpath "/MongoDB/node2" —replSet rs0
sudo mongod —bind_ip 192.168.17.129 —port 27022 —dbpath "/MongoDB/node3" —replSet rs0
You can also specify the replica set name in the configuration file . start-up mongod Use profile , File specified with configuration options
4.2 mongo shell Connect replica set
mongo -port 27020 --host 192.168.17.129
4.3 initialization initiate Replica set
utilize rs.initiate() A default replica set configuration will be initialized on a member of the replica set .
4.4 Verify the initial replica set configuration
Use rs.conf() Show replica set configuration objects :
rs0:OTHER> rs.conf()
{
"_id" : "rs0",
"version" : 1,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "192.168.17.129:27020",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("579b3500299da8059cc5fb99")
}
}
rs0:PRIMARY>
4.5 Check the status of the replica set rs.status()
rs0:PRIMARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2021-07-29T11:13:58.433Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "192.168.17.129:27020",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1200,
"optime" : {
"ts" : Timestamp(1469789441, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-07-29T11:50:41Z"),
"electionTime" : Timestamp(1469789440, 2),
"electionDate" : ISODate("2021-07-29T11:50:40Z"),
"configVersion" : 1,
"self" : true
}
],
"ok" : 1
}
rs0:PRIMARY>
4.6 Add the remaining members to the replica set
You must connect to the replica set primary Master node , Can be used rs.add() Add remaining members .
rs.add() In some cases , Trigger an election . If connected to the master node primary Become a slave node secondary, Connection required Mongo shell To the master node primary Continue to add new replica set members
utilize rs.status() Identify replica set master primary
4.7 Delete copy
rs.remove("192.168.17.129:27021")
The above example will start a named rs0 Of MongoDB example , Its port number is 27017.
After startup, open the command prompt box and connect mongoDB service .
stay Mongo The client uses the command rs.initiate() To start a new replica set .
We can use rs.conf() To view the configuration of the replica set , View replica set status using rs.status() command
边栏推荐
- 云检测2020:用于高分辨率遥感图像中云检测的自注意力生成对抗网络Self-Attentive Generative Adversarial Network for Cloud Detection
- 事务的七种传播行为
- Cookie and session comparison
- 抓细抓实抓好安全生产各项工作 全力确保人民群众生命财产安全
- MongoDB内部的存储原理
- 靠卖概念上市,认养一头牛能走多远?
- 博文推荐|Apache Pulsar 跨地域复制方案选型实践
- COSCon'22 社区召集令来啦!Open the World,邀请所有社区一起拥抱开源,打开新世界~
- Vscade editor esp32 header file wavy line does not jump completely solved
- Scrapy教程经典实战【新概念英语】
猜你喜欢
随机推荐
JS function 返回多个值
TPG x AIDU|AI领军人才招募计划进行中!
MongoDB命令汇总
Users, groups, and permissions
MongoDB优化的几点原则
[learning notes] segment tree selection
MongoDB内部的存储原理
企业级自定义表单引擎解决方案(十二)--体验代码目录结构
How does MySQL create, delete, and view indexes?
【学习笔记】线段树选做
MongoDB 遇见 spark(进行整合)
How to reset Firefox browser
LIS 最长上升子序列问题(动态规划、贪心+二分)
MongoDB的导入导出、备份恢复总结
What are the benefits of ip2long?
[untitled]
Cinnamon 任务栏网速
JS缓动动画原理教学(超细节)
测试下摘要
Layer pop-up layer closing problem




![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)




