简介:
mongodb有两种类型的复制,第一种是同于MySQL的主从复制模式,第二种是复制集,提供了自动故障转移的主从复制集群。其中复制集没有固定的主节点,当一个主机的故障后从节点会重新“选举”出一个新的主节点,从而提高的系统的可用性。
1)节点信息
node1:172.16.35.4
node2:172.10.35.3
node3:172.10.35.5
2)各个节点之间确保时间同步
(3)各个节点确保iptables和selinux以关闭
二、在配置复制集时我们需要了解复制集的影响因素
(1)复制集(副本集)重新选举的影响条件
心跳信息(heartbeat)、优先级(priority)、optime(某成员节点最近一次应用本地oplog的时间戳)、网络连接、网络分区
(2)触发选举的事件:
①新副本集初始化;
②从节点联系不到主节点;
③主节点"下台"时;有以下原因会导致主节点"下台"
主节点收到setupDown()命令时会下台;
某从节点有更高的优先级且已经满足成为主节点的其他所有条件;
主节点无法联系到副本集的"多数方"
三、配置过程
(1)各个节点安装mongodb
准备yum源 name=MongoDB 2.6 Repository baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/ gpgcheck=0 enabled=1
# yum -y install mongodb-org-server mongodb-org-shell mongodb-org-tools \\每个节点都要安装
(2)创建mongodb数据存放目录
# mkdir -vp /data/mongodb/data/db0; chown -R mongod.mongod /mongodb \\每个节点都要创建,方法相同
(3)编辑mongodb配置文件:/etc/mongod.conf,修改内容如下
# base
port = 27017 #端口
maxConns = 800
filePermissions = 0700
fork = true #是否在后台运行
noauth = true
directoryperdb = true
dbpath = /data/mongodb/data/db0 #数据库路径
pidfilepath = /data/mongodb/data/db0/mongodb.pid #知道PID文件路径
journal = true
# security
nohttpinterface = true
rest = false
# log
logpath = /data/mongodb/log/db0/mongodb.log #日志文件路径
logRotate = rename
logappend = true #是否自动切割日志
slowms = 50
replSet = rs0 #最重要的一项,设置复制集的名称,这里设置为“rs0”
replIndexPrefetch=_id_only \\这里设置取回的索引,此次设置的是
"_id_only"
[root@node1 ~]# scp /etc/mongod.conf node2:/etc/ \\复制配置文件给node2节点一份 [root@node1 ~]# scp /etc/mongod.conf node3:/etc/ \\复制配置文件给node3节点一份
[root@node1 ~]# /etc/init.d/mongod start Starting mongod: [ OK ] [root@node2 ~]# /etc/init.d/mongod start Starting mongod: [ OK ] [root@node3 ~]# /etc/init.d/mongod start Starting mongod: [ OK ] (4)配置主节点 [root@node1 ~]# mongo \\登录到mongodb数据库 MongoDB shell version: 2.6.11 connecting to: test Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-use
> rs.initiate() \\初始化复制集
{
"info2"
:
"no configuration explicitly specified -- making one"
,
"me"
:
"node1.linux.com:27017"
,
"info"
:
"Config now saved locally. Should come online in about a minute."
,
"ok"
: 1
}
>
one:PRIMARY> \\初始化复制集后,提示符变成现在的样子
one:PRIMARY> rs.add(
"172.16.2.13"
) \\添加node2从节点
{
"ok"
: 1 }
one:PRIMARY> rs.add(
"172.16.2.14"
) \\添加node3从节点
{
"ok"
: 1 }
(5)配置从节点
[root@node2 ~]# mongo \\链接node2节点的mongodb MongoDB shell version: 2.6.11 connecting to: test one:SECONDARY> rs.slaveOk() \\启动node2从节点 [root@node3 ~]# mongo \\链接node2节点的mongodb MongoDB shell version: 2.6.11 connecting to: test one:SECONDARY> rs.slaveOk() \\启动node3从节点 (6)查看各个节点信息: 2016-01-06T16:28:27.583+0800 I CONTROL [initandlisten] rs0:PRIMARY> rs.status(); { "set" : "rs0", "date" : ISODate("2016-01-08T07:29:40.119Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "172.10.35.4:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 169273, "optime" : Timestamp(1452234160, 1), "optimeDate" : ISODate("2016-01-08T06:22:40Z"), "electionTime" : Timestamp(1452070157, 1), "electionDate" : ISODate("2016-01-06T08:49:17Z"), "configVersion" : 142612, "self" : true }, { "_id" : 1, "name" : "172.10.35.5:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 168022, "optime" : Timestamp(1452234160, 1), "optimeDate" : ISODate("2016-01-08T06:22:40Z"), "lastHeartbeat" : ISODate("2016-01-08T07:29:38.747Z"), "lastHeartbeatRecv" : ISODate("2016-01-08T07:29:38.174Z"), "pingMs" : 0, "syncingTo" : "172.10.35.4:27017", "configVersion" : 142612 }, { "_id" : 2, "name" : "172.10.35.3:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 167987, "optime" : Timestamp(1452234160, 1), "optimeDate" : ISODate("2016-01-08T06:22:40Z"), "lastHeartbeat" : ISODate("2016-01-08T07:29:38.706Z"), "lastHeartbeatRecv" : ISODate("2016-01-08T07:29:38.717Z"), "pingMs" : 0, "syncingTo" : "172.10.35.4:27017", "configVersion" : 142612 } ], "ok" : 1 } rs0:PRIMARY>