MongoDB的集群模式--Replica Set
一、replica set 集群分为两种架构:
- 奇数个节点构成replica set,所有节点拥有数据集。最小架构: 1个primary节点,2个secondary节点
- 偶数个节点 + 一个仲裁节点 构成的replica set,节点拥有数据集,仲裁节点仅参与仲裁选举出primary节点。 最小架构:1个primary节点,1个secondary节点,1个arbiter节点
接下来就以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。
推荐阅读
-
MySQL MGR集群单主模式的自动搭建和自动化故障修复
-
Redis集群模式下的redis-py-cluster方式读写测试
-
MongoDB搭建高可用集群的完整步骤(3个分片+3个副本)
-
mongodb replica set 添加删除节点的2种方法
-
mongodb replica set 配置高性能多服务器详解
-
php实现的mongoDB单例模式操作类
-
用VMWare搭建服务器集群不能上外网的三种模式下对应解决办法
-
MongoDB添加仲裁节点报错:replica set IDs do not match的解决方法
-
MongoDB的集群模式--Sharding(分片)
-
rocketmq在Kubernetes(k8s)中的集群配置,2m-2s-async:多Master多Slave模式,异步复制