MongoDB 副本集架构

1. 副本集介绍

1.1 角色:

primary 主 :主节点 写入数据

secondary 备 : 副本集节点 同步prmary主节点数据

Arbiter 仲裁 :Arbiter 节点只参与投票,不能被选为 Primary,并且不从 Primary 同步数据

通过优先级来进行选举

MongoDB 副本集架构_数据

架构一

primary 主 : 写入数据

secondary 备 : 同步prmary主节点数据

Arbiter 仲裁 :Arbiter 节点只参与投票

MongoDB 副本集架构_副本集_02

演变:当主节点故障,其他两个副本集节点会再次竞选Primary角色

MongoDB 副本集架构_副本集_03

演变:当主节点故障由Arbiter仲裁来投票那台主机是Primary角色。

MongoDB 副本集架构_副本集_04

2. 架构配置

本次案例使用一台主机 创建mongo多实例。

2.1 环境

主机

ip

端口

db01

127.0.0.1

28017

db01

127.0.0.1

28018

db01

127.0.0.1

28019

2.2 创建多实例目录

[root@db01 ~]# mkdir -p /opt/mongo_cluster/mongo_2801{7,8,9}/{conf,logs,pid}
[root@db01 ~]# tree /opt/mongo_cluster/
/opt/mongo_cluster/
├── mongo_28017
│ ├── conf
│ ├── logs
│ └── pid
├── mongo_28018
│ ├── conf
│ ├── logs
│ └── pid
└── mongo_28019
├── conf
├── logs
└── pid

2.3 上传mongodb-linux-x86_64-3.6.13.tgz到/opt/mono_cluster下并解压

tar zxvf mongodb-linux-x86_64-3.6.13.tgz 
ln -s /opt/mongo_cluster/mongodb-linux-x86_64-3.6.13 /opt/mongo_cluster/mongodb

MongoDB 副本集架构_副本集_05

2.4 创建配置文件

cat <<-EOF >/opt/mongo_cluster/mongo_28017/conf/mongo_28017.conf
systemLog:
destination: file
logAppend: true
path: /opt/mongo_cluster/mongo_28017/logs/mongodb.log
storage:
journal:
enabled: true
dbPath: /data/mongo_cluster/mongo_28017
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 0.5
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
processManagement:
fork: true
pidFilePath: /opt/mongo_cluster/mongo_28017/pid/mongod.pid

net:
port: 28017
bindIp: 0.0.0.0
replication:
oplogSizeMB: 1024
replSetName: dba58 //集群名称
EOF

[root@db01 ~]# cp /opt/mongo_cluster/mongo_28017/conf/mongo_28017.conf /opt/mongo_cluster/mongo_28018/conf/mongo_28018.conf
[root@db01 ~]# cp /opt/mongo_cluster/mongo_28017/conf/mongo_28017.conf /opt/mongo_cluster/mongo_28019/conf/mongo_28019.conf
[root@db01 ~]# sed -i 's#28017#28018#g' /opt/mongo_cluster/mongo_28018/conf/mongo_28018.conf
[root@db01 ~]# sed -i 's#28017#28019#g' /opt/mongo_cluster/mongo_28019/conf/mongo_28019.conf

2.5 创建数据目录

[root@db01 ~]# mkdir /data/mongo_cluster/mongo_2801{7,8,9} -p
[root@db01 ~]# tree /data/mongo_cluster/
/data/mongo_cluster/
├── mongo_28017
├── mongo_28018
└── mongo_28019

2.6 启动所有节点

echo "export "PATH=/opt/mongo_cluster/mongodb/bin:$PATH"  >> /etc/profile
source /etc/profile
mongod -f /opt/mongo_cluster/mongo_28017/conf/mongo_28017.conf
mongod -f /opt/mongo_cluster/mongo_28018/conf/mongo_28018.conf
mongod -f /opt/mongo_cluster/mongo_28019/conf/mongo_28019.conf

MongoDB 副本集架构_副本集_06

2.7 创建副本集

[root@db01 ~]# mongo 127.0.0.1:28017
config = {
_id : "dba58",
members : [
{_id : 0, host : " db01:28017"},
{_id : 1, host : " db01:28018"},
{_id : 2, host : " db01:28019"},
] }
rs.initiate(config)

MongoDB 副本集架构_数据_07

查看状态 rs.status()

MongoDB 副本集架构_数据_08

2.8 写入数据并验证

2.8.1 插入数据

