search
HomeDatabaseMysql TutorialMongo服务器集群配置学习一主从复制

Mongo服务器集群配置学习一主从复制 主从复制是MongoDB最常用也是最简单的复制操作。常用于数据备份和故障修复等。 下面这个图就是最简单的主从复制的服务器架构 我将以实验的方式来实现MongoDB的主从复制 实验环境:windows操作系统(一台机器启动多个Mongo

Mongo服务器集群配置学习一——主从复制

主从复制是MongoDB最常用也是最简单的复制操作。常用于数据备份和故障修复等。

下面这个图就是最简单的主从复制的服务器架构

Mongo服务器集群配置学习一主从复制
我将以实验的方式来实现MongoDB的主从复制
实验环境:windows操作系统(一台机器启动多个MongoDB数据库),MongoDB 2.4
说明:
1.MongoDB以配置文件的形式启动
2.以执行保存的bat文件代替每次输入CMD中输入命令
步骤:
1.配置主节点并启动,端口为10001,下图为配置的文件结构
Mongo服务器集群配置学习一主从复制
其中config.cnf的内容为
dbpath=D:\mongodb\test\copy\10001\Data
bind_ip=127.0.0.1
port=10001
master=true
用startup.bat启动主节点:mongod -f config.cnf
用shell.bat启动shell:mongo 127.0.0.1:10001
其中master参数为true说明这台是主节点
2.配置从数据库,端口为10002
config.cnf的内容为
dbpath=D:\mongodb\test\copy\10002\Data
bind_ip=127.0.0.1
port=10002
slave=true
source=127.0.0.1:10001
用startup.bat启动从节点:mongod -f config.cnf
用shell.bat启动shell:mongo 127.0.0.1:10002
slave参数设置从节点,source从数据库对应的主节点的地址
3.下面就来做下验证,在10001主节点上的person数据库添加集合baseinfo,并添加一条文档
Mongo服务器集群配置学习一主从复制
这个时候再来10002从节点查询,就可以看到这条同样的数据已经复制过来了。
Mongo服务器集群配置学习一主从复制
4.其他参数
--only 从节点配置,只复制特定某个数据库
--autoresync 从节点配置,如果主节点与从节点数据不同,则自动重新同步。配置这个节点可以给运行了一段时间的主节点加上一个新节点,则这个新节点会把之前的主节点数据全部同步过来,而不是从现在这个时间同步。
--slavedelay 从节点配置,从数据库延迟同步主数据库的时间
--fastsync 从节点配置,以主节点的数据库快照启动从节点,可以加快启动速度。
--oplogsize 主节点配置,主节点oplog大小,主节点会把数据库操作的日志写在oplog中,从节点参考oplog做复制操作,可以根据自身情况调节日志大小。如果不指定oplogsize大小,mongod将指配5%的可用磁盘空间给他,32位机最小是50M,64位机最小是1G。
动态添加删除主从节点
先看看从节点的对于主节点的配置在哪,在从节点的local数据库的sources集合中,查看信息如下:
Mongo服务器集群配置学习一主从复制
现在再启动一台普通的节点,不设置任何主从,端口设为10003
dbpath=D:\mongodb\test\copy\10003\Data
bind_ip=127.0.0.1
port=10003
slave=true
启动后,动态的把10003加入到主从架构中,形成如下的结构
Mongo服务器集群配置学习一主从复制
在10003的shell中执行下面脚本即可。
use local
db.sources.insert({"host":"127.0.0.1:10001"})
这样10003就作为10001的从节点了
删除主从关系就用db.sources.remove({"host":"127.0.0.1:10001"})

主节点往从节点转移
永久的转移一个损坏的和不可用的主节点A到从节点B,有以下步骤:
1.关闭A节点
2.停止B节点的Mongod
3.对B节点的dbpath目录下的文件进行备份和移动
注:删除local.*是不可撤销的。执行此步骤非常谨慎。
4.在B节点上用--master参数重启Mongod
调换主节点和从节点
有一个主节点A和一个从节点B,如果想调换他们的角色,请按下面的步骤,这里假设A是健康的,可更新的可用的
如果A是不是健康的,但硬件是好的(停电,服务器崩溃等),跳过步骤1和2,并在第8步用B的文件取代所有的文件。
如果A是不是健康的,硬件是不好的,将A替换为一台新机器。可以按照上段中的说明。
1.暂停在A上使用fsync命令
2.确定B是在同步节点A
3.关闭B节点
4.从B的DBPATH目录备份和移动的所有数据文件,并删除现有的数据local.sources。
5.用master选项启动B
6.往B中写入数据,用oplog设置新的同步开始时间点
7.关闭B,当重启时B就有新的一组本地数据
8.关闭A,用备份B的dbpath目录文件复制到A的dbpath中
9.用master选项启动B
10.用通常slave选项启动A,但要包括fastsync参数

Statement
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
MySQL's Role: Databases in Web ApplicationsMySQL's Role: Databases in Web ApplicationsApr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

MySQL: Building Your First DatabaseMySQL: Building Your First DatabaseApr 17, 2025 am 12:22 AM

The steps to build a MySQL database include: 1. Create a database and table, 2. Insert data, and 3. Conduct queries. First, use the CREATEDATABASE and CREATETABLE statements to create the database and table, then use the INSERTINTO statement to insert the data, and finally use the SELECT statement to query the data.

MySQL: A Beginner-Friendly Approach to Data StorageMySQL: A Beginner-Friendly Approach to Data StorageApr 17, 2025 am 12:21 AM

MySQL is suitable for beginners because it is easy to use and powerful. 1.MySQL is a relational database, and uses SQL for CRUD operations. 2. It is simple to install and requires the root user password to be configured. 3. Use INSERT, UPDATE, DELETE, and SELECT to perform data operations. 4. ORDERBY, WHERE and JOIN can be used for complex queries. 5. Debugging requires checking the syntax and use EXPLAIN to analyze the query. 6. Optimization suggestions include using indexes, choosing the right data type and good programming habits.

Is MySQL Beginner-Friendly? Assessing the Learning CurveIs MySQL Beginner-Friendly? Assessing the Learning CurveApr 17, 2025 am 12:19 AM

MySQL is suitable for beginners because: 1) easy to install and configure, 2) rich learning resources, 3) intuitive SQL syntax, 4) powerful tool support. Nevertheless, beginners need to overcome challenges such as database design, query optimization, security management, and data backup.

Is SQL a Programming Language? Clarifying the TerminologyIs SQL a Programming Language? Clarifying the TerminologyApr 17, 2025 am 12:17 AM

Yes,SQLisaprogramminglanguagespecializedfordatamanagement.1)It'sdeclarative,focusingonwhattoachieveratherthanhow.2)SQLisessentialforquerying,inserting,updating,anddeletingdatainrelationaldatabases.3)Whileuser-friendly,itrequiresoptimizationtoavoidper

Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Apr 16, 2025 am 12:20 AM

ACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.

MySQL: Database Management System vs. Programming LanguageMySQL: Database Management System vs. Programming LanguageApr 16, 2025 am 12:19 AM

MySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.

MySQL: Managing Data with SQL CommandsMySQL: Managing Data with SQL CommandsApr 16, 2025 am 12:19 AM

MySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function