码迷,mamicode.com
首页 > 数据库 > 详细

增加和减少mongodb复制集中的节点

时间:2016-07-23 23:00:42      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:

MongoDB Replica Sets不仅提供高可用性的解决方案,同时也提供负载均衡的解决方案,增减 Replica Sets节点在实际应用中非常普通。例如,当应用的读压力暴增时,3台节点的环境已不能满足需求,那么就需要增加一些节点将压力平均分配一下;当应用的压力小时,可以减少一些节点来减少硬件资源的成本,总之这是一个长期且持续的工作。

增加节点步骤

1) 配置并启动新节点,启用 28013 这个端口给新的节点

[root@node222 mongodb]# mkdir -p /data02/mongors/data/r3

[root@node222 mongodb]# echo " this is rs1 super secret key " > /data02/mongors/key/r3

[root@node222 mongodb]# chmod 600 /data02/mongors/key/r3

[root@node222 mongodb]# /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r3 --fork --port 28013 --dbpath /data02/mongors/data/r3 --logpath=/data02/mongors/log/r3.log --logappend

about to fork child process, waiting until server is ready for connections.

forked process: 17718

child process started successfully, parent exiting

[root@node222 mongodb]#

 

2) 在primary节点上执行命令添加此新节点到现有的 Replica Sets

rs1:PRIMARY> rs.add("localhost:28013");rs.add("localhost:28013");

{ "ok" : 1 }

rs1:PRIMARY> rs.status();rs.status();

{

        "set" : "rs1",

        "date" : ISODate("2016-07-22T10:26:06.175Z"),

        "myState" : 1,

        "term" : NumberLong(2),

        "heartbeatIntervalMillis" : NumberLong(2000),

        "members" : [

                {

                        "_id" : 0,

                        "name" : "localhost:28010",

                        "health" : 1,

                        "state" : 2,

                        "stateStr" : "SECONDARY",

                        "uptime" : 235,

                        "optime" : {

                                "ts" : Timestamp(1469183031, 1),

                                "t" : NumberLong(2)

                        },

                        "optimeDate" : ISODate("2016-07-22T10:23:51Z"),

                        "lastHeartbeat" : ISODate("2016-07-22T10:26:05.493Z"),

                        "lastHeartbeatRecv" : ISODate("2016-07-22T10:26:05.603Z"),

                        "pingMs" : NumberLong(0),

                        "syncingTo" : "localhost:28012",

                        "configVersion" : 2

                },

                {

                        "_id" : 1,

                        "name" : "localhost:28011",

                        "health" : 1,

                        "state" : 2,

                        "stateStr" : "SECONDARY",

                        "uptime" : 2749,

                        "optime" : {

                                "ts" : Timestamp(1469183031, 1),

                                "t" : NumberLong(2)

                        },

                        "optimeDate" : ISODate("2016-07-22T10:23:51Z"),

                        "lastHeartbeat" : ISODate("2016-07-22T10:26:05.493Z"),

                        "lastHeartbeatRecv" : ISODate("2016-07-22T10:26:05.603Z"),

                        "pingMs" : NumberLong(0),

                        "syncingTo" : "localhost:28012",

                        "configVersion" : 2

                },

                {

                        "_id" : 2,

                        "name" : "localhost:28012",

                        "health" : 1,

                        "state" : 1,

                        "stateStr" : "PRIMARY",

                        "uptime" : 2956,

                        "optime" : {

                                "ts" : Timestamp(1469183031, 1),

                                "t" : NumberLong(2)

                        },

                        "optimeDate" : ISODate("2016-07-22T10:23:51Z"),

                        "electionTime" : Timestamp(1469182400, 1),

                        "electionDate" : ISODate("2016-07-22T10:13:20Z"),

                        "configVersion" : 2,

                        "self" : true

                },

                {

                        "_id" : 3,

                        "name" : "localhost:28013",

                        "health" : 1,

                        "state" : 2,

                        "stateStr" : "SECONDARY",

                        "uptime" : 65,

                        "optime" : {

                                "ts" : Timestamp(1469177570, 1),

                                "t" : NumberLong(1)

                        },

                        "optimeDate" : ISODate("2016-07-22T08:52:50Z"),

                        "lastHeartbeat" : ISODate("2016-07-22T08:53:54.701Z"),

                        "lastHeartbeatRecv" : ISODate("2016-07-22T08:53:51.755Z"),

                        "pingMs" : NumberLong(0),

                        "configVersion" : 2

                }

        ],

        "ok" : 1

}

