当前位置:网站首页>MongoDB复制(副本集)总结
MongoDB复制(副本集)总结
2022-07-07 11:17:00 【cui_yonghua】
基础篇(能解决工作中80%的问题):
进阶篇:
其它:
一. MongoDB 复制(副本集)
MongoDB副本集(Replica Set)是有自动故障恢复功能的主从集群,有一个主节点和一个或多个从节点组成。副本集没有固定的主节点,当主节点发生故障时,整个集群会选举一个主节点为系统提供服务以保证系统的高可用。
MongoDB复制是将数据同步在多个服务器的过程。
复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性, 并可以保证数据的安全性。
复制还允许您从硬件故障和服务中断中恢复数据。
官网参考:MongoDB副本集
二. 为什么要复制?
保障数据的安全性
数据高可用性 (24* 7 )
灾难恢复
无需停机维护(如备份,重建索引,压缩)
读缩放(额外的副本读取)
副本集对应用程序是透明
三. MongoDB复制原理
mongodb的复制至少需要两个节点。其中一个是主节点,负责处理客户端请求,其余的都是从节点,负责复制主节点上的数据。
mongodb各个节点常见的搭配方式为:一主一从、一主多从。
主节点记录在其上的所有操作oplog,从节点定期轮询主节点获取这些操作,然后对自己的数据副本执行这些操作,从而保证从节点的数据与主节点一致。
MongoDB复制结构图如下所示:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UAcjfDeV-1657022462558)(evernotecid://B1CD39FE-B044-413D-A086-0649DB3F0070/appyinxiangcom/26430792/ENResource/p1224)]
以上结构图中,客户端从主节点读取数据,在客户端写入数据到主节点时, 主节点与从节点进行数据交互保障数据的一致性。
副本集特征:
- N 个节点的集群
- 任何节点可作为主节点
- 所有写入操作都在主节点上
- 自动故障转移
- 自动恢复
四. 设置一个副本集
使用同一个MongoDB来做MongoDB主从的实验, 操作步骤如下:
1、关闭正在运行的MongoDB服务器。
2、通过指定 --replSet 选项来启动mongoDB。基本语法格式如下:
mongod --port "PORT" --dbpath "YOUR_DB_DATA_PATH" --replSet "REPLICA_SET_INSTANCE_NAME"
五. 案例
4.1 用适当的选项启动副本集的每个成员
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
还可以通过配置文件中指定副本集名称。启动mongod使用配置文件,与配置选项指定的文件
4.2 mongo shell连接副本集
mongo -port 27020 --host 192.168.17.129
4.3 初始化initiate副本集
利用 rs.initiate() 会在副本集的一个成员上初始化一个默认的复制集配置。
4.4 验证初始副本集配置
使用 rs.conf() 显示副本集配置对象:
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 检查副本集的状态 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 将剩下的成员添加到副本集
必须连接到副本集primary主节点, 才能使用 rs.add() 添加剩余的成员。
rs.add()在某些情况下,触发一个选举。如果连接到主节点primary成为从节点secondary,需要连接Mongo shell到主节点primary继续增加新的副本集成员
利用rs.status()识别副本集主节点primary
4.7 删除副本
rs.remove("192.168.17.129:27021")
以上实例会启动一个名为rs0的MongoDB实例,其端口号为27017。
启动后打开命令提示框并连接上mongoDB服务。
在Mongo客户端使用命令rs.initiate()来启动一个新的副本集。
我们可以使用rs.conf()来查看副本集的配置, 查看副本集状态使用 rs.status() 命令
边栏推荐
- 2022 examination questions and online simulation examination for safety production management personnel of hazardous chemical production units
- 2022 practice questions and mock examination of the third batch of Guangdong Provincial Safety Officer a certificate (main person in charge)
- MySQL导入SQL文件及常用命令
- 云检测2020:用于高分辨率遥感图像中云检测的自注意力生成对抗网络Self-Attentive Generative Adversarial Network for Cloud Detection
- 2022-07-07 Daily: Ian Goodfellow, the inventor of Gan, officially joined deepmind
- The URL modes supported by ThinkPHP include four common modes, pathinfo, rewrite and compatibility modes
- 共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
- Isprs2021/ remote sensing image cloud detection: a geographic information driven method and a new large-scale remote sensing cloud / snow detection data set
- MySQL入门尝鲜
- [learn wechat from 0] [00] Course Overview
猜你喜欢

MySQL master-slave replication

明星企业普渡科技大裁员:曾募资超10亿 腾讯红杉是股东

将数学公式在el-table里面展示出来

- Oui. Migration entièrement automatisée de la Sous - base de données des tableaux d'effets sous net

日本政企员工喝醉丢失46万信息U盘,公开道歉又透露密码规则

Star Enterprise Purdue technology layoffs: Tencent Sequoia was a shareholder who raised more than 1billion

HZOJ #240. Graphic printing IV

Leetcode skimming: binary tree 27 (delete nodes in the binary search tree)

迅为iTOP-IMX6ULL开发板Pinctrl和GPIO子系统实验-修改设备树文件

3D content generation based on nerf
随机推荐
Awk of three swordsmen in text processing
Leetcode skimming: binary tree 22 (minimum absolute difference of binary search tree)
[difficult and miscellaneous]pip running suddenly appears modulenotfounderror: no module named 'pip‘
How to reset Firefox browser
测试下摘要
3D content generation based on nerf
Sample chapter of "uncover the secrets of asp.net core 6 framework" [200 pages /5 chapters]
Layer pop-up layer closing problem
Practical example of propeller easydl: automatic scratch recognition of industrial parts
初学XML
Unity build error: the name "editorutility" does not exist in the current context
Session
[learn microservice from 0] [01] what is microservice
2022 examination questions and online simulation examination for safety production management personnel of hazardous chemical production units
【学习笔记】zkw 线段树
2022a special equipment related management (boiler, pressure vessel and pressure pipeline) simulated examination question bank simulated examination platform operation
leecode3. 无重复字符的最长子串
Conversion from non partitioned table to partitioned table and precautions
共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
10 张图打开 CPU 缓存一致性的大门