欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MongoDB的集群模式--Replica Set

程序员文章站 2022-06-15 08:33:15
一、Replica Set 集群分为两种架构: 奇数个节点构成Replica Set,所有节点拥有数据集。最小架构: 1个Primary节点,2个Secondary节点 偶数个节点 + 一个仲裁节点 构成的Replica Set,节点拥有数据集,仲裁节点仅参与仲裁选举出Primary节点。 最小架构 ......

一、replica set 集群分为两种架构:

  • 奇数个节点构成replica set,所有节点拥有数据集。最小架构: 1个primary节点,2个secondary节点

MongoDB的集群模式--Replica Set

  • 偶数个节点 + 一个仲裁节点 构成的replica set,节点拥有数据集,仲裁节点仅参与仲裁选举出primary节点。 最小架构:1个primary节点,1个secondary节点,1个arbiter节点
  •  MongoDB的集群模式--Replica Set

接下来就以3台服务器为例,部署具有仲裁的框架。

ip地址 操作系统版本 mongodb版本 端口 功能
10.10.18.10 centos7.5  4.0  27017  primary 
10.10.18.11 centos7.5  4.0  27017  secondary
10.10.18.12 centos7.5  4.0  27017  arbiter

二、安装部署(请访问 "mongodb安装")

primary的配置文件:

systemlog:
   destination: file
   path: "/data/mongodb/log/mongod.log"
   logappend: true
storage:
   dbpath: "/data/mongodb/data"
   journal:
      enabled: true
   wiredtiger:
      engineconfig:
        cachesizegb: 2
processmanagement:
   fork: true
   pidfilepath: "/data/mongodb/pid/m.pid"
net:
   bindip: 10.10.18.10
   port: 27017
replication:
   replsetname: "rs0"

secondary的配置文件

systemlog:
   destination: file
   path: "/data/mongodb/log/mongod.log"
   logappend: true
storage:
   dbpath: "/data/mongodb/data"
   journal:
      enabled: true
   wiredtiger:
      engineconfig:
        cachesizegb: 2
processmanagement:
   fork: true
   pidfilepath: "/data/mongodb/pid/m.pid"
net:
   bindip: 10.10.18.11
   port: 27017
replication:
   replsetname: "rs0"

arbiter的配置文件

systemlog:
   destination: file
   path: "/data/mongodb/log/mongod.log"
   logappend: true
storage:
   dbpath: "/data/mongodb/data"
   journal:
      enabled: true
   wiredtiger:
      engineconfig:
        cachesizegb: 2
processmanagement:
   fork: true
   pidfilepath: "/data/mongodb/pid/m.pid"
net:
   bindip: 10.10.18.12
   port: 27017
replication:
   replsetname: "rs0"

启动三台服务器上的mongdb

mongod -f /data/mongodb/mongod.conf

三、配置replica set

登录三台服务器中任意一台,登录mongo

mongo --host 10.10.18.10
>cfg={ _id:"rs0",members:[{_id:0,host:'10.10.18.10:27017',priority:1},{_id:1,host:'10.10.18.11:27017',priority:1},{_id:2,host:'10.10.18.12:27017',arbiteronly:true}] };

>rs.initiate(cfg)

查看replica set配置

> rs.conf()

{
"_id" : "rs0",
"version" : 1,
"protocolversion" : numberlong(1),
"writeconcernmajorityjournaldefault" : true,
"members" : [
{
"_id" : 0,
"host" : "10.10.18.10:27017",
"arbiteronly" : false,
"buildindexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {

},
"slavedelay" : numberlong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "10.10.18.11:27017",
"arbiteronly" : false,
"buildindexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {

},
"slavedelay" : numberlong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "10.10.18.12:27017",
"arbiteronly" : true,
"buildindexes" : true,
"hidden" : false,
"priority" : 0,
"tags" : {

},
"slavedelay" : numberlong(0),
"votes" : 1
}
],
"settings" : {
"chainingallowed" : true,
"heartbeatintervalmillis" : 2000,
"heartbeattimeoutsecs" : 10,
"electiontimeoutmillis" : 10000,
"catchuptimeoutmillis" : -1,
"catchuptakeoverdelaymillis" : 30000,
"getlasterrormodes" : {

},
"getlasterrordefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicasetid" : objectid("5cff76e5e57e23a5bc7054e2")
}
}

四、验证replica set

在primary中插入数据

rs0:primary>  show dbs
admin   0.000gb
config  0.000gb
local   0.000gb
rs0:primary> db.users.insertone(
... {
... name:"sue",
... age: 26,
... status:"pending"
... })
{
        "acknowledged" : true,
        "insertedid" : objectid("5cff79e8993e70290a081d04")
}

rs0:primary> db.users.find()
{ "_id" : objectid("5cff79e8993e70290a081d04"), "name" : "sue", "age" : 26, "status" : "pending" }

在secondary中,默认是不允许读

rs0:secondary> db.users.find()
error: error: {
        "operationtime" : timestamp(1560247181, 1),
        "ok" : 0,
        "errmsg" : "not master and slaveok=false",
        "code" : 13435,
        "codename" : "notmasternoslaveok",
        "$clustertime" : {
                "clustertime" : timestamp(1560247181, 1),
                "signature" : {
                        "hash" : bindata(0,"aaaaaaaaaaaaaaaaaaaaaaaaaaa="),
                        "keyid" : numberlong(0)
                }
        }
}

五、故障模拟

停止primary上的mongo进程,在secondary上登录mongo查看

mongo --host 10.10.18.11

rs0:primary> db.users.find() { "_id" : objectid("5cff79e8993e70290a081d04"), "name" : "sue", "age" : 26, "status" : "pending" }

发现原secondary变成primary,并且可以进行查询。

开启原primary服务器上的mongo进程,该服务器变成了secondary。