rs1:PRIMARY> rs.config();rs.config();

{

        "_id" : "rs1",

        "version" : 2,

        "protocolVersion" : NumberLong(1),

        "members" : [

                {

                        "_id" : 0,

                        "host" : "localhost:28010",

                        "arbiterOnly" : false,

                        "buildIndexes" : true,

                        "hidden" : false,

                        "priority" : 1,

                        "tags" : {

 

                        },

                        "slaveDelay" : NumberLong(0),

                        "votes" : 1

                },

                {

                        "_id" : 1,

                        "host" : "localhost:28011",

                        "arbiterOnly" : false,

                        "buildIndexes" : true,

                        "hidden" : false,

                        "priority" : 1,

                        "tags" : {

 

                        },

                        "slaveDelay" : NumberLong(0),

                        "votes" : 1

                },

                {

                        "_id" : 2,

                        "host" : "localhost:28012",

                        "arbiterOnly" : false,

                        "buildIndexes" : true,

                        "hidden" : false,

                        "priority" : 1,

                        "tags" : {

 

                        },

                        "slaveDelay" : NumberLong(0),

                        "votes" : 1

                },

                {

                        "_id" : 3,

                        "host" : "localhost:28013",

                        "arbiterOnly" : false,

                        "buildIndexes" : true,

                        "hidden" : false,

                        "priority" : 1,

                        "tags" : {

 

                        },

                        "slaveDelay" : NumberLong(0),

                        "votes" : 1

                }

        ],

        "settings" : {

                "chainingAllowed" : true,

                "heartbeatIntervalMillis" : 2000,

                "heartbeatTimeoutSecs" : 10,

                "electionTimeoutMillis" : 10000,

                "getLastErrorModes" : {

 

                },

                "getLastErrorDefaults" : {

                        "w" : 1,

                        "wtimeout" : 0

                },

                "replicaSetId" : ObjectId("5791ea00a9dbf7228bb1ec0c")

        }

}

rs1:PRIMARY>

 

 

3)验证数据是否已经同步过来了

# /usr/local/mongodb/bin/mongo -port 28013

> rs.slaveOK()

> db.c1.find()

 

删除节点步骤

下面将刚刚添加的两个新节点 28013 从复制集中去除掉,只需 rs.remove 指令就可以了,具体如下:

登录到primary 节点,执行下面的命令将28013 从复制集中去掉:

> rs.remove("localhsots:28013");

查看复制集状态,可以看到只有三个成员,原来的两个成员都已经成功去除了:

> rs.status()

rs1:PRIMARY> rs.remove("localhost:28013");rs.remove("localhost:28013");

{ "ok" : 1 }

rs1:PRIMARY> rs.conf() rs.conf()

{

        "_id" : "rs1",

        "version" : 3,

        "protocolVersion" : NumberLong(1),

        "members" : [

                {

                        "_id" : 0,

                        "host" : "localhost:28010",

                        "arbiterOnly" : false,

                        "buildIndexes" : true,

                        "hidden" : false,

                        "priority" : 1,

                        "tags" : {

 

                        },

                        "slaveDelay" : NumberLong(0),

                        "votes" : 1

                },

                {

                        "_id" : 1,

                        "host" : "localhost:28011",

                        "arbiterOnly" : false,

                        "buildIndexes" : true,

                        "hidden" : false,

                        "priority" : 1,

                        "tags" : {

 

                        },

                        "slaveDelay" : NumberLong(0),

                        "votes" : 1

                },

                {

                        "_id" : 2,

                        "host" : "localhost:28012",

                        "arbiterOnly" : false,

                        "buildIndexes" : true,

                        "hidden" : false,

                        "priority" : 1,

                        "tags" : {

 

                        },

                        "slaveDelay" : NumberLong(0),

                        "votes" : 1

                }

        ],

        "settings" : {

                "chainingAllowed" : true,

                "heartbeatIntervalMillis" : 2000,

                "heartbeatTimeoutSecs" : 10,

                "electionTimeoutMillis" : 10000,

                "getLastErrorModes" : {

 

                },

                "getLastErrorDefaults" : {

                        "w" : 1,

                        "wtimeout" : 0

                },

                "replicaSetId" : ObjectId("5791d785947160a5bdbd700b")

        }

}

rs1:PRIMARY>

 

增加和减少mongodb复制集中的节点

标签:

原文地址:http://www.cnblogs.com/iamqiu/p/5699674.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!