当前位置:网站首页>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
边栏推荐
- Unity 构建错误:当前上下文中不存在名称“EditorUtility”
- Practical case: using MYCAT to realize read-write separation of MySQL
- The difference between cache and buffer
- 聊聊伪共享
- How to make the new window opened by electorn on the window taskbar
- . Net ultimate productivity of efcore sub table sub database fully automated migration codefirst
- Enterprise custom form engine solution (XII) -- experience code directory structure
- regular expression
- 【无标题】
- ClickHouse(03)ClickHouse怎么安装和部署
猜你喜欢
Coscon'22 community convening order is coming! Open the world, invite all communities to embrace open source and open a new world~
Adopt a cow to sprint A shares: it plans to raise 1.85 billion yuan, and Xu Xiaobo holds nearly 40%
Differences between MySQL storage engine MyISAM and InnoDB
Blog recommendation | Apache pulsar cross regional replication scheme selection practice
AUTOCAD——大于180度的角度标注、CAD直径符号怎么输入?
DHCP 动态主机设置协议 分析
Practical example of propeller easydl: automatic scratch recognition of industrial parts
DETR介绍
Go language learning notes - structure
MATLAB中polarscatter函数使用
随机推荐
《开源圆桌派》第十一期“冰与火之歌”——如何平衡开源与安全间的天然矛盾?
DrawerLayout禁止侧滑显示
【无标题】
error LNK2019: 无法解析的外部符号
Unity build error: the name "editorutility" does not exist in the current context
MongoDB 分片总结
Lingyunguang of Dachen and Xiaomi investment is listed: the market value is 15.3 billion, and the machine is implanted into the eyes and brain
工具箱之 IKVM.NET 项目新进展
关于 appium 如何关闭 app (已解决)
如何让electorn打开的新窗口在window任务栏上面
博文推荐|Apache Pulsar 跨地域复制方案选型实践
Japanese government and enterprise employees got drunk and lost 460000 information USB flash drives. They publicly apologized and disclosed password rules
php——laravel缓存cache
Cmu15445 (fall 2019) project 2 - hash table details
OSI 七层模型
Conversion from non partitioned table to partitioned table and precautions
AUTOCAD——大于180度的角度标注、CAD直径符号怎么输入?
滑轨步进电机调试(全国海洋航行器大赛)(STM32主控)
【黑马早报】华为辟谣“军师”陈春花;恒驰5预售价17.9万元;周杰伦新专辑MV 3小时播放量破亿;法华寺回应万元月薪招人...
[learning notes] agc010