安装部署MongoDB
- 官方下载安装包
- 本文档的集群部署方案只用到了两台服务器,一主一从
服务器A(192.168.1.1)-主节点
# 上传mongodb-4.4.9.tar.gz到/data/appuser01目录下
# 部署用户appuser01执行以下命令
cd /data/appuser01
# 解压缩
tar -zxvf mongodb-4.4.9.tar.gz
# 进入mongodb目录
cd mongodb-4.4.9/
# 修改配置文件(部署目录没有变更则不用修改配置文件)
# vi conf/mongod.conf
# 授权
chmod 400 keyfile
# 启动
nohup ./bin/mongod --config ./conf/mongod.conf &
# 退出仍保留nohup进程
ctrl+a+d
服务器B(192.168.1.2)-副本节点、兼仲裁节点
# 上传mongodb-4.4.9.tar.gz到/data/appuser01目录下
# 部署用户appuser01执行以下命令
cd /data/appuser01
# 解压缩
tar -zxvf mongodb-4.4.9.tar.gz
# 进入mongodb目录
cd mongodb-4.4.9/
# 修改配置文件(部署目录没有变更则不用修改配置文件)
# vi conf/mongod.conf
mkdir -p arbiter/data
mkdir -p arbiter/log
mkdir -p arbiter/pid
# 修改仲裁节点配置文件
cp conf/mongod.conf conf/mongo.arbiter.conf
vi conf/mongo.arbiter.conf
# 授权
chmod 400 keyfile
# 启动从节点
nohup ./bin/mongod --config ./conf/mongod.conf &
# 退出仍保留nohup进程
ctrl+a+d
# 重新登录
# 启动仲裁节点
nohup ./bin/mongod --config ./conf/mongod.arbiter.conf &
# 退出仍保留nohup进程
ctrl+a+d
mongo.arbiter.conf修改内容如下:
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /data/appuser01/mongodb-4.4.9/arbiter/log/mongod.log
# Where and how to store data.
storage:
dbPath: /data/appuser01/mongodb-4.4.9/arbiter/data
journal:
enabled: true
# engine:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /data/appuser01/mongodb-4.4.9/arbiter/pid/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27018
bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
security:
authorization: enabled
keyFile: /data/appuser01/mongodb-4.4.9/keyfile
replication:
oplogSizeMB: 8192
replSetName: rs0
初始化集群
mongo安装完毕后,开始初始化副本集集群,登录服务器A,执行以下命令:
# 初始化副本集集群
rs.initiate({_id:"rs0",members:[{_id:0,host:"192.168.1.1:27017",priority:3},{_id:1,host:"192.168.1.2:27017",priority:2},{_id:2,host:"192.168.1.2:27018",priority:0,arbiterOnly:true}]})
# 查看集群状态
rs.status()
# 切换库
use admin
# 创建管理用户
db.createUser({ user: 'root_user', pwd: 'Mongo20240316A:!', roles: [ { role: "root", db: "admin" } ] })
# 验证管理用户
db.auth("root_user","Mongo20240316A:!")
# 切换业务库
use ase_01
# 创建业务库访问账号
db.createUser({user: "ase_user", pwd: "Mongo20240316Pwd@!", roles: [{ role: "dbOwner", db: "ase_01" }]})