热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

MongoDB高可用架构:深入解析ReplicaSet机制

MongoDB的高可用架构主要依赖于其ReplicaSet机制。ReplicaSet通过多个mongod节点的协同工作,实现了数据的冗余存储和故障自动切换,确保了系统的高可用性和数据的一致性。本文将深入解析ReplicaSet的工作原理及其在实际应用中的配置和优化方法,帮助读者更好地理解和实施MongoDB的高可用架构。

为什么80%的码农都做不了架构师?>>>   hot3.png

mongodb高可用性架构---Replica Set Replica Set使用的是n个mongod节点,构建具备自动的容错功能(auto-failover),自动恢复的(auto-recovery)的高可用方案。 使用Replica Set来实现读写分离。通过在连接时指定或者在主库指定slaveOk,由Secondary来分担读的压力,Primary只承担写操作。 对于Replica Set中的secondary 节点默认是不可读的。 环境如下: 192.168.198.131 192.168.198.129 192.168.198.132 分别在三台服务器上安装mongod服务,安装如下: # wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.0.3.tgz # tar zxvf mongodb-linux-x86_64-2.0.3.tgz -C ../software/ # ln -s mongodb-linux-x86_64-2.0.3 /usr/local/mongodb # useradd mongodb # mkdir -p /data/mongodb/myset # cd /usr/local/mongodb/bin # ./mongod --replSet myset --dbpath /data/mongodb/myset --oplogSize 100 --logpath /data/mongodb/myset/myset.log --logappend --fork # ./mongo //任选一台执行以下内容 > config={_id:"myset",members:[ ... {_id:0,host:"192.168.198.131:27017"}, ... {_id:1,host:"192.168.198.129:27017"}, ... {_id:2,host:"192.168.198.132:27017",arbiterOnly:true}]} 以下输出内容: { "_id" : "myset", "members" : [ { "_id" : 0, "host" : "192.168.198.131:27017" }, { "_id" : 1, "host" : "192.168.198.129:27017" }, { "_id" : 2, "host" : "192.168.198.132:27017", "arbiterOnly" : true } ] } > rs.initiate(config) //初始化 以下输出内容: { "info" : "Config now saved locally. Should come online in about a minute.", "ok" : 1 } > rs.conf() //查看配置内容 { "_id" : "myset", "version" : 1, "members" : [ { "_id" : 0, "host" : "192.168.198.131:27017" }, { "_id" : 1, "host" : "192.168.198.129:27017" }, { "_id" : 2, "host" : "192.168.198.132:27017", "arbiterOnly" : true } ] } > rs.status() //查看状态信息 { "set" : "myset", "date" : ISODate("2012-03-01T08:45:01Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "192.168.198.131:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "optime" : { "t" : 1330591378000, "i" : 1 }, "optimeDate" : ISODate("2012-03-01T08:42:58Z"), "self" : true }, { "_id" : 1, "name" : "192.168.198.129:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 121, "optime" : { "t" : 1330591378000, "i" : 1 }, "optimeDate" : ISODate("2012-03-01T08:42:58Z"), "lastHeartbeat" : ISODate("2012-03-01T08:45:01Z"), "pingMs" : 0 }, { "_id" : 2, "name" : "192.168.198.132:27017", "health" : 1, "state" : 7, "stateStr" : "ARBITER", "uptime" : 121, "optime" : { "t" : 0, "i" : 0 }, "optimeDate" : ISODate("1970-01-01T00:00:00Z"), "lastHeartbeat" : ISODate("2012-03-01T08:45:01Z"), "pingMs" : 1 } ], "ok" : 1 } state: 1表示当前可以进行读写,2表示不能读写 health: 1表示是正常的,0异常 在同一时刻,每组 Replica Sets 只有一个 Primary,用于接受写操作。而后会异步复制到其他成员数据库中。一旦 primary 死掉,会自动投票选出接任的 primary 来,原服务器恢复后成为普通成员。如果数据尚未从先前的 primary 复制到成员服务器,有可能会丢失数据。 PRIMARY> db.test.insert({"name":"foobar","age":25}) PRIMARY> db.test.find() { "_id" : ObjectId("4f4f38fc47db2bfa5ceb2aee"), "name" : "foobar", "age" : 25 } SECONDARY> db.test.find() error: { "$err" : "not master and slaveok=false", "code" : 13435 } SECONDARY> db.test.insert({"name":"foobar","age":25}) not master 在主库上设置slaveok=ok PRIMARY> db.getMongo().setSlaveOk() SECONDARY> use test switched to db test SECONDARY> db.test.find() { "_id" : ObjectId("4f4f38fc47db2bfa5ceb2aee"), "name" : "foobar", "age" : 25 } 192.168.198.131上pkill mongod Thu Mar 1 17:17:51 got kill or ctrl c or hup signal 15 (Terminated), will terminate after current cmd ends Thu Mar 1 17:17:51 [interruptThread] now exiting Thu Mar 1 17:17:51 dbexit: Thu Mar 1 17:17:51 [interruptThread] shutdown: going to close listening sockets... Thu Mar 1 17:17:51 [interruptThread] closing listening socket: 7 Thu Mar 1 17:17:51 [interruptThread] closing listening socket: 8 Thu Mar 1 17:17:51 [interruptThread] closing listening socket: 9 Thu Mar 1 17:17:51 [interruptThread] removing socket file: /tmp/mongodb-27017.sock Thu Mar 1 17:17:51 [interruptThread] shutdown: going to flush diaglog... Thu Mar 1 17:17:51 [interruptThread] shutdown: going to close sockets... Thu Mar 1 17:17:51 [conn1] end connection 127.0.0.1:58614 Thu Mar 1 17:17:51 [interruptThread] shutdown: waiting for fs preallocator... Thu Mar 1 17:17:51 [interruptThread] shutdown: lock for final commit... Thu Mar 1 17:17:51 [interruptThread] shutdown: final commit... Thu Mar 1 17:17:52 [interruptThread] shutdown: closing all files... Thu Mar 1 17:17:52 [interruptThread] closeAllFiles() finished Thu Mar 1 17:17:52 [interruptThread] journalCleanup... Thu Mar 1 17:17:52 [interruptThread] removeJournalFiles Thu Mar 1 17:17:52 [interruptThread] shutdown: removing fs lock... Thu Mar 1 17:17:52 dbexit: really exiting now 192.168.198.129选择为primary Thu Mar 1 00:17:51 [conn144] end connection 192.168.198.131:35714 Thu Mar 1 00:17:51 [rsSync] replSet syncThread: 10278 dbclient error communicating with server: 192.168.198.131:27017 Thu Mar 1 00:17:52 [rsHealthPoll] DBClientCursor::init call() failed Thu Mar 1 00:17:52 [rsHealthPoll] replSet info 192.168.198.131:27017 is down (or slow to respond): DBClientBase::findN: transport error: 192.168.198.131:27017 query: { replSetHeartbeat: "myset", v: 1, pv: 1, checkEmpty: false, from: "192.168.198.129:27017" } Thu Mar 1 00:17:52 [rsHealthPoll] replSet member 192.168.198.131:27017 is now in state DOWN Thu Mar 1 00:17:52 [rsMgr] not electing self, 192.168.198.132:27017 would veto Thu Mar 1 00:17:58 [rsMgr] replSet info electSelf 1 Thu Mar 1 00:17:58 [rsMgr] replSet PRIMARY 【ARBITER】192.168.198.132日志 Thu Mar 1 04:17:51 [conn143] end connection 192.168.198.131:56260 Thu Mar 1 04:17:53 [rsHealthPoll] DBClientCursor::init call() failed Thu Mar 1 04:17:53 [rsHealthPoll] replSet info 192.168.198.131:27017 is down (or slow to respond): DBClientBase::findN: transport error: 192.168.198.131:27017 query: { replSetHeartbeat: "myset", v: 1, pv: 1, checkEmpty: false, from: "192.168.198.132:27017" } Thu Mar 1 04:17:53 [rsHealthPoll] replSet member 192.168.198.131:27017 is now in state DOWN Thu Mar 1 04:17:58 [conn144] replSet info voting yea for 192.168.198.129:27017 (1) Thu Mar 1 04:17:59 [rsHealthPoll] replSet member 192.168.198.129:27017 is now in state PRIMARY Thu Mar 1 04:18:05 [rsHealthPoll] couldn't connect to 192.168.198.131:27017: couldn't connect to server 192.168.198.131:27017 PRIMARY> rs.status(); { "set" : "myset", "date" : ISODate("2012-03-01T09:20:37Z"), "myState" : 1, "syncingTo" : "192.168.198.131:27017", "members" : [ { "_id" : 0, "name" : "192.168.198.131:27017", "health" : 0, "state" : 8, "stateStr" : "(not reachable/healthy)", "uptime" : 0, "optime" : { "t" : 1330591997000, "i" : 1 }, "optimeDate" : ISODate("2012-03-01T08:53:17Z"), "lastHeartbeat" : ISODate("2012-03-01T09:17:50Z"), "pingMs" : 0, "errmsg" : "socket exception" }, { "_id" : 1, "name" : "192.168.198.129:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "optime" : { "t" : 1330591997000, "i" : 1 }, "optimeDate" : ISODate("2012-03-01T08:53:17Z"), "self" : true }, { "_id" : 2, "name" : "192.168.198.132:27017", "health" : 1, "state" : 7, "stateStr" : "ARBITER", "uptime" : 2244, "optime" : { "t" : 0, "i" : 0 }, "optimeDate" : ISODate("1970-01-01T00:00:00Z"), "lastHeartbeat" : ISODate("2012-03-01T09:20:36Z"), "pingMs" : 0 } ], "ok" : 1 } PRIMARY> db.test.find() { "_id" : ObjectId("4f4f38fc47db2bfa5ceb2aee"), "name" : "foobar", "age" : 25 } { "_id" : ObjectId("4f4f3fe2a7c9a9d1eb78392f"), "name" : "ttlsa", "age" : 1 } 再次启动192.168.198.131的mongod服务 Thu Mar 1 17:23:24 [initandlisten] MongoDB starting : pid=6977 port=27017 dbpath=/data/mongodb/myset 64-bit host=node2 Thu Mar 1 17:23:24 [initandlisten] db version v2.0.3, pdfile version 4.5 Thu Mar 1 17:23:24 [initandlisten] git version: 05bb8aa793660af8fce7e36b510ad48c27439697 Thu Mar 1 17:23:24 [initandlisten] build info: Linux ip-10-110-9-236 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64 BOOST_LIB_VERSION=1_41 Thu Mar 1 17:23:24 [initandlisten] options: { dbpath: "/data/mongodb/myset", fork: true, logappend: true, logpath: "/data/mongodb/myset/myset.log", oplogSize: 100, replSet: "myset" } Thu Mar 1 17:23:24 [initandlisten] journal dir=/data/mongodb/myset/journal Thu Mar 1 17:23:24 [initandlisten] recover : no journal files present, no recovery needed Thu Mar 1 17:23:26 [initandlisten] waiting for connections on port 27017 Thu Mar 1 17:23:26 [websvr] admin web console waiting for connections on port 28017 Thu Mar 1 17:23:27 [initandlisten] connection accepted from 192.168.198.129:43753 #1 Thu Mar 1 17:23:27 [initandlisten] connection accepted from 127.0.0.1:37253 #2 Thu Mar 1 17:23:27 [rsStart] trying to contact 192.168.198.129:27017 Thu Mar 1 17:23:27 [rsStart] replSet STARTUP2 Thu Mar 1 17:23:27 [rsSync] replSet SECONDARY SECONDARY> use test switched to db test SECONDARY> db.test.find() { "_id" : ObjectId("4f4f38fc47db2bfa5ceb2aee"), "name" : "foobar", "age" : 25 } { "_id" : ObjectId("4f4f3fe2a7c9a9d1eb78392f"), "name" : "ttlsa", "age" : 1 }

