mongodb基础系列—副本集具体搭建以及解说
既然有了主从复制,为啥又出来个副本集?一个东西的出现,肯定存在一种东西被解救。所谓一物降一物O(_)O 上文中说道主从复制缺点主服务器出现故障,从服务器是干瞪眼。这篇文章就要解说,主仆之间会有一个怎样的变化? 仍然是一台电脑工作。步骤如下: 1.准
既然有了主从复制,为啥又出来个副本集?一个东西的出现,肯定存在一种东西被解救。所谓一物降一物O(∩_∩)O 上文中说道主从复制缺点—主服务器出现故障,从服务器是干瞪眼。这篇文章就要解说,主仆之间会有一个怎样的变化?
仍然是一台电脑工作。步骤如下:
1.准备条件,模拟三个服务器,一个主primary,两个secondary(可以称副节点)
可以新建目录如下: 以三个节点为例:节点1: HOST:localhost:10001 Log File:e:\mastersavle\logs\node1\logs.txt Data File:E:\mastersavle\dbs\node1 节点2: HOST:localhost:10002 Log File:E:\mastersavle\logs\node2\logs.txt Data File:E:\mastersavle\dbs\node2 节点3: HOST:localhost:10003 Log File:E:\mastersavle\logs\node3\logs.txt Data File:E:\mastersavle\dbs\node3
2.启动三个节点,观察启动后每个节点的日志文件
启动节点1:
mongod --dbpath E:\mastersavle\dbs\node1 --logpath E:\mastersavle\logs\node1\logs.txt --logappend --port 10001 --replSet itcast/localhost:10002 --master

我们看看log日志:

启动节点2:
mongod --dbpath E:\mastersavle\dbs\node2 --logpath E:\mastersavle\logs\node2\logs.txt --logappend --port 10002 --replSet itcast/localhost:10001

启动节点3:
mongod --dbpath E:\mastersavle\dbs\node3 --logpath E:\mastersavle\logs\node3\logs.txt --logappend --port 10003 --replSet itcast/localhost:10001,localhost:10002

3.初始化节点:只初始化一次,随便登陆三个节点任何一个,但是必须进入admin数据库
节点2和节点3日志文件中已经告诉我们,replSet info you may need to run replSetInitiate,需要运行初始化函数。db.runCommand({"replSetInitiate":{"_id":"itcast","members":[{"_id":1,"host":"localhost:10001","priority":3},{"_id":2,"host":"localhost:10002","priority":2},{"_id":3,"host":"localhost:10003","priority":1}]}});

4.开启另一个窗口,测试谁是主服务器
连接10001端口:
查看是否是主服务器:db.$cmd.findOne ( {ismaster: 1 } );

5.测试副本集特性,断开主节点,看副节点有何变化?
直接把主节点服务断开,10001日志文件和10002日志,则会打印,连不上10001问题。 10002节点日志文件:
10003节点日志文件:

通过控制台发现:

6.测试几种情况,目前剩下10002(主)和10003(副),宕掉主,副会有啥变化
继续宕掉10002,看看10003有啥变化?

集群中因为投票数不大于总数一半,所以10003是不会升级为主服务器的。另一种测试,我们把10003起来后,目前是10002是主服务器,我们现在宕掉10003副节点,看效果:


7.副本集同步原理:如何保持主从节点数据一致性:
上篇博客提到主从复制中,主节点记录在local数据库,来保持同步性。副本集其实也是类似的,主节点操作数据库状态的日志,记录在local.oplog.rs集合中,这样,备份节点通过读取日志,在自己数据库完完全全执行一遍。【不记录查询哈,同上篇】 local数据中集合中local.system.replset中没有任何配置信息,是记录节点初始化
local.oplog.rs集合记录主节点操作数据库状态日志:

