MongoDB主从

时间:2022-06-18 04:09:20

环境示例:

系统:Centos6.6x64
安装目录:/opt/
主:172.16.15.101
从:172.16.15.102

 1、下载安装:

# wget https://fastdl.mongodb.org/src/mongodb-src-r3.2.7.tar.gz?_ga=1.217384598.1880361485.1476164670
新版本: 
# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.6.9.tgz
# tar -xf mongodb-linux-x86_64-3.2.7.tgz
# mv mmongodb-linux-x86_64-3.2.7 /opt/mongodb

 2、mongodb环境变量配置

# cat /etc/profile
# export PATH=$PATH:/opt/mongodb/bin
# source /etc/profile

 3、iptables、selinux及内核设置:

1、iptables
# cat /etc/sysconfig/iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 28017 -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 27017 -j ACCEPT 2、关闭selinux (在系统优化前操作,重启生效) # sed "s/SELINUX=enables/SELINUX=disabled/g" /etc/sysconfig/selinux SELINUX=disabled 3、内核 # cat >>/etc/rc.d/rc.local <<HERE echo never > /sys/kernel/mm/transparent_hugepage/enabled echo never > /sys/kernel/mm/transparent_hugepage/defrag HERE

  4、创建文件存放目录:

# mkdir -p /opt/mongodb/{log,db,conf}

 主服务 :172.16.15.101

5、mongod.conf 参考配置;

# cat mongodb.conf

### ****
systemLog:
destination: file
logAppend: true
logRotate: rename
timeStampFormat: ctime
path: /opt/mongodb/log/mongod.log

storage:
dbPath: /opt/mongodb/db
journal:
enabled: true
processManagement:
fork: true # fork and run in background
pidFilePath: /opt/mongodb/mongod.pid # location of pidfile

net:
port: 27017
#bindIp: 127.0.0.1 # Listen to local interface only, comment to listen on all interfaces.
http:
enabled: true
JSONPEnabled: true
RESTInterfaceEnabled: true
setParameter:
enableLocalhostAuthBypass: false

master = true #
# source = 172.16.15.102 # 从服务地址不用配置

部分参数解释 // 附件有官方配置文件
--fork #后台daemon运行
--bind_ip #监听IP地址列表,以逗号分隔
--port #监听端口,默认27017
--setParameter enableLocalhostAuthBypass=0 #所有接口都需要认证
--pidfilepath #pid文件
--dbpath #db存放路径
--logpath #日志文件
--config #配置文件
--auth #启用认证
--httpinterface #启用web接口
--rest #rest api
--jsonp #json api

 从服务:172.16.15.102

6、mongod.conf 参考配置;

### ****
systemLog:
destination: file
logAppend: true
logRotate: rename
timeStampFormat: ctime
path: /opt/mongodb/log/mongod.log

storage:
dbPath: /opt/mongodb/db
journal:
enabled: true
processManagement:
fork: true # fork and run in background
pidFilePath: /opt/mongodb/mongod.pid # location of pidfile
net:
port: 27017
#bindIp: 127.0.0.1 # Listen to local interface only, comment to listen on all interfaces.
http:
enabled: true
JSONPEnabled: true
RESTInterfaceEnabled: true
setParameter:
enableLocalhostAuthBypass: false

slave = true #
source = 172.16.15.101 #

  7、从服务器启动

/opt/mongodb/bin/mongod --fork --slave --source 172.16.15.101:27017 --port 27017 --dbpath /opt/mongodb/db --logpath /opt/mongodb/log/mongodb.log

 8、启动检测mongod进程以及端口

# mongod --fork --httpinterface --rest --jsonp --setParameter enableLocalhostAuthBypass=0 --pidfilepath /opt/mongodb/mongod.pid --dbpath /opt/mongodb/db --logpath /opt/mongodb/log/mongod.log --logappend --logRotate rename --timeStampFormat ctime

 主要的配置文件日志进程端口启动就好;

9、主服务的启动:

/opt/mongodb/bin/mongod --fork --master --oplogSize=1024 --port 27017 --dbpath /opt/mongodb/db --logpath /opt/mongodb/log/mongodb.log

 进程检测:

# ps -ef|grep mongod
# netstat -tunlp|grep mongod

 10、/etc/init.d/mongod 自控脚本

# useradd -s /sbin/nologin -r mongod
# chown -R mongod: /opt/mongodb
# /etc/init.d/mongod
// * 这个附件有官方配置信息
# chmod  x /etc/init.d/mongod
// * 需要更改配置目录
# sed -i /CONFIGFILE=/i MONGOD="/opt/mongodb/bin/mongod" /etc/init.d/mongod
# sed -i /CONFIGFILE=/s:/etc/mongod.conf:/opt/mongodb/conf/mongod.conf:g /etc/init.d/mongod