mongodb版本4.2.5
1、下载安装
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.15.tgz
tar -xzf mongodb-linux-x86_64-rhel70-4.2.15.tgz
mv mongodb-linux-x86_64-rhel70-4.2.15 /usr/local/mongodb
2、创建目录
mkdir -p /data/mongodb/{db,logs}
3、配置文件
vim /data/mongodb/27017.conf
systemLog:
destination: file
logAppend: true
path: /data/mongodb/logs/27017.log
storage:
dbPath: /data/mongodb/db
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /data/mongodb/27017.pid
net:
port: 27017
bindIp: 0.0.0.0
security:
authorization: enabled #认证
fork: false 前台启动
fork: true 放入后台启动
4、启动
/usr/local/mongodb/bin/mongod -f /data/mongodb/27017.conf
登录后关闭
/usr/local/mongodb/bin/mongo 127.0.0.1:27017
> use admin
switched to db admin
> db.shutdownServer()
直接关闭
kill <mongod process ID>
5、给admin授权并登录及用户管理
shell> use admin;
Shell>db.createUser({user:"admin",pwd:"123456",roles:[{role:"root",db:"admin"}]})
shell> 修改admin密码
> db.changeUserPassword('admin','123456')
6、登录
/usr/local/mongodb/bin/mongo 127.0.0.1:27017 -u admin -p 123456 --authenticationDatabase admin
7、备份恢复
单库备份
/usr/local/mongodb/bin/mongodump -d cloud --host=127.0.0.1 --port=27017 -u admin -p 123456 -o /data/temp/mongodb_tmp/
恢复
/usr/local/mongodb/bin/mongorestore -d test --host=127.0.0.1 --port=27017 -u admin -p 123456 --authenticationDatabase admin /data/temp/mongodb_tmp/test/