mongodb基础整理篇————副本概念篇[外篇]

前言副本集整理 。开始逐步把mongodb博客补齐了 。
正文什么是副本集副本集是一组服务器,其中一个是用于处理写入操作的主节点,还有多个用于保存主节点的数据副本的从节点 。
如果主节点崩溃了,则从节点会从其中选取出一个新的主节点 。
作用起到一个热备份 和 容灾的作用,如果出现不可预料的事故,比如主节点磁盘损坏,那么可以故障转移,其他节点将会提到主节点进行写入 。
实验现在一台机器上演示 。
步骤一创建对应的目录:
数据:
mkdir -p ~/data/rs{1,2,3}日志:
mkdir -p ~/logs/rs{1,2,3}步骤二启动3个mongod:
mongod --replSet mydb --dbpath ~/data/rs1 --logpath ~/logs/rs1/log --port 27017 --smallfiles --oplogSize=200 &mongod --replSet mydb --dbpath ~/data/rs2 --logpath ~/logs/rs2/log --port 27018 --smallfiles --oplogSize=200 &mongod --replSet mydb --dbpath ~/data/rs3 --logpath ~/logs/rs3/log --port 27019 --smallfiles --oplogSize=200 &步骤三把副本连接到一起,副本集配置传递:
mongo --port 27017初始化副本集配置:
rsconf={ "_id" : "mydb", "members" : [{"_id" : 0,"host" : "localhost:27017"},{"_id" : 1,"host" : "localhost:27018"},{"_id" : 2,"host" : "localhost:27019"} ]}rs.initiate(rsconf)这个配置文档就是副本集的配置 。在localhost:27017 上运行的成员会解析配置并将消息发送给其他成员,提醒他们存在新的配置 。一旦所有成员都加载了配置,他们就会选择一个主节点并开始处理读写操作 。
注意:
不能在不停机的情况下将单机服务器转换为副本集,以重新启动并初始化该副本集 。因此,即使一开始只有一台服务器,你也希望将其配置为一个单成员的副本集 。这样,如果以后想添加更多成员,则可以在不停止运行的情况下进行添加 。然后使用rs.status()查看副本集的状态
{ "set" : "mydb", "date" : ISODate("2022-10-16T02:51:57.670Z"), "myState" : 1, "term" : NumberLong(1), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : {"lastCommittedOpTime" : {"ts" : Timestamp(1665888717, 1),"t" : NumberLong(1)},"readConcernMajorityOpTime" : {"ts" : Timestamp(1665888717, 1),"t" : NumberLong(1)},"appliedOpTime" : {"ts" : Timestamp(1665888717, 1),"t" : NumberLong(1)},"durableOpTime" : {"ts" : Timestamp(1665888717, 1),"t" : NumberLong(1)} }, "members" : [{"_id" : 0,"name" : "localhost:27017","health" : 1,"state" : 1,"stateStr" : "PRIMARY","uptime" : 955,"optime" : {"ts" : Timestamp(1665888717, 1),"t" : NumberLong(1)},"optimeDate" : ISODate("2022-10-16T02:51:57Z"),"syncingTo" : "","syncSourceHost" : "","syncSourceId" : -1,"infoMessage" : "","electionTime" : Timestamp(1665888285, 1),"electionDate" : ISODate("2022-10-16T02:44:45Z"),"configVersion" : 1,"self" : true,"lastHeartbeatMessage" : ""},{"_id" : 1,"name" : "localhost:27018","health" : 1,"state" : 2,"stateStr" : "SECONDARY","uptime" : 443,"optime" : {"ts" : Timestamp(1665888707, 1),"t" : NumberLong(1)},"optimeDurable" : {"ts" : Timestamp(1665888707, 1),"t" : NumberLong(1)},"optimeDate" : ISODate("2022-10-16T02:51:47Z"),"optimeDurableDate" : ISODate("2022-10-16T02:51:47Z"),"lastHeartbeat" : ISODate("2022-10-16T02:51:55.983Z"),"lastHeartbeatRecv" : ISODate("2022-10-16T02:51:57.128Z"),"pingMs" : NumberLong(0),"lastHeartbeatMessage" : "","syncingTo" : "localhost:27017","syncSourceHost" : "localhost:27017","syncSourceId" : 0,"infoMessage" : "","configVersion" : 1},{"_id" : 2,"name" : "localhost:27019","health" : 1,"state" : 2,"stateStr" : "SECONDARY","uptime" : 443,"optime" : {"ts" : Timestamp(1665888707, 1),"t" : NumberLong(1)},"optimeDurable" : {"ts" : Timestamp(1665888707, 1),"t" : NumberLong(1)},"optimeDate" : ISODate("2022-10-16T02:51:47Z"),"optimeDurableDate" : ISODate("2022-10-16T02:51:47Z"),"lastHeartbeat" : ISODate("2022-10-16T02:51:56.020Z"),"lastHeartbeatRecv" : ISODate("2022-10-16T02:51:57.065Z"),"pingMs" : NumberLong(0),"lastHeartbeatMessage" : "","syncingTo" : "localhost:27017","syncSourceHost" : "localhost:27017","syncSourceId" : 0,"infoMessage" : "","configVersion" : 1} ], "ok" : 1, "operationTime" : Timestamp(1665888717, 1), "$clusterTime" : {"clusterTime" : Timestamp(1665888717, 1),"signature" : {"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)} }}

经验总结扩展阅读