转载于:https://my.oschina.net/766/blog/211493


推荐阅读
  • 本文详细介绍了如何在循环双链表的指定位置插入新元素的方法,包括必要的步骤和代码示例。 ... [详细]
  • 在1995年,Simon Plouffe 发现了一种特殊的求和方法来表示某些常数。两年后,Bailey 和 Borwein 在他们的论文中发表了这一发现,这种方法被命名为 Bailey-Borwein-Plouffe (BBP) 公式。该问题要求计算圆周率 π 的第 n 个十六进制数字。 ... [详细]
  • 本文介绍了SIP(Session Initiation Protocol,会话发起协议)的基本概念、功能、消息格式及其实现机制。SIP是一种在IP网络上用于建立、管理和终止多媒体通信会话的应用层协议。 ... [详细]
  • 二维码的实现与应用
    本文介绍了二维码的基本概念、分类及其优缺点,并详细描述了如何使用Java编程语言结合第三方库(如ZXing和qrcode.jar)来实现二维码的生成与解析。 ... [详细]
  • 洛谷 P4009 汽车加油行驶问题 解析
    探讨了经典算法题目——汽车加油行驶问题,通过网络流和费用流的视角,深入解析了该问题的解决方案。本文将详细阐述如何利用最短路径算法解决这一问题,并提供详细的代码实现。 ... [详细]
  • CentOS下ProFTPD的安装与配置指南
    本文详细介绍在CentOS操作系统上安装和配置ProFTPD服务的方法,包括基本配置、安全设置及高级功能的启用。 ... [详细]
  • 回顾两年前春节期间的一个个人项目,该项目原本计划参加竞赛,但最终作为练习项目完成。独自完成了从编码到UI设计的全部工作,尽管代码量不大,但仍有一定的参考价值。本文将详细介绍该项目的背景、功能及技术实现。 ... [详细]
  • Jupyter Notebook多语言环境搭建指南
    本文详细介绍了如何在Linux环境下为Jupyter Notebook配置Python、Python3、R及Go四种编程语言的环境,包括必要的软件安装和配置步骤。 ... [详细]
  • 本题要求计算一组正整数的最小公倍数(LCM)。输入包括多组测试数据,每组数据首先给出一个正整数n,随后是n个正整数。 ... [详细]
  • Maven + Spring + MyBatis + MySQL 环境搭建与实例解析
    本文详细介绍如何使用MySQL数据库进行环境搭建,包括创建数据库表并插入示例数据。随后,逐步指导如何配置Maven项目,整合Spring框架与MyBatis,实现高效的数据访问。 ... [详细]
  • 问题描述现在,不管开发一个多大的系统(至少我现在的部门是这样的),都会带一个日志功能;在实际开发过程中 ... [详细]
  • 本问题涉及在给定的无向图中寻找一个至少包含三个节点的环,该环上的节点不重复,并且环上所有边的长度之和最小。目标是找到并输出这个最小环的具体方案。 ... [详细]
  • 深入解析WebP图片格式及其应用
    随着互联网技术的发展,无论是PC端还是移动端,图片数据流量占据了很大比重。尤其在高分辨率屏幕普及的背景下,如何在保证图片质量的同时减少文件大小,成为了亟待解决的问题。本文将详细介绍Google推出的WebP图片格式,探讨其在实际项目中的应用及优化策略。 ... [详细]
  • 本文档介绍了如何使用ESP32开发板在STA模式下实现与TCP服务器的通信,包括环境搭建、代码解析及实验步骤。 ... [详细]
  • 本文探讨了如何高效地计算数组中和为2的幂的偶对数量,提供了从基础到优化的方法。 ... [详细]
author-avatar
古零零碎碎
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有