MongoDB 安装部署
简介
由于资源有限,在一台虚机上部署3个节点,一主一备,一个仲裁;
主机IP:10.0.153.211
安装包准备
获取指定版本的安装包,此处以:4.0.6版本为例
安装
安装目录规划
根目录:/soft/mongodb
master目录:/soft/mongodb/mongodb-master
master目录:/soft/mongodb/mongodb-slave
master目录:/soft/mongodb/mongodb-arbiter
创建master配置文件
目录:/soft/mongodb/mongodb-master/conf(如果目录不存在,手工创建)
# master.conf
dbpath=/opt/data/mongodb/master
logpath=/opt/mongodb/master.log
pidfilepath=/opt/mongodb/master.pid
#keyFile=/opt/mongodb/mongodb.key
directoryperdb=true
logappend=true
replSet=testdb
port=27017
#auth=true
#keyFile=/opt/mongodb/mongodb.key
oplogSize=100
fork=true
noprealloc=true
#maxConns=4000
创建slave配置文件
目录:/soft/mongodb/mongodb-slave/conf(如果目录不存在,手工创建)
# slave.conf
dbpath=/opt/data/mongodb/slave
logpath=/opt/mongodb/slave.log
pidfilepath=/opt/mongodb/slave.pid
#keyFile=/opt/mongodb/mongodb.key
directoryperdb=true
logappend=true
replSet=testdb
port=27018
#auth=true
#keyFile=/opt/mongodb/mongodb.key
oplogSize=100
fork=true
noprealloc=true
#maxConns=4000
创建arbiter配置文件
目录:/soft/mongodb/mongodb-arbiter/conf(如果目录不存在,手工创建)
# arbiter.conf
dbpath=/opt/data/mongodb/arbiter
logpath=/opt/mongodb/arbiter.log
pidfilepath=/opt/mongodb/arbiter.pid
#keyFile=/opt/mongodb/mongodb.key
directoryperdb=true
logappend=true
replSet=testdb
port=27019
#auth=true
#keyFile=/opt/mongodb/mongodb.key
oplogSize=100
fork=true
noprealloc=true
#maxConns=4000
创建目录
mkdir -p /opt/data/mongodb/arbiter
mkdir -p /opt/data/mongodb/master
mkdir -p /opt/data/mongodb/slave
mkdir -p /opt/mongodb
启动mongodb服务
# 获取主机ip
ip=`ifconfig -a |grep inet |grep -v inet6 |grep -v 172.|grep -v 192.168 |grep -v 127.0.0.1|awk '{print $2}'
#启动master服务
/soft/mongodb/mongodb-master/bin/mongod -f /soft/mongodb/mongodb-master/conf/mongodb-master.conf --bind_ip=$ip
#启动slave服务
/soft/mongodb/mongodb-slave/bin/mongod -f /soft/mongodb/mongodb-slave/conf/mongodb-slave.conf --bind_ip=$ip
#启动arbiter服务
/soft/mongodb/mongodb-arbiter/bin/mongod -f /soft/mongodb/mongodb-arbiter/conf/mongodb-arbiter.conf --bind_ip=$ip
构建replica集群
# cfg="{ _id:\"testdb\", members:[{_id:0,host:'$ip:27017',priority:2},{_id:1,host:'$ip:27018',priority:1},{_id:2,host:'$ip:27019',arbiterOnly:true}]};"
# echo $cfg
# /soft/mongodb/mongodb-master/bin/mongo $ip
# >use admin
(只有这一步,需要复制上面echo $cfg 的结果赋值给cfg)
# >cfg=上面echo $cfg的结果
# >rs.initiate(cfg);
# >rs.status()
增加认证授权
# 增加用户
# >db.createUser({user:'root',pwd:'snbprod',roles:[{role:'root',db:'admin'}]});
// 创建认证文件,改变文件读写设置600(必须)
$ openssl rand -base64 756 >mongodb.key
# 修改配置文件(master,slave,arbiter),取消如下行的注释
auth=true
keyFile=/opt/mongodb/mongodb.key
#重启所有的服务(master,slave,arbiter)
# 杀掉mongo进程
ps -ef |grep mongo |grep -v grep |awk '{print $2}' |xargs kill -9
# 启动服务
参见启动mongodb服务小节
创建数据库
# /soft/mongodb/mongodb-master/bin/mongo 10.0.153.211
# >use admin
# > db.auth("root","snbprod");
# > use oces
# > db.createUser({user:'ocesuser',pwd:'ocesuser',roles:[{role:'readWrite',db:'oces'}]});
更多操作
# 删除用户
# > db.dropUser("ocesuser");
mongodb设置过期时间
# 查询索引
# > db.personCredit.createIndex({"createdTime":1,"param":1})
# > db.enterpriseCredit.createIndex({"createdTime":1,"param":1})
# 过期时间
# > db.personCredit.createIndex({"createdTime":1},{expireAfterSeconds:2343600})
# > db.enterpriseCredit.createIndex({"createdTime":1},{expireAfterSeconds:2343600})
# 插入数据
# > db.personCredit.insert({"createdTime":new Date(),"param":"test"});
# 时间显示问题
mongodb默认是utc时间,比当前时区晚了8个小时
Robo中 options->display date in local timezone;