当前位置:网站首页>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()
命令
边栏推荐
- Cinnamon 任务栏网速
- 【学习笔记】线段树选做
- Aosikang biological sprint scientific innovation board of Hillhouse Investment: annual revenue of 450million yuan, lost cooperation with kangxinuo
- Enterprise custom form engine solution (XII) -- experience code directory structure
- Sequoia China completed the new phase of $9billion fund raising
- Common text processing tools
- About the problem of APP flash back after appium starts the app - (solved)
- 滑轨步进电机调试(全国海洋航行器大赛)(STM32主控)
- Ogre入门尝鲜
- Coscon'22 community convening order is coming! Open the world, invite all communities to embrace open source and open a new world~
猜你喜欢
聊聊伪共享
关于 appium 如何关闭 app (已解决)
Coscon'22 community convening order is coming! Open the world, invite all communities to embrace open source and open a new world~
.Net下极限生产力之efcore分表分库全自动化迁移CodeFirst
Milkdown 控件图标
MySQL入门尝鲜
Go language learning notes - structure
Blog recommendation | Apache pulsar cross regional replication scheme selection practice
Differences between MySQL storage engine MyISAM and InnoDB
飞桨EasyDL实操范例:工业零件划痕自动识别
随机推荐
.Net下极限生产力之efcore分表分库全自动化迁移CodeFirst
抓细抓实抓好安全生产各项工作 全力确保人民群众生命财产安全
DHCP 动态主机设置协议 分析
regular expression
Leetcode skimming: binary tree 21 (verifying binary search tree)
《ASP.NET Core 6框架揭秘》样章[200页/5章]
学习突围2 - 关于高效学习的方法
如何让electorn打开的新窗口在window任务栏上面
[learn microservice from 0] [01] what is microservice
“新红旗杯”桌面应用创意大赛2022
Star Enterprise Purdue technology layoffs: Tencent Sequoia was a shareholder who raised more than 1billion
HZOJ #235. Recursive implementation of exponential enumeration
@What is the difference between resource and @autowired?
Leetcode skimming: binary tree 25 (the nearest common ancestor of binary search tree)
【无标题】
Leetcode skimming: binary tree 27 (delete nodes in the binary search tree)
The URL modes supported by ThinkPHP include four common modes, pathinfo, rewrite and compatibility modes
Conversion from non partitioned table to partitioned table and precautions
- Oui. Migration entièrement automatisée de la Sous - base de données des tableaux d'effets sous net
国泰君安证券开户怎么开的?开户安全吗?