当前位置:Gxlcms > 数据库问题 > 搭建高可用MongoDB集群(二): 副本集

搭建高可用MongoDB集群(二): 副本集

时间:2021-07-01 10:21:17 帮助过:3人阅读

mongoDB官方已经不建议使用主从模式了,替代方案是采用副本集的模式,点击查看 ,如图:

技术分享

 

那什么是副本集呢?打魔兽世界总说打副本,其实这两个概念差不多一个意思。游戏里的副本是指玩家集中在高峰时间去一个场景打怪,会出现玩家暴多怪物少的情况,游戏开发商为了保证玩家的体验度,就为每一批玩家单独开放一个同样的空间同样的数量的怪物,这一个复制的场景就是一个副本,不管有多少个玩家各自在各自的副本里玩不会互相影响。 mongoDB的副本也是这个,主从模式其实就是一个单副本的应用,没有很好的扩展性和容错性。而副本集具有多个副本保证了容错性,就算一个副本挂掉了还有很多副本存在,并且解决了上面第一个问题“主节点挂掉了,整个集群内会自动切换”。难怪mongoDB官方推荐使用这种模式。我们来看看mongoDB副本集的架构图:

技术分享

 

由图可以看到客户端连接到整个副本集,不关心具体哪一台机器是否挂掉。主服务器负责整个副本集的读写,副本集定期同步数据备份,一但主节点挂掉,副本节点就会选举一个新的主服务器,这一切对于应用服务器不需要关心。我们看一下主服务器挂掉后的架构:

技术分享

副本集中的副本节点在主节点挂掉后通过心跳机制检测到后,就会在集群内发起主节点的选举机制,自动选举一位新的主服务器。看起来很牛X的样子,我们赶紧操作部署一下!
官方推荐的副本集机器数量为至少3个,那我们也按照这个数量配置测试。

1、准备两台机器 192.168.1.136、192.168.1.137、192.168.1.138。 192.168.1.136 当作副本集主节点,192.168.1.137、192.168.1.138作为副本集副本节点

2、分别在每台机器上建立mongodb副本集测试文件夹

   1  #存放整个mongodb文件

   2   mkdir -p /data/mongodbtest/replset
 
   3  #存放mongodb数据文件
   4  mkdir -p /data/mongodbtest/replset/data
 
   5  #进入mongodb文件夹
   6  cd  /data/mongodbtest

3、下载mongodb的安装程序
1 wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.4.8.tgz

注意linux生产环境不能安装32位的mongodb,因为32位受限于操作系统最大2G的文件限制。

技术分享

 

MySQL <textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="readonly" style="border: none; outline: none; font-stretch: normal; display: block; -webkit-appearance: none; padding-top: 0px; padding-right: 5px; padding-left: 5px; width: 608px; height: 58.5px; resize: none; overflow: auto; margin: 0px; position: absolute; opacity: 0; box-shadow: none; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; -webkit-box-shadow: none; white-space: pre; word-wrap: normal; color: rgb(0, 0, 0); tab-size: 4; z-index: 0; line-height: 15px !important; font-family: Monaco, MonacoRegular, 'Courier New', monospace !important; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"></textarea>
12#解压下载的压缩包tarxvzfmongodb-linux-x86_64-2.4.8.tgz

4、分别在每台机器上启动mongodb

MySQL<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="readonly" style="border: none; outline: none; font-stretch: normal; display: block; -webkit-appearance: none; padding-top: 0px; padding-right: 5px; padding-left: 5px; width: 608px; height: 49.5px; resize: none; overflow: auto; margin: 0px; position: absolute; opacity: 0; box-shadow: none; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; -webkit-box-shadow: none; white-space: pre; word-wrap: normal; color: rgb(0, 0, 0); tab-size: 4; z-index: 0; line-height: 15px !important; font-family: Monaco, MonacoRegular, 'Courier New', monospace !important; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"></textarea>
1 /data/mongodbtest/mongodb-linux-x86_64-2.4.8/bin/mongod  --dbpath /data/mongodbtest/replset/data   --replSet repset

可以看到控制台上显示副本集还没有配置初始化信息。

