mongodb主从复制及备份集的部署

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mongodb主从复制及备份集的部署相关的知识,希望对你有一定的参考价值。

一.mongodb配置参数介绍:

[[email protected] tmp]# cat  /etc/mongod.conf 


#以守护进程的方式运行

fork=true


#设置db的路径

dbpath=/usr/local/mongodb/data/db


#设置日志路径

logpath=/usr/local/mongodb/data/log/mongod.log

#logappend:写日志的模式:设置为true为追加。默认是覆盖。如果未指定此设置,启动时MongoDB的将覆盖现有的日志文件。

logappend=true


#设置端口号

port=27017


#在服务的端口号加上1000的端口上启动一个web服务器

rest=true


#打开权限控制体系

#auth=true

二.部署mongodb主从复制:

1.创建master和slave的配置文件:

[[email protected] tmp]# cat /etc/mongod_master.conf 

logpath=/usr/local/mongodb/data/log/mongod_master.log

logappend=true

fork=true

dbpath=/usr/local/mongodb/data/master

port=10000

rest=true

master=true

#auth=true

#slaveok=true

[[email protected] tmp]#

[[email protected] tmp]# cat /etc/mongod_slave.conf

logpath=/usr/local/mongodb/data/log/mongod_slave.log

logappend=true

fork=true

dbpath=/usr/local/mongodb/data/slave

port=10001

rest=true

slave=true

#auth=true

source=127.0.0.1:10000

2.启动主从进程;

[[email protected] tmp]#mkdir -pv /usr/local/mongodb/data/master

[[email protected] tmp]#mkdir -pv /usr/local/mongodb/data/slave

[[email protected] tmp]mongod -f /etc/mongod_master.conf 

[[email protected] tmp]mongod -f /etc/mongod_slave.conf 

3.检查主从状态:

[[email protected] tmp]# mongo localhost:10000

> db.isMaster();

{

"ismaster" : true,

"maxBsonObjectSize" : 16777216,

"maxMessageSizeBytes" : 48000000,

"maxWriteBatchSize" : 1000,

"localTime" : ISODate("2016-02-28T11:46:54.199Z"),

"maxWireVersion" : 4,

"minWireVersion" : 0,

"ok" : 1

}

> db.printReplicationInfo();

configured oplog size:   990MB

log length start to end: 23821secs (6.62hrs)

oplog first event time:  Sun Feb 28 2016 13:05:17 GMT+0800 (CST)

oplog last event time:   Sun Feb 28 2016 19:42:18 GMT+0800 (CST)

now:                     Sun Feb 28 2016 19:42:20 GMT+0800 (CST)

> db.printSlaveReplicationInfo();

local.sources is empty; is this db a --slave?

> db.getReplicationInfo();

{

"logSizeMB" : 990,

"usedMB" : 0.14,

"timeDiff" : 23871,

"timeDiffHours" : 6.63,

"tFirst" : "Sun Feb 28 2016 13:05:17 GMT+0800 (CST)",

"tLast" : "Sun Feb 28 2016 19:43:08 GMT+0800 (CST)",

"now" : "Sun Feb 28 2016 19:43:18 GMT+0800 (CST)"

}

[[email protected] tmp]# mongo localhost:10001

> db.printReplicationInfo();

this is a slave, printing slave replication info.

source: 127.0.0.1:10000

syncedTo: Sun Feb 28 2016 19:44:18 GMT+0800 (CST)

4 secs (0 hrs) behind the freshest member (no primary available at the moment)


三.备份集的部署:

1.创建各node节点的配置文件:

[[email protected] tmp]# cat /etc/mongod_node1.conf 

logpath=/usr/local/mongodb/data/log/mongod_node1.log

logappend=true

fork=true

dbpath=/usr/local/mongodb/data/node1

port=20001

rest=true

#auth=true

#slaveok=true

replSet=myrepl

[[email protected] tmp]# cat /etc/mongod_node2.conf 

logpath=/usr/local/mongodb/data/log/mongod_node2.log

logappend=true

fork=true

dbpath=/usr/local/mongodb/data/node2

port=20002

rest=true

#auth=true

#slaveok=true

replSet=myrepl

[[email protected] tmp]# cat /etc/mongod_node3.conf 

logpath=/usr/local/mongodb/data/log/mongod_node3.log

logappend=true

fork=true

dbpath=/usr/local/mongodb/data/node3

port=20003

rest=true

#auth=true

#slaveok=true

replSet=myrepl

[[email protected] tmp]# 

2.配置并启动各node服务;

[[email protected] tmp]#mkdir -pv /usr/local/mongodb/data/node1

[[email protected] tmp]#mkdir -pv /usr/local/mongodb/data/node2

[[email protected] tmp]#mkdir -pv /usr/local/mongodb/data/node3

[[email protected] tmp]#mongod -f /etc/mongod_node1.conf 

[[email protected] tmp]#mongod -f /etc/mongod_node2.conf 

[[email protected] tmp]#mongod -f /etc/mongod_node2.conf 

登录mongod:

#设置备份集变量:

> config = { _id:"myrepl", members:[{_id:0,host:"127.0.0.1:20001"},{_id:1,host:"127.0.0.1:20002"},{_id:2,host:"127.0.0.1:20003"}]};

#初始化副本集配置

> rs.initiate(config);


3.检查各node节点状态;

#查看副本集的状态

> rs.status();

#删除节点:

rs.remove("127.0.0.1:20001");

rs.status();

#添加节点:

rs.add("127.0.0.1:20001");

rs.status();

四.至此mongo主从及备份集群部署完成;


本文出自 “webseven” 博客,请务必保留此出处http://webseven.blog.51cto.com/4388012/1745849

以上是关于mongodb主从复制及备份集的部署的主要内容,如果未能解决你的问题,请参考以下文章

Linux安装MongoDB双机热备份(主从复制)

MongoDB复制集 (主从复制)

部署MongoDB复制集(主从复制读写分离高可用)

mongodb4.0.2 复制集主从部署

MongoDB主从复制以及副本集的配置和使用

MongoDB的主从复制和副本集