Home Database Mysql Tutorial mongodb复制集的搭建

mongodb复制集的搭建

Jun 07, 2016 pm 04:06 PM
mongo mongodb copy copy build Group node

副本集是一组,由N个mongo节点组成并协同工作的,提供自动的故障集群转移。 通俗的讲就是用多台机器进行同一数据的异步同步,从而使多台机器拥有同一数据的多个副本,其中有一个主服务器(primary),用户处理客户端请求,还有多个备份服务器(secondary),

副本集是一组,由N个mongo节点组成并协同工作的,提供自动的故障集群转移。

通俗的讲就是用多台机器进行同一数据的异步同步,从而使多台机器拥有同一数据的多个副本,其中有一个主服务器(primary),用户处理客户端请求,还有多个备份服务器(secondary),用户保存主服务器的数据副本。并且当主库奔溃时在不需要用户干预的情况下自动切换其他备份服务器做主库。而且还可以利用副本服务器做只读服务器,实现读写分离,提高负载。同时需要三台服务器或者数据库就可以满足。以上也是其的主要优势所在。

建立一个副本集需要步骤,1、需要启动每个节点,2、然后进行初始化设置。在这里,我们将配置一组(三个节点,可以使用两个数据节点一个仲裁节点),一般三个节点也是标准设置。一旦mongod节点启动,我们将发出一个命令,以正确地初始化设置。几秒钟后,将选举产生一个主节点,你就可以开始写和查询集。

Mongodb(M)表示主节点,Mongodb(S)表示备节点,Mongodb(A)表示仲裁节点。主备节点存储数据,仲裁节点不存储数据。客户端同时连接主节点与备节点,不连接仲裁节点。

具体搭建如下:

1.启动三个节点(两个数据节点,一个仲裁节点)

介绍一下涉及到的参数

--dbpath 数据文件路径

--logpath 日志文件路径

--port 端口号,默认是27017.我这里使用的也是这个端口号.

--replSet 复制集的名字,一个replicasets中的每个节点的这个参数都要用一个复制集名字

--replSet replcopy / 这个后面跟的是其他standard节点的ip和端口

--maxConns 最大连接数

--fork 后台运行

--logappend 日志文件循环使用,如果日志文件已满,那么新日志覆盖最久日志。

1>启动第一个standard节点(ip:192.168.191.151)

/u01/mongo/bin/mongod--dbpath=/u01/mongodb/data/ --logpath=/u01/mongodb/logs/node1.log--oplogSize400 --maxConns=2000 --replSet replcopy/192.168.191.151:27017 --fork

2>启动第二个standard节点 (ip:192.168.191.150)

/u01/mongo/bin/mongod--dbpath=/u01/mongodb/data/ --logpath=/u01/mongodb/logs/node2.log --oplogSize 400--maxConns=2000 --replSet replcopy/192.168.191.150:27017 --fork

3>启动arbiter节点,也就是仲裁节点 (ip:192.168.191.150)。注意!--replSet replcopy/后面写的是两个standard节点的ip和端口

/u01/mongo/bin/mongod--dbpath=/u01/mongodb/arbiter/ --port 20000 --logpath=/u01/mongodb/logs/arbiter.log--replSet replcopy/192.168.191.150:27017,192.168.191.151:27017 --fork

2.很关键的一步,配置完上面,下面开始初始化各个节点。在第一个启动的节点上,运行mongo

cfg = { _id: "replcopy", members:[ { _id:0,host:"192.168.191.150:27017"}, { _id:1, host:"192.168.191.151:27017"} ]}

rs.initiate(cfg)

3.加入arbiter(仲裁)节点

1.PRIMARY>rs.addArb("192.16.191.150:2000");

到了这里,这个集群已经配置完成,三个节点的角色已经分配完毕,怎么查看是否正常呢

1.rs.status()通过这个命令,可以查看各个节点的ip、角色已经是否正常

2.rs.stepDown() 这个命令可以强制primary和standard节点角色互换,从而验证是否能够实现failover功能,在主节点上运行。

注意:添加新节点前,一定要配置好防火墙,开放对应的IP及PORT。

添加普通数据节点

PRIMARY>rs.add("ip:port")

删除节点

PRIMARY>rs.remove("ip:port")

显示当前谁是primay

PRIMARY> rs.isMaster()

注mongodb复制为什么要三台机器以上

首先介绍一下在replicaset里分为三种节点类型:

1>primary 负责client的读写。

2 >secondary作为热备节点,应用Primary的oplog读取的操作日志,和primary保持一致,不提供读写操作!

secondary有两种类型:

1)normal secondary 随时和Primay保持同步,

