Table of Contents
一、常见场景
二、工作原理
2.1 关于oplog
2.2 心跳检测以及故障转移
三、管理
3.1 配置细节
3.2 故障转移与恢复
3.3 部署策略
Home Database Mysql Tutorial 【MongoDB】windows平台搭建Mongo数据库复制集(类似集群)(三

【MongoDB】windows平台搭建Mongo数据库复制集(类似集群)(三

Jun 07, 2016 pm 04:06 PM
mongo mongodb windows platform build data

关于windows平台搭建Mongo数据库复制集这个话题,我已经在前面写了两篇博客 第一篇: 如何在windows平台搭建Mongo数据库复制集 第二篇: 数据同步和故障自适应测试 在本篇里面,咱们重点总结一下复制集,以及分析一下它的工作原理 一、常见场景 应用程序和数

关于windows平台搭建Mongo数据库复制集这个话题,我已经在前面写了两篇博客

第一篇: 如何在windows平台搭建Mongo数据库复制集

第二篇: 数据同步和故障自适应测试

在本篇里面,咱们重点总结一下复制集,以及分析一下它的工作原理

一、常见场景

应用程序和数据库之间的网络连接丢失

计划停机、断电、数据库服务硬盘故障等等

复制可以进行故障转移,复制能让你在副本间均衡读负载,保证复制节点与主节点保持同步

二、工作原理

副本集依赖于两个基础机制:oplog和“心跳”(heartbeat).oplog让数据的复制成为可能,而“心跳”则监控健康情况并出发故障转移;

2.1 关于oplog

oplog是MongoDB复制的关键,oplog是一个固定集合,位于每个复制节点的local数据库中,记录了对数据库的所有变更,每次客户端向主节点写入数据,就会自动向主节点的oplog里添加爱一条记录,其中博客了足够的信息来再现数据。一旦写操作被复制到某个从节点上,从节点的oplog也会保存一条记录。

local数据库里保存了所有的副本集元数据和oplog,因为本身不能被复制;

\

那我们详细在看oplog

\

在此注意,每个从节点都有一份自己的oplog,从节点使用长轮询的方式立即应用来自主节点oplog的新条目。如果丛节点在主节点的oplog中找不到自己要同步的点,那么就永久停止复制。这是会在日志中有如下异常:

replcation data too stale, halting

caught syncException

调整oplog的大小,利用命令db.getReplicationInfo()可以查看分配了多少oplog空间,同时利用如下命令可以改变默认oplog大小

mongod.exe --replSet myapp --oplogSize 1024 
Copy after login

2.2 心跳检测以及故障转移

副本集的心跳检测有助于选举和故障转移。默认情况下,每个副本集成员每隔2s ping一次其他成员。这样一来系统就可以弄清自己的健康状况了。运行rs.status()也可以看到健康状态。

注意:在三个节点中,如果两个从节点都被杀掉了,在主节点的log会多如下一句话:

replSet can't see a majority of the set,

replSet Secondary

意思是没有多数节点,主节点就把自己降级为从节点;

三、管理

由于副本集存在许多潜在的复杂配置项,接下来我们详细介绍这些复杂配置项目;

3.1 配置细节

可以用rs.initiate()和rs.add()方法初始化副本集合。利用config.members.push({})增加节点;
\

其他的一些方法: \

3.2 故障转移与恢复

恢复是在故障后讲副本集还原到原始状态的过程。有两大类故障需要处理。第一类就是包含所有的无损故障,直接重启服务就好。第二种是明确故障,主要是数据文件损坏等等,非正常关闭mongodb服务,如果不更改主机名称和端口号则重新执行数据文件夹,启动后数据后同步过来。如果修改属性,则要用rs.reconfig();

3.3 部署策略

副本集最多包含12个节点,提供自动故障转移的最小副本集合配置就是先前例子中。包含两个副本和一个仲裁节点。在生产环境中,仲裁机节点可以运行在应用服务器上,而副本则运行在自己的机器上。对于多数环境而言,这样配置经济又高校

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundle Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundle Apr 18, 2025 am 11:48 AM

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:

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

How to solve complex BelongsToThrough relationship problem in Laravel? Use Composer! How to solve complex BelongsToThrough relationship problem in Laravel? Use Composer! Apr 17, 2025 pm 09:54 PM

In Laravel development, dealing with complex model relationships has always been a challenge, especially when it comes to multi-level BelongsToThrough relationships. Recently, I encountered this problem in a project dealing with a multi-level model relationship, where traditional HasManyThrough relationships fail to meet the needs, resulting in data queries becoming complex and inefficient. After some exploration, I found the library staudenmeir/belongs-to-through, which easily installed and solved my troubles through Composer.

git software installation git software installation Apr 17, 2025 am 11:57 AM

Installing Git software includes the following steps: Download the installation package and run the installation package to verify the installation configuration Git installation Git Bash (Windows only)

vscode cannot install extension vscode cannot install extension Apr 15, 2025 pm 07:18 PM

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

MongoDB vs. Oracle: Choosing the Right Database for Your Needs MongoDB vs. Oracle: Choosing the Right Database for Your Needs Apr 22, 2025 am 12:10 AM

MongoDB is suitable for unstructured data and high scalability requirements, while Oracle is suitable for scenarios that require strict data consistency. 1.MongoDB flexibly stores data in different structures, suitable for social media and the Internet of Things. 2. Oracle structured data model ensures data integrity and is suitable for financial transactions. 3.MongoDB scales horizontally through shards, and Oracle scales vertically through RAC. 4.MongoDB has low maintenance costs, while Oracle has high maintenance costs but is fully supported.

See all articles