mongodb主從複製及副本集的部署

一.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主從及副本集羣部署完成;


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章