8.总结
从上文测试可以看出,副本集和主从复制的区别副本集特性:1.没有固定的master主节点。2.故障自动恢复,继续工作。没有固定的主节点,是因为一个主节点宕掉,集群中会elect投票,从副节点secondary中选举一个,升级为主节点。总体而言,副本集就是具有故障自动恢复的主从复制。其实主从复制,目前官网也也不怎么提倡,因为主从复制只能起到备份作用,没有达到自动恢复效果。 ps:主节点其实就是活跃点,副节点、从节点就是备份节点。
Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The Xiaomi Mi 15 series is expected to be officially released in October, and its full series codenames have been exposed in the foreign media MiCode code base. Among them, the flagship Xiaomi Mi 15 Ultra is codenamed "Xuanyuan" (meaning "Xuanyuan"). This name comes from the Yellow Emperor in Chinese mythology, which symbolizes nobility. Xiaomi 15 is codenamed "Dada", while Xiaomi 15Pro is named "Haotian" (meaning "Haotian"). The internal code name of Xiaomi Mi 15S Pro is "dijun", which alludes to Emperor Jun, the creator god of "The Classic of Mountains and Seas". Xiaomi 15Ultra series covers

Since the Huawei Mate60 series went on sale last year, I personally have been using the Mate60Pro as my main phone. In nearly a year, Huawei Mate60Pro has undergone multiple OTA upgrades, and the overall experience has been significantly improved, giving people a feeling of being constantly new. For example, recently, the Huawei Mate60 series has once again received a major upgrade in imaging capabilities. The first is the new AI elimination function, which can intelligently eliminate passers-by and debris and automatically fill in the blank areas; secondly, the color accuracy and telephoto clarity of the main camera have been significantly upgraded. Considering that it is the back-to-school season, Huawei Mate60 series has also launched an autumn promotion: you can enjoy a discount of up to 800 yuan when purchasing the phone, and the starting price is as low as 4,999 yuan. Commonly used and often new products with great value

This article introduces how to configure MongoDB on Debian system to achieve automatic expansion. The main steps include setting up the MongoDB replica set and disk space monitoring. 1. MongoDB installation First, make sure that MongoDB is installed on the Debian system. Install using the following command: sudoaptupdatesudoaptinstall-ymongodb-org 2. Configuring MongoDB replica set MongoDB replica set ensures high availability and data redundancy, which is the basis for achieving automatic capacity expansion. Start MongoDB service: sudosystemctlstartmongodsudosys

This article describes how to build a highly available MongoDB database on a Debian system. We will explore multiple ways to ensure data security and services continue to operate. Key strategy: ReplicaSet: ReplicaSet: Use replicasets to achieve data redundancy and automatic failover. When a master node fails, the replica set will automatically elect a new master node to ensure the continuous availability of the service. Data backup and recovery: Regularly use the mongodump command to backup the database and formulate effective recovery strategies to deal with the risk of data loss. Monitoring and Alarms: Deploy monitoring tools (such as Prometheus, Grafana) to monitor the running status of MongoDB in real time, and

It is impossible to view MongoDB password directly through Navicat because it is stored as hash values. How to retrieve lost passwords: 1. Reset passwords; 2. Check configuration files (may contain hash values); 3. Check codes (may hardcode passwords).

MongoDB and relational database: In-depth comparison This article will explore in-depth the differences between NoSQL database MongoDB and traditional relational databases (such as MySQL and SQLServer). Relational databases use table structures of rows and columns to organize data, while MongoDB uses flexible document-oriented models to better suit the needs of modern applications. Mainly differentiates data structures: Relational databases use predefined schema tables to store data, and relationships between tables are established through primary keys and foreign keys; MongoDB uses JSON-like BSON documents to store them in a collection, and each document structure can be independently changed to achieve pattern-free design. Architectural design: Relational databases need to pre-defined fixed schema; MongoDB supports

Detailed explanation of MongoDB efficient backup strategy under CentOS system This article will introduce in detail the various strategies for implementing MongoDB backup on CentOS system to ensure data security and business continuity. We will cover manual backups, timed backups, automated script backups, and backup methods in Docker container environments, and provide best practices for backup file management. Manual backup: Use the mongodump command to perform manual full backup, for example: mongodump-hlocalhost:27017-u username-p password-d database name-o/backup directory This command will export the data and metadata of the specified database to the specified backup directory.

When developing an e-commerce website, I encountered a difficult problem: how to provide users with personalized product recommendations. Initially, I tried some simple recommendation algorithms, but the results were not ideal, and user satisfaction was also affected. In order to improve the accuracy and efficiency of the recommendation system, I decided to adopt a more professional solution. Finally, I installed andres-montanez/recommendations-bundle through Composer, which not only solved my problem, but also greatly improved the performance of the recommendation system. You can learn composer through the following address:
