本次在一台服务器上搭建,根据端口区分不同进程。架构为一主两从,其中一个从节点为投票节点。
IP | 端口 | 角色 |
10.238.162.33 | 27017 | PRIMARY |
10.238.162.33 | 27018 | SECONDARY |
10.238.162.33 | 27019 | ARBITER |
实验环境为CentOS 7.5
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
安装MongoDB
MongoDB官网下载安装包
3.6.8版本
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.6.8.tgz
3.4.18版本
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.4.18.tgz
解压
tar zxf mongodb-linux-x86_64-rhel62-3.4.18.tgz
移动至/usr/local/mongodb 下
mv mongodb-linux-x86_64-rhel62-3.4.18 /usr/local/mongodb
设置环境变量并生效
echo 'export PATH=$PATH:/usr/local/mongodb/bin'>> /etc/profile
source /etc/profile
添加用户和组
groupadd mongo
useradd -g mongo mongo
创建三个节点的相关目录并修改权限
mkdir -p /data/mongodb27017/log/
mkdir -p /data/mongodb27018/log/
mkdir -p /data/mongodb27019/log/
chown -R mongo.mongo /data/
配置文件模板,只有端口号不同
#SERVER
fork = true
port = 27017
quiet = true
dbpath = /data/mongodb27017/
logpath = /data/mongodb27017/log/mongod.log
logappend = true
journal = true
#auth = true #开启认证
# <=3.4
nohttpinterface = true
directoryperdb = true
#SLOW_LOG
profile = 1
slowms = 500
#RS
replSet = rs01
oplogSize = 4096
# add for 3.4
# shardsvr = true
#SCO
#cinfigsvr = true
#configdb = MSCHOST
#USER
#keyFile=/
#setParameter=enableLocalhostAuthBypass=1
storageEngine=wiredTiger
wiredTigerCacheSizeGB=1
wiredTigerCollectionBlockCompressor=snappy
#mongo_version=mongodb34
启动进程
sudo -u mongo /usr/local/mongodb/bin/mongod -f /data/mongodb27017/mongodb27017.cnf
sudo -u mongo /usr/local/mongodb/bin/mongod -f /data/mongodb27018/mongodb27018.cnf
sudo -u mongo /usr/local/mongodb/bin/mongod -f /data/mongodb27019/mongodb27019.cnf
初始化
进入到MongoDB命令行
# mongo 10.238.162.33:27017
设置变量
cfg={_id:'rs01',version:1,members:[{_id:0,host:'10.238.162.33:27017'},{_id:1,host:'10.238.162.33:27018'},{_id:2,host:'10.238.162.33:27019',arbiterOnly:true}]};
初始化变量
rs.initiate(cfg);