第六章 MongoDB 副本集搭建
- 搭建副本集群
- 在主节点添加数据,查看从节点
- kill一个
mongod实例,模拟故障,查看选举
1.搭建副本集群
- 环境准备:操作环境:CentOS 7。需要三台安装MongoBD的主机。安装移步至:MongoDB安装与使用
host1:192.168.195.3
host2: 192.168.195.4
host3: 192.168.195.5
- 关闭Linux防火墙和SElinux
systemctl stop firewalld.service # 关闭防火墙
systemctl disable firewalld.service # 禁止开机启动
firewall-cmd --state # 查看防火墙状态
# 永久关闭Selinux 设置后需要重启
vim /etc/selinux/config
SELINUX=disabled
- 给每个
mongod实例创建数据目录和日志目录
mkdir -p /opt/local/mongodb/data
mkdir -p /opt/local/mongodb/conf
- 修改
mongod.conf配置文件
vim /opt/local/mongodb/conf/mongod.conf
systemLog:
destination: file
logAppend: true
path: /opt/local/mongodb/log/mongod.log
storage:
dbPath: /opt/local/mongodb/data
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /opt/local/mongodb/mongod.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 27017
bindIp: 0.0.0.0 # 这里所有ip地址都可以访问
replication:
replSetName: "rs0"
- 启动三台
mongod实例,登录到其中一个mongod实例,执行初始化
# 启动mongod服务
/opt/local/mongodb/bin/mongod -f /opt/local/mongodb/conf/mongod.conf
# 连接mongod服务
/opt/local/mongodb/bin/mongo -p 27017
# 初始化 _id是副本集群名字
rs.initiate( {
_id : "rs0",
members: [
{ _id: 0, host: "192.168.195.3:27017" },
{ _id: 1, host: "192.168.195.4:27017" },
{ _id: 2, host: "192.168.195.5:27017" }
]
})
- 查看副本集群的配置情况
rs.conf()
{
"_id" : "rs0",
"version" : 1,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "192.168.195.3:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "192.168.195.4:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "192.168.195.5:27017",
"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("5f9eb1a81d83200f367912d7")
}
}
2.在主节点添加数据,查看从节点
- 登录到从节点 (
secondary) 查看student集合数据,此时并没有student集合
# secondary 节点默认不允许读,需要`rs.slaveOk()`设置可读
rs.slaveOk();
use test;
db.student.find({})
- 在主节点(
primary)添加student集合
db.student.insertOne({
name: "Tome",
age: 20,
address: "nanjing"
})
- 在
secondary节点上查看student集合就能看到从primary节点同步过来的数据了
db.student.find({})
{ "_id" : ObjectId("5f9eb62d9a01685fb407a5bc"), "name" : "Tome", "age" : 20, "address" : "nanjing" }
3.模拟故障
ps -aux | grep mongod
root 36483 1.4 10.4 1943052 103928 ? Sl 07:52 0:28 ./bin/mongod -f ./conf/mongodb.conf
root 70708 0.0 0.0 112824 976 pts/0 R+ 08:24 0:00 grep --color=auto mongod
kill -9 36483
从这里可以看到以前的 secondary 节点变成 primary 节点

本文介绍了MongoDB副本集的搭建过程,包括在CentOS 7环境下准备三台安装MongoDB的主机,关闭防火墙和SElinux,创建数据与日志目录,修改配置文件并初始化。还展示了在主节点添加数据后从节点的同步情况,以及模拟故障时节点的变化。
1398

被折叠的 条评论
为什么被折叠?



