mongodb主从复制及副本集的部署

发布时间:2020-07-08 16:34:04 作者:webseven
来源:网络 阅读:1594

一.mongodb配置参数介绍:

[root@host11 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的配置文件:

[root@host11 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

[root@host11 tmp]#

[root@host11 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.启动主从进程;

[root@host11 tmp]#mkdir -pv /usr/local/mongodb/data/master

[root@host11 tmp]#mkdir -pv /usr/local/mongodb/data/slave

[root@host11 tmp]mongod -f /etc/mongod_master.conf 

[root@host11 tmp]mongod -f /etc/mongod_slave.conf 

3.检查主从状态:

[root@host11 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)"

}

[root@host11 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节点的配置文件:

[root@host11 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

[root@host11 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

[root@host11 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

[root@host11 tmp]# 

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

[root@host11 tmp]#mkdir -pv /usr/local/mongodb/data/node1

[root@host11 tmp]#mkdir -pv /usr/local/mongodb/data/node2

[root@host11 tmp]#mkdir -pv /usr/local/mongodb/data/node3

[root@host11 tmp]#mongod -f /etc/mongod_node1.conf 

[root@host11 tmp]#mongod -f /etc/mongod_node2.conf 

[root@host11 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主从及副本集群部署完成;


推荐阅读:
  1. 配置mongodb副本集
  2. MongoDB创建副本集

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

%d mong

上一篇:查看电脑有没有安装Python的方法

下一篇:mongodb删除所有为空字段的方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》