当前位置:网站首页>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
边栏推荐
- 【无标题】
- Cookie and session comparison
- JS determines whether an object is empty
- DrawerLayout禁止侧滑显示
- [untitled]
- JS缓动动画原理教学(超细节)
- Cmu15445 (fall 2019) project 2 - hash table details
- Enterprise custom form engine solution (XII) -- experience code directory structure
- Star Enterprise Purdue technology layoffs: Tencent Sequoia was a shareholder who raised more than 1billion
- ESP32系列专栏
猜你喜欢

OSI 七层模型

【学习笔记】AGC010

Vscade editor esp32 header file wavy line does not jump completely solved

Ogre入门尝鲜

TPG x AIDU|AI领军人才招募计划进行中!

详细介绍六种开源协议(程序员须知)
![[learning notes] agc010](/img/2c/37f2537a4dadd84adacf3da5f1327a.png)
[learning notes] agc010

Japanese government and enterprise employees got drunk and lost 460000 information USB flash drives. They publicly apologized and disclosed password rules

MATLAB中polarscatter函数使用

Aosikang biological sprint scientific innovation board of Hillhouse Investment: annual revenue of 450million yuan, lost cooperation with kangxinuo
随机推荐
leecode3. 无重复字符的最长子串
【学习笔记】线段树选做
飞桨EasyDL实操范例:工业零件划痕自动识别
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
Cinnamon 任务栏网速
Aosikang biological sprint scientific innovation board of Hillhouse Investment: annual revenue of 450million yuan, lost cooperation with kangxinuo
MATLAB中polarscatter函数使用
ESP32系列专栏
ESP32 ① 编译环境
.Net下極限生產力之efcore分錶分庫全自動化遷移CodeFirst
Per capita Swiss number series, Swiss number 4 generation JS reverse analysis
MySQL master-slave replication
初学XML
云检测2020:用于高分辨率遥感图像中云检测的自注意力生成对抗网络Self-Attentive Generative Adversarial Network for Cloud Detection
信号强度(RSSI)知识整理
. Net ultimate productivity of efcore sub table sub database fully automated migration codefirst
红杉中国完成新一期90亿美元基金募集
Sample chapter of "uncover the secrets of asp.net core 6 framework" [200 pages /5 chapters]
日本政企员工喝醉丢失46万信息U盘,公开道歉又透露密码规则
Go语言学习笔记-结构体(Struct)