MySQL <textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="readonly" style="border: none; outline: none; font-stretch: normal; display: block; -webkit-appearance: none; padding-top: 0px; padding-right: 5px; padding-left: 5px; width: 608px; height: 64.5px; resize: none; overflow: auto; margin: 0px; position: absolute; opacity: 0; box-shadow: none; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; -webkit-box-shadow: none; white-space: pre; word-wrap: normal; color: rgb(0, 0, 0); tab-size: 4; z-index: 0; line-height: 15px !important; font-family: Monaco, MonacoRegular, 'Courier New', monospace !important; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"></textarea>
12SunDec2920:12:02.953[rsStart]replSetcan‘tgetlocal.system.replsetconfigfromselforanyseed(EMPTYCONFIG)SunDec2920:12:02.953[rsStart]replSetinfoyoumayneedtorun  replSetInitiate-- rs.initiate() in the shell -- if that is not already done

5、初始化副本集

在三台机器上任意一台机器登陆mongodb

MySQL<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="readonly" style="border: none; outline: none; font-stretch: normal; display: block; -webkit-appearance: none; padding-top: 0px; padding-right: 5px; padding-left: 5px; width: 608px; height: 88.5px; resize: none; overflow: auto; margin: 0px; position: absolute; opacity: 0; box-shadow: none; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; -webkit-box-shadow: none; white-space: pre; word-wrap: normal; color: rgb(0, 0, 0); tab-size: 4; z-index: 0; line-height: 15px !important; font-family: Monaco, MonacoRegular, 'Courier New', monospace !important; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"></textarea>
1 2 3 4 /data/mongodbtest/mongodb-linux-x86_64-2.4.8/bin/mongo   #使用admin数据库 use admin

#定义副本集配置变量,这里的 _id:”repset” 和上面命令参数“ –replSet repset” 要保持一样。

MySQL <textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="readonly" style="border: none; outline: none; font-stretch: normal; display: block; -webkit-appearance: none; padding-top: 0px; padding-right: 5px; padding-left: 5px; width: 608px; height: 103.5px; resize: none; overflow: auto; margin: 0px; position: absolute; opacity: 0; box-shadow: none; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; -webkit-box-shadow: none; white-space: pre; word-wrap: normal; color: rgb(0, 0, 0); tab-size: 4; z-index: 0; line-height: 15px !important; font-family: Monaco, MonacoRegular, 'Courier New', monospace !important; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"></textarea>
12345config={_id:"repset",members:[...{_id:0,host:"192.168.1.136:27017"},...{_id:1,host:"192.168.1.137:27017"},...{_id:2,host:"192.168.1.138:27017"}]...}

#输出

MySQL<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="readonly" style="border: none; outline: none; font-stretch: normal; display: block; -webkit-appearance: none; padding-top: 0px; padding-right: 5px; padding-left: 5px; width: 608px; height: 283.5px; resize: none; overflow: auto; margin: 0px; position: absolute; opacity: 0; box-shadow: none; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; -webkit-box-shadow: none; white-space: pre; word-wrap: normal; color: rgb(0, 0, 0); tab-size: 4; z-index: 0; line-height: 15px !important; font-family: Monaco, MonacoRegular, 'Courier New', monospace !important; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"></textarea>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 {         "_id" : "repset",         "members" : [                 {                         "_id" : 0,                         "host" : "192.168.1.136:27017"                 },                 {                         "_id" : 1,                         "host" : "192.168.1.137:27017"                 },                 {                         "_id" : 2,                         "host" : "192.168.1.138:27017"                 }         ] }

MySQL <textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="readonly" style="border: none; outline: none; font-stretch: normal; display: block; -webkit-appearance: none; padding-top: 0px; padding-right: 5px; padding-left: 5px; width: 608px; height: 58.5px; resize: none; overflow: auto; margin: 0px; position: absolute; opacity: 0; box-shadow: none; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; -webkit-box-shadow: none; white-space: pre; word-wrap: normal; color: rgb(0, 0, 0); tab-size: 4; z-index: 0; line-height: 15px !important; font-family: Monaco, MonacoRegular, 'Courier New', monospace !important; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"></textarea>
12#初始化副本集配置rs.initiate(config);

#输出成功

MySQL<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="readonly" style="border: none; outline: none; font-stretch: normal; display: block; -webkit-appearance: none; padding-top: 0px; padding-right: 5px; padding-left: 5px; width: 608px; height: 94.5px; resize: none; overflow: auto; margin: 0px; position: absolute; opacity: 0; box-shadow: none; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; -webkit-box-shadow: none; white-space: pre; word-wrap: normal; color: rgb(0, 0, 0); tab-size: 4; z-index: 0; line-height: 15px !important; font-family: Monaco, MonacoRegular, 'Courier New', monospace !important; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"></textarea>

人气教程排行