当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
日本政企员工喝醉丢失46万信息U盘,公开道歉又透露密码规则
国泰君安证券开户怎么开的?开户安全吗?
AUTOCAD——大于180度的角度标注、CAD直径符号怎么输入?
MySQL master-slave replication
[untitled]
MongoDB命令汇总
抓细抓实抓好安全生产各项工作 全力确保人民群众生命财产安全
关于 appium 启动 app 后闪退的问题 - (已解决)
JS缓动动画原理教学(超细节)
regular expression
How does MySQL create, delete, and view indexes?
JS function 返回多个值
OSI 七层模型
shell 批量文件名(不含扩展名)小写改大写
Query whether a field has an index with MySQL
Scrapy教程经典实战【新概念英语】
【学习笔记】线段树选做
MongoDB内部的存储原理
[untitled]
【学习笔记】AGC010