关于MongoDB之前已经写了一个复制集的集群搭建文章,内中是主+从+裁决节点的部署的方式,该方式我推荐使用2个节点去配置使用,如果是3个节点,这也是官方推荐的最小节点,部署模式为主+从+从的 方式。并附加安全验证,已应对当下服务端的安全性要求。
一、环境需要
mongodb-linux-x86_64-rhel70-4.2.8.tgz 软件包
Centos75操作系统(最小化安全),节点信息如下
10.1.5.246 master1
10.1.5.245 node1
10.1.5.244 node2
二、Mongo复制集安装
1. 解压缩mongodb-linux-x86_64-rhel70-4.2.8.tgz软件
tar -xvf mongodb-linux-x86_64-rhel70-4.2.8.tgz
mv mongodb-linux-x86_64-rhel70-4.2.8 mongodb
2. 创建默认工作目录包括:conf,data,logs
cd ~/mongodb
mkdir {conf,data,logs}
3. 创建conf/mongodb.conf配置文件,添加如下内容:
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /root/mongodb/data # 配置数据存放的位置
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /root/mongodb/logs/mongod.log # 日志文件
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0 # 配置网络
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
fork: true
#security:
# keyFile: /root/mongodb/conf/keyfile
#replication:
replication:
replSetName: rs # 定义副本集的名称
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
4. 启动所有节点服务,进入mongodb/bin目录
cd ~/mongodb/bin
./mongod -f ../conf/mongodb.conf
5. 切换到主节点,本环境为master1,登录mongo通过mongo shell,进行集群初始化
./mongo
rs.initiate({"_id": "rs","members":[{"_id":1,"host":"10.1.5.244"},{"_id":2,"host":"10.1.5.245"},{"_id":3,"host":"10.1.5.246"}]})
# 显示ok:1代表集群初始成功
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1636524099, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1636524099, 1)
}
# 等待一会,再次执行操作显示
rs:SECONDARY> show users
rs:PRIMARY> show users
6. 创建管理员用户,在mongo shell模式下
use admin
db.createUser({user:"admin",pwd:"password",roles:[{role:"userAdminAnyDatabase",db:"admin"}]})
7. 切换业务库创建业务库管理用户
use test
db.createUser({user:"tester",pwd:"password",roles:[{role:"dbOwner",db:"test"}]})
8. 查看mongo集群状态
rs.status()
...
...
#忽略上述输出信息
"members" : [
{
"_id" : 1,
"name" : "10.1.5.244:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 898,
"optime" : {
"ts" : Timestamp(1636524111, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-11-10T06:01:51Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1636524110, 1),
"electionDate" : ISODate("2021-11-10T06:01:50Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "10.1.5.245:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 28,
"optime" : {
"ts" : Timestamp(1636524111, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1636524111, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-11-10T06:01:51Z"),
"optimeDurableDate" : ISODate("2021-11-10T06:01:51Z"),
"lastHeartbeat" : ISODate("2021-11-10T06:02:06.648Z"),
"lastHeartbeatRecv" : ISODate("2021-11-10T06:02:07.450Z"),
"pingMs" : NumberLong(1),
"lastHeartbeatMessage" : "",
"syncingTo" : "10.1.5.244:27017",
"syncSourceHost" : "10.1.5.244:27017",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 3,
"name" : "10.1.5.246:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 28,
"optime" : {
"ts" : Timestamp(1636524111, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1636524111, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-11-10T06:01:51Z"),
"optimeDurableDate" : ISODate("2021-11-10T06:01:51Z"),
"lastHeartbeat" : ISODate("2021-11-10T06:02:06.647Z"),
"lastHeartbeatRecv" : ISODate("2021-11-10T06:02:07.426Z"),
"pingMs" : NumberLong(1),
"lastHeartbeatMessage" : "",
"syncingTo" : "10.1.5.244:27017",
"syncSourceHost" : "10.1.5.244:27017",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1636524111, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
9. 关闭所有节点mongo,mongo shell模式
db.shutdwonServer()
10. 创建keyfile文件
openssl rand -base64 756 > /root/mongodb/conf/keyfile
chmod 400 /root/mongodb/conf/keyfile
11. 修改配置文件,打开如下信息注释:
security:
keyFile: /root/mongodb/conf/keyfile
12. 启动所有mongo服务
13. mongo shell登录,并查看集群状态
./mongo
use admin
db.auth('admin','password')
rs.status()
好了,上述基本已经完成了mongodb复制集集群的搭建并配置用户验证。
如果验证集群的故障迁移,可以关闭一个MongoDB服务,登录其它节点查看rs.status()状态,主节点是否能够正常切换。