前段时间碰到了Mongo的集群部署, 当时没有做记录, 现在来做一下笔记, 方便后续使用, 下面我们会从0开始搭建MongoDB的集群
准备工作
准备MongoDB
这里用的是MongoDB社区版(Community Server) 5.0.6 --- 当前最新版
使用本教程配置的MongoDB集群是社区版本
准备机器
下面我准备了三台机器,用于部署集群
操作系统 | IP地址 | 备注 |
---|---|---|
CentOS 7.6 | 192.168.100.133 | 主节点 |
CentOS 7.6 | 192.168.100.134 | 备节点 |
CentOS 7.6 | 192.168.100.135 | 仲裁节点 |
请确保各机器的防火墙已关闭或对应的端口规则已放通
安装MongDB
配置完mongodb.service文件即可, 不要去启动MongoDB服务
如果你在安装的过程中, 不慎启动了MongoDB, 导致了后续的集群部署出现问题, 则需要删除MongoDB数据目录下的所有数据
配置MongoDB集群
修改上述机器列表中的MongoDB配置文件
vim /usr/local/mongodb/conf/mongodb.conf
向文件末尾添加如下内容
# 复制集的名字(可自定义)
replSet=test
# 设置每个数据库将被保存在一个单独的目录
directoryperdb=true
# 设置oplog的大小MB
oplogSize=2000
# 设置时区
timeZoneInfo=/usr/share/zoneinfo
启动上述机器列表中的MongoDB
systemctl start mongodb
配置MongoDB
进入MongoDB(主节点)
mongo
在MongoDB上编辑集群配置
config = {
_id:"test",
members:[
{_id:0,host:"192.168.100.133:27017",priority:2},
{_id:1,host:"192.168.100.134:27017",priority:1},
{_id:2,host:"192.168.100.135:27017",arbiterOnly:true}
]
}
应用上述配置
rs.initiate(config)
执行完上述命令之后, 会返回如下结果
{ "ok" : 1 }
此时,我们的集群配置就已经完成了, 我们可以通过如下命令各机器的状态
rs.status()
返回结果
{
"set" : "test",
"date" : ISODate("2022-04-06T10:06:31.759Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"votingMembersCount" : 3,
"writableVotingMembersCount" : 2,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1649239583, 1),
"t" : NumberLong(1)
},
"lastCommittedWallTime" : ISODate("2022-04-06T10:06:23.075Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1649239583, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1649239583, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1649239583, 1),
"t" : NumberLong(1)
},
"lastAppliedWallTime" : ISODate("2022-04-06T10:06:23.075Z"),
"lastDurableWallTime" : ISODate("2022-04-06T10:06:23.075Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1649239523, 1),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2022-04-06T10:03:43Z"),
"electionTerm" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(1649239411, 1),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1649239411, 1),
"t" : NumberLong(-1)
},
"numVotesNeeded" : 2,
"priorityAtElection" : 2,
"electionTimeoutMillis" : NumberLong(10000),
"numCatchUpOps" : NumberLong(0),
"newTermStartDate" : ISODate("2022-04-06T10:03:43.036Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2022-04-06T10:03:43.739Z")
},
"members" : [
{
"_id" : 0,
"name" : "192.168.100.133:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1125,
"optime" : {
"ts" : Timestamp(1649239583, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2022-04-06T10:06:23Z"),
"lastAppliedWallTime" : ISODate("2022-04-06T10:06:23.075Z"),
"lastDurableWallTime" : ISODate("2022-04-06T10:06:23.075Z"),
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1649239423, 1),
"electionDate" : ISODate("2022-04-06T10:03:43Z"),
"configVersion" : 1,
"configTerm" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "192.168.100.134:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 179,
"optime" : {
"ts" : Timestamp(1649239583, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1649239583, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2022-04-06T10:06:23Z"),
"optimeDurableDate" : ISODate("2022-04-06T10:06:23Z"),
"lastAppliedWallTime" : ISODate("2022-04-06T10:06:23.075Z"),
"lastDurableWallTime" : ISODate("2022-04-06T10:06:23.075Z"),
"lastHeartbeat" : ISODate("2022-04-06T10:06:31.233Z"),
"lastHeartbeatRecv" : ISODate("2022-04-06T10:06:30.268Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "192.168.100.133:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1,
"configTerm" : 1
},
{
"_id" : 2,
"name" : "192.168.100.135:27017",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 179,
"lastHeartbeat" : ISODate("2022-04-06T10:06:31.233Z"),
"lastHeartbeatRecv" : ISODate("2022-04-06T10:06:31.230Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1,
"configTerm" : 1
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1649239583, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1649239583, 1)
}
在MongoDB(备节点)中执行如下语句
rs.secondaryOk()
执行上述语句,手动承认自己是集群中的备节点
此时, MongoDB的集群部署,就已经完成了,但是该集群还不是很安全,所以我们需要对MongoDB做安全配置
MongoDB集群安全配置
进入到如下目录
/usr/local/mongodb
在上述机器列表中创建密钥文件存放的目录
mkdir keys
在主节点中生成密钥文件
openssl rand -base64 765 -out /usr/local/mongodb/keys/mongodb.keyfile
设置密钥文件权限, 这一步非常重要
chmod 400 /usr/local/mongodb/keys/mongodb.keyfile
将上面生成的密钥文件, 分别上传到各机器中的密钥存放目录
上传完成之后,我们需要修改各机器的配置文件
vim /usr/local/mongodb/conf/mongodb.conf
修改内容如下
#开启用户认证(有用户则开启)
auth=true
# 设置密钥文件(追加的)
keyFile=/usr/local/mongodb/keys/mongodb.keyfile
修改完成之后, 重启各机器的MongoDB服务
systemctl restart mongodb
文章评论