MongoDB Replica Set使用经验分享理论篇
MongoDB Replica Set是MongoDB官方推荐的主从复制和高可用方案,用于替代原有的Master-Slave主从复制方案。Replicat Set具有自动
MongoDB Replica Set是MongoDB官方推荐的主从复制和高可用方案,用于替代原有的Master-Slave主从复制方案。Replicat Set具有自动切换功能,当Primary挂掉之后,可以自动由Replica Set中的某一个Secondary来切换到Primary,以实现高可用的目的,不像MySQL那样需要使用第三方软件。
目前很多游戏公司都开始使用MongoDB作为数据库,我们公司线上使用的版本是2.4.6.
一 MongoDB Replica Set的原理
复制主要用于备份、灾难恢复和读写分离。一个Replica Set就是一组mongod实例。Replica Set中的Primary接收所有的写操作,Secondaries从Primary复制操作然后应用到自己的data set。
一个Replica Set中的成员角色有三种:Primary,Secondary和Arbiter。
Primary 接收来自客户端的所有的写操作,一个Replica Set中有且只有一个Primary。Primary如果宕掉,Replica Set会自动选举一个Secondary成为Primary。Primary将它data sets的所有操作都记录到oplog中。
Secondary Secondary从Primary复制oplog,然后将oplog中的操作应用到自己的data sets。Secondary和Primary之间是异步复制,也就是Secondary中的数据可能不是最新的。默认情况下,Secondary不可读不可写,但是可以通过设置运行客户端从Secondary读。
Arbiter Arbiter不需要维护自己的data sets,,只是当Primary挂掉之后参与投票选择哪个Secondary可以升级为Primary。当Replica Set中的成员个数为偶数个时,就需要添加一个Arbiter用于投票选举哪个可以升级为Primary。Arbiter对硬件的要求很低。不能在Primary或者Secondary主机上运行Arbiter。
一个Replica Set可以最多拥有12个成员,但是只有7个成员可以同时参与投票选举成为Primary,如果成员数量超过12,就需要使用Master-Slave主从复制方式。
部署一个Replica Set至少需要三个成员,一个Arbiter,一个Secondary和一个Primary或者一个Primary,两个Secondary。
可以将Secondary配置为以下几种特殊用途:
A.在选举中阻止其成为Primary,只用作备份数据。通过设置优先级priority为0来实现。
B.阻止应用程序从它读,通过设置优先级priority为0和设置hidden为true来实现。
一个隐藏的成员同样复制Primary的数据,但是对于客户端应用程序来讲,它不可见。
C.保留历史镜像数据用于数据回档,比如如果误删除数据,可以使用Delayed Replica Set成员中的数据恢复。
Delayed members即延时成员会延时从Primary复制oplog
二 MongoDB Replica Set部署架构
Replica Set Elections 复制集选举
Replica Set通过投票选举的方式来决定哪个成员可以升级为Primary。初始化一个Replica Set后就会产生选举出现,或者任何时候当Primary不可用时也会有选举出现。需要注意的是,投票选举Primary会花费一定的时间来完成,在这段时间内,整个Replica Set无法进行写操作,所以尽量避免重新投票选举的情况出现。
影响选举的因素和条件有:
Heartbeats 心跳检测
Replica Set中成员每2秒向其他成员发送心跳,如果在10秒内无回应,这个成员就被其他成员标记为不可用。
Priortiy Comparisons 对比优先级
设定成员的优先级priority会影响投票选举,优先级越高,越容易被选举成为Primary。如果优先级设置为0,那么这个成员不会永远不会被选举成为Primary,它也不需要其他成员为它投票。
Optime
Optime是Replica Set的成员上一次从oplog中将操作应用到data sets的时间戳。一个成员不会成为Primary,除非它比其他可见的成员拥有最新的时间戳。
Connections
一个Replica Set中的成员如果想要成为Primary,它必须能够连接这个Replica Set中具有投票权利的大部分成员。
Network Partitions
为了避免当Primary宕掉后,整个Replica Set无法选举出新的Primary,Replica Set变成只读,需要在一个数据中心放置大部分Replica Set中的成员实例。
参考文档
CentOS 编译安装 MongoDB与mongoDB的php扩展
CentOS 6 使用 yum 安装MongoDB及服务器端配置
Ubuntu 13.04下安装MongoDB2.4.3
MongoDB入门必读(概念与实战并重)
Ubunu 14.04下MongoDB的安装指南
《MongoDB 权威指南》(MongoDB: The Definitive Guide)英文文字版[PDF]
Nagios监控MongoDB分片集群服务实战
基于CentOS 6.5操作系统搭建MongoDB服务
MongoDB 的详细介绍:请点这里
MongoDB 的下载地址:请点这里
本文永久更新链接地址:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values and pointers to data rows, and is suitable for non-primary key column queries.

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.
