MongoDB 副本集架构
程序员文章站
2022-05-08 15:01:00
...
MongoDB 副本集架构
1. 副本集介绍
1.1 角色:
primary 主 :主节点 写入数据
secondary 备 : 副本集节点 同步prmary主节点数据
Arbiter 仲裁 :Arbiter 节点只参与投票,不能被选为 Primary,并且不从 Primary 同步数据
通过优先级来进行选举
架构一
primary 主 : 写入数据
secondary 备 : 同步prmary主节点数据
Arbiter 仲裁 :Arbiter 节点只参与投票
演变:当主节点故障,其他两个副本集节点会再次竞选Primary角色
演变:当主节点故障由Arbiter仲裁来投票那台主机是Primary角色。
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 创建多实例目录
[aaa@qq.com ~]# mkdir -p /opt/mongo_cluster/mongo_2801{7,8,9}/{conf,logs,pid}
[aaa@qq.com ~]# 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
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
[aaa@qq.com ~]# cp /opt/mongo_cluster/mongo_28017/conf/mongo_28017.conf /opt/mongo_cluster/mongo_28018/conf/mongo_28018.conf
[aaa@qq.com ~]# cp /opt/mongo_cluster/mongo_28017/conf/mongo_28017.conf /opt/mongo_cluster/mongo_28019/conf/mongo_28019.conf
[aaa@qq.com ~]# sed -i 's#28017#28018#g' /opt/mongo_cluster/mongo_28018/conf/mongo_28018.conf
[aaa@qq.com ~]# sed -i 's#28017#28019#g' /opt/mongo_cluster/mongo_28019/conf/mongo_28019.conf
2.5 创建数据目录
[aaa@qq.com ~]# mkdir /data/mongo_cluster/mongo_2801{7,8,9} -p
[aaa@qq.com ~]# 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
2.7 创建副本集
[aaa@qq.com ~]# 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)
查看状态 rs.status()
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" }
]);
2.7.2 登录副本mongo查看
这里会提示副本不可读,通过命令配置副本可读
dba58:SECONDARY> rs.slaveOk();
dba58:SECONDARY> db.inventory.find()
已同步到其备实例中。
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 登录副本节点查看
此时 28019节点变为PRIMARY
2.9.3 重新启动损坏节点
[aaa@qq.com ~]# mongod -f /opt/mongo_cluster/mongo_28017/conf/mongo_28017.conf
[aaa@qq.com ~]# mongo 127.0.0.1:28017
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)
2.10.2 主节点主动降级
dba58:PRIMARY> rs.stepDown()
降级成功
验证: 此时优先级高的为主状态
2.11 新增副本集节点配置
创建新节点目录及启动
[aaa@qq.com ~]# mkdir /opt/mongo_cluster/mongo_28010/{conf,logs,pid} -p
[aaa@qq.com ~]# mkdir /data/mongo_cluster/mongo_28010
[aaa@qq.com ~]# cp /opt/mongo_cluster/mongo_28017/conf/mongo_28017.conf /opt/mongo_cluster/mongo_28010/conf/mongo_28010.conf
[aaa@qq.com ~]# sed -i 's#28017#28010#g' /opt/mongo_cluster/mongo_28010/conf/mongo_28010.conf
[aaa@qq.com ~]# mongod -f /opt/mongo_cluster/mongo_28010/conf/mongo_28010.conf
[aaa@qq.com ~]# mongo db01:28010
新增节点操作命令
#进入主节点进行添加
[aaa@qq.com ~]$ mongo db01:28017
dba58:PRIMARY> use admin
dba58:PRIMARY> rs.add("db01:28010")
验证
rs.status()
28010已加入集群。
2.12 删除旧节点
#主节点操作
dba58:PRIMARY> rs.remove("db01:28010")
验证
rs.status()
表中没有db01:28010节点 删除节点成功
目前28010状态为OTHER
#关闭28010节点
[aaa@qq.com ~]# mongod -f /opt/mongo_cluster/mongo_28010/conf/mongo_28010.conf --shutdown、
[aaa@qq.com ~]# rm -rf /data/mongo_cluster/mongo_28010/*
2.13 增加仲裁节点
注意:Arbiter 节点只参与投票,不能被选为 Primary,并且不从 Primary 同步数据
#将28010节点设备为Arbiter角色
[aaa@qq.com ~]# mongo 127.0.0.1:28017
dba58:PRIMARY> rs.addArb("db01:28010")
dba58:PRIMARY> rs.status() #//查看状态信息
进入28010节点查看状态
完成
下一篇: PHP编程常用技巧四则_PHP