mongodb配置、启动、备份

时间:2021-03-03 12:52:29
Mongodb:
启动:
/usr/bin/mongod --config /data/mydata/mongodb/mongodb.conf 停止Mongodb:
方法一:$ mongod --shutdown --dbpath 数据路径
方法二:查看进程,使用kill命令;不能使用kill -9,可用kill -2 或kill-15
方法三:在客户端进去,使用shutdown命令
> use admin;
switched to db admin
> db.shutdownServer();
server should be down.. 自动备份脚本:
/data/mydata/mongodb/mongodb_bak.sh
配置定时任务:crontab; //mongod.conf:
# mongod.conf # for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/ # where to write logging data.
systemLog:
destination: file
logAppend: true
path: /data/mydata/mongodb/logs/mongodb.log # Where and how to store data.
storage:
dbPath: /data/mydata/mongodb/db
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger: # how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongod.pid # location of pidfile # network interfaces
net:
port: 27017
bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces. #security: #operationProfiling: #replication: #sharding: ## Enterprise-Only Options #auditLog: #snmp: //mongodb_bak.sh:
#!/bin/bash
#backup MongoDB #mongodump命令路径
DUMP=mongodump
#临时备份目录
OUT_DIR=/data/mydata/mongodb/bak/bak_tmp
#备份存放路径
TAR_DIR=/data/mydata/mongodb/bak/bak_list
#获取当前系统时间
DATE=`date +%Y_%m_%d`
#数据库账号
DB_USER=
#数据库密码
DB_PASS=123
#DAYS=15代表删除15天前的备份,即只保留近15天的备份
DAYS=15
#最终保存的数据库备份文件
TAR_BAK="mongodb_bak_$DATE.tar.gz" cd $OUT_DIR
rm -rf $OUT_DIR/*
mkdir -p $OUT_DIR/$DATE
#备份全部数据库
#$DUMP -h 10.168.1.10:27017 -u $DB_USER -p $DB_PASS --authenticationDatabase "admin" -o $OUT_DIR/$DATE
$DUMP -o $OUT_DIR/$DATE
#压缩为.tar.gz格式
tar -zcvf $TAR_DIR/$TAR_BAK $OUT_DIR/$DATE
#删除15天前的备份文件
find $TAR_DIR/ -mtime +$DAYS -delete exit