MongoDB管理与开发精要《红丸出品》21.4.2 管理维护Replica Sets之故障转移-LMLPHP

故障转移

复制集比传统的Master-Slave有改进的地方就是他可以进行故障的自动转移,如果我们停掉复制集中的一个成员,那么剩余成员会再自动选举出一个成员,做为主库,例如:

我们将28010这个主库停掉,然后再看一下复制集的状态

1、杀掉28010端口的MongoDB

[root@localhost bin]# ps aux|grep mongod

root      6706  1.6  6.9 463304  6168         Sl   21:49   0:26 /Apps/mongo/bin/mongod --replSet rs1 --keyFile /data/key/r0 --fork --port 28010

root      6733  0.4  6.7 430528  6044 ?        Sl   21:50   0:06 /Apps/mongo/bin/mongod --replSet rs1 --keyFile /data/key/r1 --fork --port 28011

root      6747  0.4  4.7 431548  4260 ?        Sl   21:50   0:06 /Apps/mongo/bin/mongod --replSet rs1 --keyFile /data/key/r2 --fork --port 28012

root      7019  0.0  0.7   5064   684 pts/2    S+   22:16   0:00 grep mongod

[root@localhost bin]# kill -9 6706

1、  查看复制集状态

[root@localhost bin]# ./mongo --port 28011

MongoDB shell version: 1.8.1

connecting to: 127.0.0.1:28011/test

rs1:SECONDARY> rs.status()

{

        "set" : "rs1",

        "date" : ISODate("2012-05-31T14:17:03Z"),

        "myState" : 2,

        "members" : [

                {

                        "_id" : 0,

                        "name" : "localhost:28010",

                        "health" : 0,

                        "state" : 1,

                        "stateStr" : "(not reachable/healthy)",

                        "uptime" : 0,

                        "optime" : {

                                "t" : 1338472279000,

                                "i" : 1

                        },

                        "optimeDate" : ISODate("2012-05-31T13:51:19Z"),

                        "lastHeartbeat" : ISODate("2012-05-31T14:16:42Z"),

                        "errmsg" : "socket exception"

                },

                {

                        "_id" : 1,

                        "name" : "localhost:28011",

                        "health" : 1,

                        "state" : 2,

                        "stateStr" : "SECONDARY",

                        "optime" : {

                                "t" : 1338472279000,

                                "i" : 1

                        },

                        "optimeDate" : ISODate("2012-05-31T13:51:19Z"),

                        "self" : true

                },

                {

                        "_id" : 2,

                        "name" : "localhost:28012",

                        "health" : 1,

                        "state" : 1,

                        "stateStr" : "PRIMARY",

                        "uptime" : 1528,

                        "optime" : {

                                "t" : 1338472279000,

                                "i" : 1

                        },

                        "optimeDate" : ISODate("2012-05-31T13:51:19Z"),

                        "lastHeartbeat" : ISODate("2012-05-31T14:17:02Z")

                }

        ],

        "ok" : 1

}

rs1:SECONDARY>

可以看到28010这个端口的MongoDB出现了异常,而系统自动选举了28012这个端口为主,所以这样的故障处理机制,能将系统的稳定性大大提高。
12-17 15:04