拷贝rpm文件
cd /root/mongodb
yum install -y *
配置节点
定义变量
NETWORK_ETH='eth0'
ip=`ip address show dev ${NETWORK_ETH} | grep 'inet ' | awk -F ' ' '{print $2}' | cut -d / -f 1`
#ip=`ifconfig eth1 | grep 'inet ' | awk -F ' ' '{print $2}' | cut -d : -f 2`
confPath='/etc/mongod.conf'
mongoDataPath='/data/mongodb/data'
mongopath='/data/mongodb'
新建mongodb的存储目录
mkdir -p ${mongopath}
mkdir -p ${mongoDataPath} # [path to mongdb data]
配置文件
cat > ${confPath} <<EOF
dbpath=${mongoDataPath}
logpath=${mongopath}/mongo.log
pidfilepath=${mongopath}/mongo.pid
directoryperdb=true
logappend=true
replSet=tnebula
bind_ip=${ip}
port=27017
#auth=true
oplogSize=100
fork=true
noprealloc=true
#maxConns=4000
EOF
启动mongo服务
mongod -f ${confPath}
其他两台副本节点(secondary)、仲裁节点(arbiter)也执行以上操作
登录主节点组建集群
mongo 172.20.10.5:27017
use admin
#[根据现场修改IP]
cfg={ _id:"tnebula",members:[{_id:0,host:'172.26.1.14:27017',priority:2},{_id:1,host:'172.26.1.15:27017',priority:1},{_id:2,host:'172.26.1.16:27017',arbiterOnly:true}] };
rs.initiate(cfg)
#检测集群状态
rs.status();
通过集群地址连接 [根据现场修改IP]
mongo mongodb://172.26.1.14:27017,172.26.1.15:27017,172.26.1.16:27017/?replicaSet=tnebula
数据备份
# mongodb导出、整库导出 去掉 -d
mongodump -h 172.26.1.14:27017 -o -d test mongodb_bak20200319
# mongodb导入
mongorestore --host 172.26.1.14:27017 --port 27017 --username mongouser --password "password" --authenticationDatabase=admin --dir=./mongodb_bak20200319 --drop
集群故障处理
1、关闭一直处于RECOVERING状态的mongodb server
/opt/mongodb/mongodb-linux-x86_64-2.4.8/bin/mongo 127.0.0.1:22001
use admin
db.shutdownServer()
2、将原数据目录改名,新建数据目录,再启动mongodb实例
mv /opt/mongodb/shard1/data /opt/mongodb/shard1/data_bak
mkdir /opt/mongodb/shard1/data
/opt/mongodb/mongodb-linux-x86_64-2.4.8/bin/mongod --shardsvr --replSet shard1 --port 22001 --dbpath /opt/mongodb/shard1/data --logpath /opt/mongodb/shard1/log/shard1.log --fork
3、查看恢复状态,为STARTUP2
/opt/mongodb/mongodb-linux-x86_64-2.4.8/bin/mongo 127.0.0.1:22001
use admin
rs.status()
本人也编写了ansible-playbook一键部署mongodb集群,github:https://github.com/xiaowenxiao/deploy/tree/master/ansible-mongodb-cluster-deploy