2)delayed secondary 延时指定时间和primary保持同步,防止误操作.

3 >arbiter.它不负责任何读写,只作为一个仲裁者,负责primary down的时候剩余节点的选举操作

为什么我们要添加一个仲裁节点,具体说明如下:

如果创建两节点的Replica Sets,一主一备secondary,如果Secondary宕机,Primary会变成Secondary!这时候集群里没有Primary了!为什么会出现这样的情况呢。

这是和MongoDB的Primary选举策略有关的,如果情况不是Secondary宕机,而是网络断开,那么两个节点都会选取自己为Primary,因为他们能连接上的只有自己这一个节点。而这样的情况在网络恢复后就需要处理复杂的一致性问题。而且断开的时间越长,时间越复杂。所以MongoDB选择的策略是如果集群中只有自己一个节点,那么不选取自己为Primary。

所以正确的做法应该是添加两个以上的节点,或者添加arbiter,当然最好也最方便的做法是添加arbiter,aribiter节点只参与选举,几乎不会有压力,所以你可以在各种闲置机器上启动arbiter节点,这不仅会避免上面说到的无法选举Primary的情况,更会让选取更快速的进行。因为如果是三台数据节点,一个节点宕机,另外两个节点很可能会各自选举自己为Primary,从而导致很长时间才能得出选举结果。实际上集群选举主库上由优先级和数据的新鲜度这两个条件决定的。

db.getMongo().setSlaveOk() 在slave上可以使备库可读,从而实现负载的转移。

程序连接字符串形式:

mongodb://localhost,localhost:27018,localhost:27019

连接到三台服务器组成的Replica Sets,把所有写操作集中在主库,读操作分布在各丛库

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 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)

What is the use of net4.0 What is the use of net4.0 May 10, 2024 am 01:09 AM

.NET 4.0 is used to create a variety of applications and it provides application developers with rich features including: object-oriented programming, flexibility, powerful architecture, cloud computing integration, performance optimization, extensive libraries, security, Scalability, data access, and mobile development support.

Integration of Java functions and databases in serverless architecture Integration of Java functions and databases in serverless architecture Apr 28, 2024 am 08:57 AM

In a serverless architecture, Java functions can be integrated with the database to access and manipulate data in the database. Key steps include: creating Java functions, configuring environment variables, deploying functions, and testing functions. By following these steps, developers can build complex applications that seamlessly access data stored in databases.

How to configure MongoDB automatic expansion on Debian How to configure MongoDB automatic expansion on Debian Apr 02, 2025 am 07:36 AM

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

How to ensure high availability of MongoDB on Debian How to ensure high availability of MongoDB on Debian Apr 02, 2025 am 07:21 AM

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

Navicat's method to view MongoDB database password Navicat's method to view MongoDB database password Apr 08, 2025 pm 09:39 PM

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).

Major update of Pi Coin: Pi Bank is coming! Major update of Pi Coin: Pi Bank is coming! Mar 03, 2025 pm 06:18 PM

PiNetwork is about to launch PiBank, a revolutionary mobile banking platform! PiNetwork today released a major update on Elmahrosa (Face) PIMISRBank, referred to as PiBank, which perfectly integrates traditional banking services with PiNetwork cryptocurrency functions to realize the atomic exchange of fiat currencies and cryptocurrencies (supports the swap between fiat currencies such as the US dollar, euro, and Indonesian rupiah with cryptocurrencies such as PiCoin, USDT, and USDC). What is the charm of PiBank? Let's find out! PiBank's main functions: One-stop management of bank accounts and cryptocurrency assets. Support real-time transactions and adopt biospecies

The best software choice for building a mobile virtual live broadcast room (key skills in using key software to create a virtual live broadcast room) The best software choice for building a mobile virtual live broadcast room (key skills in using key software to create a virtual live broadcast room) Sep 02, 2024 pm 03:17 PM

With the rapid development of mobile Internet, mobile live broadcast has become a popular way of social entertainment. In order to improve the quality and experience of live broadcasts, many anchors have begun to look for solutions to build virtual live broadcast rooms. This article will focus on several software suitable for building a mobile virtual live broadcast room, and share some key skills to help anchors create a professional-level live broadcast space. 1. Choose suitable virtual live broadcast software. To build a mobile virtual live broadcast room, you first need to choose a suitable software. There are many excellent options on the market, such as OBS Studio, XSplit Gamecaster, etc., which provide rich functions and user-friendly interfaces to facilitate live broadcasts and interactions. Design a personalized live broadcast interface A personalized live broadcast

What is the CentOS MongoDB backup strategy? What is the CentOS MongoDB backup strategy? Apr 14, 2025 pm 04:51 PM

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.

See all articles