| 前言 |
熟悉MongoDB的朋友们都知道它是非关系型数据库,文档查询速度快,适用于当下数据结构复杂的互联网业务。随着许多公司逐渐转向使用非关系型数据库,用户对数据容灾的需求也在加强。尤其对小型业务来说,如何用更少的资源兼顾数据库连接安全和数据备份,成为许多用户所关注的痛点。 |
一、MongoDB的主从备份模式
/usr/bin/mongod --dbpath=/data --master
/usr/bin/mongod -f etc/mongodb.conf
# mongodb.confdbpath = datalogpath = var/log/mongodb/mongodb.loglogappend = truejournal = truebind_ip = 0.0.0.0port = 27017oplogSize = 2048master = true
/usr/bin/mongod --dbpath=/data --slave --source 192.168.1.100:27017 --slavedelay 1 --autoresync
# mongodb.confdbpath = /datalogpath = /var/log/mongodb/mongodb.loglogappend = truejournal = truebind_ip = 0.0.0.0port = 27017oplogSize = 2048slave = truesource = 192.168.1.100:27017autoresync = trueslavedelay = 1
二、双节点MongoDB备份可能遇到的问题
# mongodb.confdbpath = datalogpath = var/log/mongodb/mongodb.loglogappend = truejournal = truebind_ip = 0.0.0.0port = 27017oplogSize = 2048master = trueslave = truesource = 192.168.1.200:27017autoresync = trueslavedelay = 1
# mongodb.confdbpath = datalogpath = var/log/mongodb/mongodb.loglogappend = truejournal = truebind_ip = 0.0.0.0port = 27017oplogSize = 2048master = trueslave = truesource = 192.168.1.100:27017autoresync = trueslavedelay = 1
三、兼顾连接安全性与双节点备份特性
sudo apt-get install -y rsync inotify-toolssudo yum install -y rsync inotify-tools
RSYNC_ENABLE=trueRSYNC_CONFIG_FILE='/etc/rsyncd.conf'RSYNC_OPTS=''RSYNC_NICE=''
root:123456mongodb:123456
log file=/var/log/rsyncdpid file=/var/run/rsyncd.pid[mongodb]comment = mongodb dbpathpath = /var/libuse chroot = yeslock file = /var/lock/rsyncdauth users = mongodbuid = mongodbgid = mongodbinclude = mongodblist = nohosts allow = *secrets file = /etc/rsyncd.secretsread only = falseignore errors
sudo chown 0:0 /etc/default/rsync /etc/rsyncd.conf /etc/rsyncd.secretssudo chmod 600 /etc/rsyncd.secrets
sudo /bin/sh -c 'echo "123456" >/etc/rsync-mongodb.pass'sudo chmod 600 /etc/rsync-mongodb.pass
sudo /bin/sh -c 'echo "fs.inotify.max_user_watches = 128000" >>/etc/sysctl.conf'sudo sysctl -p
sudo systemctl enable rsyncsudo systemctl start rsync
#!/bin/bashhost="192.168.1.200"src="/data"dst=mongodb@$host::mongodbname=mongodbconsole_log() {echo -e "$(get_date_time) ▶ $@"}/usr/bin/inotifywait -mrq --format="%T %w%f%e" --timefmt="%Y-%m-%d %H:%M:%S" -e modify,delete,create,attrib $src|while read files; doconsole_log "Testing connection: " 1>>/var/log/rsync-$name.log 2>&1nc -zvn $host 27017# connection error => safely syncif [[ $? -ne 0 ]]; then/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/rsync-mongodb.pass $src $dstconsole_log "${files} was rsynced" 1>>/var/log/rsync-$name.log 2>&1elseconsole_log "${files} not rsync, $host is alive" 1>>/var/log/rsync-$name.log 2>&1fidone
文章转载自耑辰随笔,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




