用树莓派搭建MongoDB复制集
Mercer-Lee的空间 2020-09-29
树莓派
Linux
MongoDB
数据库
# MongoDB复制集
复制集(replica Set)或者副本集是MongoDB的核心高可用特性之一,
它基于主节点的oplog日志持续传送到辅助节点,
并重放得以实现主从节点一致。再结合心跳机制,当感知到主节点不可访问或宕机的情形下,
辅助节点通过选举机制来从剩余的辅助节点中推选一个新的主节点从而实现自动切换
MongoDB的复制集在生产环境中是最低的要求,当然,你也可以上分片集群来使你的数据库更稳定,性能更好。
MongoDB的复制集搭建其实是比较简单的,不过再简单也需要我们来实践部署。
# 准备好树莓派
我不建议在自己的电脑上来时间部署树莓派,不然很影响你的工作,这时候树莓派就派上了用场,我们先找一个安装了Linux系统和MongoDB的树莓派,不懂安装可以先看我这个文章 (opens new window)。
# 配置文件
我们准备创建三个节点,一个主节点,两个从节点。首先创建各个复制集的配置文件和log存放的文件夹
mkdir data
cd data
mkdir -p data{1,2,3}
创建配置文件:
sudo vim data1/mongod.conf
输入以下内容:
systemLog:
destination: file
path: data/data1/mongod.log # 日志文件路径
logAppend: true
storage:
dbPath: data/data1 # 数据目录
net:
bindIp: 0.0.0.0
port: 28017 # 这个端口不能冲突
replication:
replSetName: rs0
processManagement:
fork: true # 这个一定要,很关键
其余的两个节点的配置文件在分别的文件夹创建:
systemLog:
destination: file
path: data/data2/mongod.log # 日志文件路径
logAppend: true
storage:
dbPath: data/data2 # 数据目录
net:
bindIp: 0.0.0.0
port: 28018 # 这个端口不能冲突
replication:
replSetName: rs0
processManagement:
fork: true
systemLog:
destination: file
path: data/data3/mongod.log # 日志文件路径
logAppend: true
storage:
dbPath: data/data3 # 数据目录
net:
bindIp: 0.0.0.0
port: 28019 # 这个端口不能冲突
replication:
replSetName: rs0
processManagement:
fork: true
# 执行进程
cd ~
sudo mongod -f data/data1/mongod.conf
sudo mongod -f data/data2/mongod.conf
sudo mongod -f data/data2/mongod.conf
执行进程成功应该能看到这样的:
# 配置复制集
进入shell
mongo --port 28017
创建复制集:
rs.initiate({
_id: "rs0",
members: [{
_id: 0,
host: "localhost:28017"
},{
_id: 1,
host: "localhost:28018"
},{
_id: 2,
host: "localhost:28019"
}]
})
查看进程状态:
rs.status()
shell会弹出当前的状态:
{
"set" : "rs0",
"date" : ISODate("2021-08-25T09:30:55.438Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"votingMembersCount" : 3,
"writableVotingMembersCount" : 3,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastCommittedWallTime" : ISODate("1970-01-01T00:00:00Z"),
"appliedOpTime" : {
"ts" : Timestamp(1629883855, 6),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1629883855, 6),
"t" : NumberLong(1)
},
"lastAppliedWallTime" : ISODate("2021-08-25T09:30:55.388Z"),
"lastDurableWallTime" : ISODate("2021-08-25T09:30:55.388Z")
},
"lastStableRecoveryTimestamp" : Timestamp(0, 0),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2021-08-25T09:30:55.160Z"),
"electionTerm" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1629883844, 1),
"t" : NumberLong(-1)
},
"numVotesNeeded" : 2,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"numCatchUpOps" : NumberLong(0),
"newTermStartDate" : ISODate("2021-08-25T09:30:55.259Z")
},
"members" : [
{
"_id" : 0,
"name" : "localhost:28017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 402,
"optime" : {
"ts" : Timestamp(1629883855, 6),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-08-25T09:30:55Z"),
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "Could not find member to sync from",
"electionTime" : Timestamp(1629883855, 1),
"electionDate" : ISODate("2021-08-25T09:30:55Z"),
"configVersion" : 1,
"configTerm" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "localhost:28018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 10,
"optime" : {
"ts" : Timestamp(1629883844, 1),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(1629883844, 1),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("2021-08-25T09:30:44Z"),
"optimeDurableDate" : ISODate("2021-08-25T09:30:44Z"),
"lastHeartbeat" : ISODate("2021-08-25T09:30:55.192Z"),
"lastHeartbeatRecv" : ISODate("2021-08-25T09:30:55.223Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1,
"configTerm" : 0
},
{
"_id" : 2,
"name" : "localhost:28019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 10,
"optime" : {
"ts" : Timestamp(1629883844, 1),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(1629883844, 1),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("2021-08-25T09:30:44Z"),
"optimeDurableDate" : ISODate("2021-08-25T09:30:44Z"),
"lastHeartbeat" : ISODate("2021-08-25T09:30:55.191Z"),
"lastHeartbeatRecv" : ISODate("2021-08-25T09:30:55.213Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1,
"configTerm" : 0
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1629883855, 6),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1629883855, 6)
}ash
# 总结
这样我们的MongoDB复制集就完成了,其实并不是很难,如果是在生产环境那就是直接把localhost改成服务器的IP就行了。