1.下载地址
https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.0.3.tgz
2.创建mongod用户用户组
useradd -s /sbin/nologin -M mongod
chattr file -i
lsattr file 查看文件隐藏属性
3.安装
tar -zxvf mongo.tgz
mv 移动到安装目录
mkdir -p ./data/db-1 ./log ./tmp
4.创建配置文件
touch bin/mongodb.conf
storage:
dbPath: /usr/local/mongodb-4.0.3/data/db-1
journal:
enabled: true
systemLog:
destination: file
logAppend: true
path: /usr/local/mongodb-4.0.3/log/mongodb-1.log
processManagement:
fork: true
pidFilePath: /usr/local/mongodb-4.0.3/tmp/mongod-1.pid
net:
port: 27017
bindIp: 0.0.0.0
# 配置副本集时解除注释
# replication:
# replSetName: "rs"
# 创建用户后解除注释
# security:
# authorization: enabled
# clusterAuthMode: "keyFile"
# keyFile: /usr/local/mongodb-4.0.3/bin/keyfile
5.创建mongodb数据库间通信所需密钥密钥
openssl rand -base64 756 > keyfile
chmod 600 keyfile
6.启动mongodb
./mongod -f ./mongodb.conf
6.连接mongodb,创建admin用户
./mongo
use admin
db.createUser({
user: ‘admin’,
pwd: ‘2b8ba2ded188’,
roles:[{
role: ‘root’,
db: ‘admin’
}]
})
7.开启副本集,需要至少三个mongdb服务
配置文件开启参数,配置统一的副本集名称
在主服务器上设置副本集配置
conf={
_id: "rs",
members: [
{ _id: 0, host: "127.0.0.1:27017" },
{ _id: 1, host: "127.0.0.1:27018" },
{ _id: 2, host: "127.0.0.1:28019",arbiterOnly:true }
]
}
rs.initiate(conf)
rs.status()
- ./mongodump -u admin -p=pwd -hip:port —authenticationDatabase admin -d 待备份数据库 -o 备份文件地址
- ./mongorestore -h 81.70.159.29:27017 -u admin -p 2b8ba2ded188 —authenticationDatabase admin -dir ./back/ns-platform -d ns-platform
- 重命名数据库