search
HomeDatabaseMysql TutorialMongoDB 副本集搭建实现读写分享

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

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

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

系统:

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
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 String Types: Storage, Performance, and Best PracticesMySQL String Types: Storage, Performance, and Best PracticesMay 10, 2025 am 12:02 AM

MySQLstringtypesimpactstorageandperformanceasfollows:1)CHARisfixed-length,alwaysusingthesamestoragespace,whichcanbefasterbutlessspace-efficient.2)VARCHARisvariable-length,morespace-efficientbutpotentiallyslower.3)TEXTisforlargetext,storedoutsiderows,

Understanding MySQL String Types: VARCHAR, TEXT, CHAR, and MoreUnderstanding MySQL String Types: VARCHAR, TEXT, CHAR, and MoreMay 10, 2025 am 12:02 AM

MySQLstringtypesincludeVARCHAR,TEXT,CHAR,ENUM,andSET.1)VARCHARisversatileforvariable-lengthstringsuptoaspecifiedlimit.2)TEXTisidealforlargetextstoragewithoutadefinedlength.3)CHARisfixed-length,suitableforconsistentdatalikecodes.4)ENUMenforcesdatainte

What are the String Data Types in MySQL?What are the String Data Types in MySQL?May 10, 2025 am 12:01 AM

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,2)VARCHARforvariable-lengthtext,3)BINARYandVARBINARYforbinarydata,4)BLOBandTEXTforlargedata,and5)ENUMandSETforcontrolledinput.Eachtypehasspecificusesandperformancecharacteristics,sochoose

How to Grant Permissions to New MySQL UsersHow to Grant Permissions to New MySQL UsersMay 09, 2025 am 12:16 AM

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

How to Add Users in MySQL: A Step-by-Step GuideHow to Add Users in MySQL: A Step-by-Step GuideMay 09, 2025 am 12:14 AM

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

MySQL: Adding a new user with complex permissionsMySQL: Adding a new user with complex permissionsMay 09, 2025 am 12:09 AM

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

MySQL: String Data Types and CollationsMySQL: String Data Types and CollationsMay 09, 2025 am 12:08 AM

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

MySQL: What length should I use for VARCHARs?MySQL: What length should I use for VARCHARs?May 09, 2025 am 12:06 AM

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.

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

Video Face Swap

Video Face Swap

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

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor