Home Database Mysql Tutorial MongoDB 副本集搭建实现读写分享

MongoDB 副本集搭建实现读写分享

Jun 07, 2016 pm 05:29 PM
m mongodb

副本集就是有自动故障恢复功能的主从集群。主从集群和副本集最为明显的区别就是副本集没有固定的主节点。可以把副本集当做一个集

副本集就是有自动故障恢复功能的主从集群。主从集群和副本集最为明显的区别就是副本集没有固定的主节点。

可以把副本集当做一个集群,整个集群会选出一个主节点,当其不能正常工作时则会激活其它节点。

系统:

CentOS 5.6 x86_64

mongo版本:mongodb-linux-x86_64-2.0.6.tgz

Server1 10.0.1.121:20001

server2 10.0.1.212:20001

Arbiter 10.0.1.212:20002

 


目录规划:

数据目录和日志单分出来

Server1:

/mongodb

/mlogs

应用程序日志统计放到/usr/local/mongo

Server2:

/mongodb

/mlogs

/arbiter

应用程序日志统计放到/usr/local/mongo

 


部署:

1.防火墙开启20001端口,关闭selinux

 


2.分别在两台机器上安装mongo服务

tar xf mongodb-linux-x86_64-2.0.6.tgz -C /usr/local/mongo

3.创建key文件

Server1:

mkdir /etc/keys

echo "test123456" > r0

Server2:

mkdir /etc/keys

echo "test123456" > r1

Arbiter:

echo "test123456" > r2

chmod 600 r0 r1 r2

4.分别启动Server1和Server2的mongo服务

Server1:

/usr/local/mongo/bin/mongod --replSet rs1 --keyFile /usr/local/mongo/key/r0 --port 20001 --fork --dbpath=/mongodb/ --logpath /mlogs/Server1.log

--fork为后台启动

netstat -tunlp | grep mongo  查看端口是否被开启

加入到开机启动脚本 /etc/rc.local

Server2:

/usr/local/mongo/bin/mongod --replSet rs1 --keyFile /usr/local/mongo/key/r1 --port 20001 --fork --dbpath=/mongodb/ --logpath /mlogs/Server2.log

netstat -tunlp | grep mongo  查看端口是否被开启

 


/usr/local/mongo/bin/mongod --replSet rs1 --keyFile /usr/local/mongo/key/r2 --port 20002 --fork --dbpath=/arbiter --logpath /mlogs/arbiter.log

 


加入到开机启动脚本 /etc/rc.local使机器重启后继续生效

 


选项:

--only

在从节点上指定复制某个数据库(默认复制所有数据库)

--slavedelay

用在从节点上,当应用主节点的操作时增加延时(秒)

--fastsync

以主节点的数据快照为基础启动从节点,如果数据目录一开始是主节点的数据快照,从节点用这个选项启动要比完整同步快多了

--autoresync

如果从节点与主节点不同步,则自动重新同步

--oplogSize

主节点oplog的大小(MB)

5.配置及初始化Replica Sets

 > config_pongo={

..._id:'pongo',members:[

...{_id:0,host:'10.0.1.121:20001',priority:1},    #如果有多个节点继续往下增加

...{_id:1,host:'10.0.1.212:20001',priority:2},

...{_id:2,host:"10.0.1.212:20002",arbiterOnly : true}]

...}

rs.initiate(config_rs1); 初始化Replica Sets

 


注:关于扩展节点和添加仲裁设备请看最下方、

副本集中节点的类型

standard:常规节点,它存储一份完整的数据副本,参与选举投票可以成为活跃节点。

passive:存储了完整的数据副本,参与投票,,不能成为活跃节点。

arbiter:仲裁者只参与投票,不能接受复制数据,也不能成为活跃节点。

每个参与节点(非仲裁者)都有个优先权,优先权为0是被动的,不能成为活跃节点,优先值不为0,则按照大小选出活跃节点。

如果2个值一样,则数据最新的为活跃节点。在节点配置中修改priority键,来配置成标准节点或被动节点。

 


注:如果配置成功master会变成PRIMARY  slave会变成SECONDARY

 


6.使用rs.status()查看副本集的状态信息

 


"health" : 1,                        --1表明正常; 0表明异常、

"state" : 1,                          -- 1表明是Primary; 2 表明是Secondary;

"stateStr" : "PRIMARY",      --表明此机器是主库

linux

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.

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

MongoDB and relational database: a comprehensive comparison MongoDB and relational database: a comprehensive comparison Apr 08, 2025 pm 06:30 PM

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

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.

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

How to encrypt data in Debian MongoDB How to encrypt data in Debian MongoDB Apr 12, 2025 pm 08:03 PM

Encrypting MongoDB database on a Debian system requires following the following steps: Step 1: Install MongoDB First, make sure your Debian system has MongoDB installed. If not, please refer to the official MongoDB document for installation: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-debian/Step 2: Generate the encryption key file Create a file containing the encryption key and set the correct permissions: ddif=/dev/urandomof=/etc/mongodb-keyfilebs=512

See all articles