db.inventory.insertMany( [
{ "item": "journal", "qty": 25, "size": { "h": 14, "w": 21, "uom": "cm" }, "status": "A" },
{ "item": "notebook", "qty": 50, "size": { "h": 8.5, "w": 11, "uom": "in" }, "status": "A" },
{ "item": "paper", "qty": 100, "size": { "h": 8.5, "w": 11, "uom": "in" }, "status": "D" },
{ "item": "planner", "qty": 75, "size": { "h": 22.85, "w": 30, "uom": "cm" }, "status": "D" },
{ "item": "postcard", "qty": 45, "size": { "h": 10, "w": 15.25, "uom": "cm" }, "status": "A" }
]);

MongoDB 副本集架构_mongodb_09

2.7.2 登录副本mongo查看

MongoDB 副本集架构_数据_10

这里会提示副本不可读,通过命令配置副本可读

dba58:SECONDARY> rs.slaveOk();
dba58:SECONDARY> db.inventory.find()

MongoDB 副本集架构_mongodb_11

已同步到其备实例中。

2.7.3 设置永久副本可读

echo "rs.slaveOk()" >> .mongorc.js

2.9 模拟故障转移

2.9.1 将主节点杀掉

mongod -f /opt/mongo_cluster/mongo_28017/conf/mongo_28017.conf --shutdown
killing process with pid: 19483

2.9.2 登录副本节点查看MongoDB 副本集架构_副本集_12

此时 28019节点变为PRIMARY

2.9.3 重新启动损坏节点

[root@db01 ~]# mongod -f /opt/mongo_cluster/mongo_28017/conf/mongo_28017.conf 
[root@db01 ~]# mongo 127.0.0.1:28017

MongoDB 副本集架构_副本集_13

28017启动后变为副本集状态。

2.10 副本集权重调整

如果初始化进群的时候没有设置权重,那么默认每个节点的权重都是一样的,也就是说每个节点都 可以竞选主节点

2.10.1 将28017节点优先级改为100

mongo 127.0.0.1:27019
dba58:PRIMARY> rs.conf()
{
"_id" : "dba58",
"version" : 1,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0, ###### //28017 ID
"host" : "db01:28017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {

},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "db01:28018",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {

},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "db01:28019",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"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("5f28e4d4f09d8ce7f14266a4")
}
}
dba58:PRIMARY> config = rs.conf()
#将28017优先级改为100
dba58:PRIMARY> config.members[0].priority=100
dba58:PRIMARY> rs.reconfig(config)

MongoDB 副本集架构_副本集_14

2.10.2 主节点主动降级

dba58:PRIMARY> rs.stepDown()

MongoDB 副本集架构_副本集_15

降级成功

验证: 此时优先级高的为主状态

MongoDB 副本集架构_数据_16

2.11 新增副本集节点配置


创建新节点目录及启动


[root@db01 ~]# mkdir /opt/mongo_cluster/mongo_28010/{conf,logs,pid} -p
[root@db01 ~]# mkdir /data/mongo_cluster/mongo_28010
[root@db01 ~]# cp /opt/mongo_cluster/mongo_28017/conf/mongo_28017.conf /opt/mongo_cluster/mongo_28010/conf/mongo_28010.conf
[root@db01 ~]# sed -i 's#28017#28010#g' /opt/mongo_cluster/mongo_28010/conf/mongo_28010.conf
[root@db01 ~]# mongod -f /opt/mongo_cluster/mongo_28010/conf/mongo_28010.conf
[root@db01 ~]# mongo db01:28010

新增节点操作命令

#进入主节点进行添加
[mongo@db01 ~]$ mongo db01:28017
dba58:PRIMARY> use admin
dba58:PRIMARY> rs.add("db01:28010")

MongoDB 副本集架构_数据_17

验证

rs.status()

MongoDB 副本集架构_mongodb_18

28010已加入集群。

2.12 删除旧节点

#主节点操作
dba58:PRIMARY> rs.remove("db01:28010")

MongoDB 副本集架构_mongodb_19

验证

rs.status()

MongoDB 副本集架构_数据_20

表中没有db01:28010节点 删除节点成功

MongoDB 副本集架构_副本集_21

目前28010状态为OTHER

#关闭28010节点
[root@db01 ~]# mongod -f /opt/mongo_cluster/mongo_28010/conf/mongo_28010.conf --shutdown、
[root@db01 ~]# rm -rf /data/mongo_cluster/mongo_28010/*

2.13 增加仲裁节点

注意:Arbiter 节点只参与投票,不能被选为 Primary,并且不从 Primary 同步数据

#将28010节点设备为Arbiter角色
[root@db01 ~]# mongo 127.0.0.1:28017
dba58:PRIMARY> rs.addArb("db01:28010")
dba58:PRIMARY> rs.status() #//查看状态信息

MongoDB 副本集架构_mongodb_22

进入28010节点查看状态

MongoDB 副本集架构_数据_23

完成