MongoDB架构篇-Replica Set 副本集-增加删除节点等

MongoDB架构篇 ReplicaSets复制(副本集)增加删除节点: 

 --命令解释

mongod--replSet rs1(小组) --keyFile=e:/key1(密钥) --port 20001--dbpath=F:\DingSai\Mongodb\replication\rs1\data --logpath=F:\DingSai\Mongodb\replication\rs1\logs\mongodb.log 

开rs1

mongod--replSet rs1  --port 20001--dbpath=F:\DingSai\Mongodb\replication\rs1\data --logpath=F:\DingSai\Mongodb\replication\rs1\logs\mongodb.log

 

开rs2

mongod--replSet rs1  --port 20002--dbpath=F:\DingSai\Mongodb\replication\rs2\data --logpath=F:\DingSai\Mongodb\replication\rs2\logs\mongodb.log

  

开rs3

mongod--replSet rs1  --port 20003--dbpath=F:\DingSai\Mongodb\replication\rs3\data --logpath=F:\DingSai\Mongodb\replication\rs3\logs\mongodb.log

  

开rs4

mongod--replSet rs1  --port 20004--dbpath=F:\DingSai\Mongodb\replication\rs4\data  --logpath=F:\DingSai\Mongodb\replication\rs4\logs\mongodb.log

 

开rs5

mongod--replSet rs1  --port 20005--dbpath=F:\DingSai\Mongodb\replication\rs5\data --logpath=F:\DingSai\Mongodb\replication\rs5\logs\mongodb.log

 

先打开前三台服务

登录primary3号主机 mongo localhost:20003

--编写配置清单  

config_rs1={_id:"rs1",members:[{_id:0,host:"localhost:20001",priority:1},{_id:1,host:"localhost:20002",priority:2},{_id:2,host:"localhost:20003",priority:4}]} 

priority优先级越大,优先级越高;3号机是主机

 --初始化配置清单 

rs.initiate(config_rs1); 

rs.isMaster();查看当前服务器是否是主机

rs.status();查看状态

                {

                        "_id" : 1,

                        "name" :"localhost:20002",

                        "health" : 1,

                        "state" : 2,

                        "stateStr" :"SECONDARY",                     --用户描述服务器状态的字符串。有SECONDARY,PRIMARY,RECOVERING等

                        "uptime" :1039,

                        "optime" :Timestamp(1422583902, 1),

                        "optimeDate": ISODate("2015-01-30T02:11:42Z"),

                       "lastHeartbeat" :ISODate("2015-01-30T02:14:42Z"), --最后心跳时间

                       "lastHeartbeatRecv" :ISODate("2015-01-30T02:14:42Z"),--最后心跳时间接收

                        "pingMs" :0,                                      --心跳平均时间

                        "syncingTo" :"localhost:20003"                   --同步来源

                },

                {

                        "_id" : 2,

                        "name" :"localhost:20003",

                        "health" : 1,

                        "state" : 1,

                        "stateStr" :"PRIMARY",

                        "uptime" :1971,

                        "optime" :Timestamp(1422583902, 1),

                        "optimeDate": ISODate("2015-01-30T02:11:42Z"),

                       "electionTime" : Timestamp(1422583052, 1),

                       "electionDate" : ISODate("2015-01-30T01:57:32Z"),

                        "self" : true

                }

        ], 

在主服务器 priortity3服务器插入数据

db.c1.insert({name:"user1"})

db.c1.insert({name:"user2"})

db.c1.insert({name:"user3"}) 

secondary服务器登陆后 执行

 rs.slaveOk()--需要确认有读的权限

db.c1.find();--从服务器执行slaveOk后就可以查看。

但是从服务器不能插入数据

db.c1.insert({name:"user1"});--"errmsg": "not master" 

搭建Replica Set完成。

  

增加普通节点设置:

开rs4

mongod--replSet rs1  --port 20004--dbpath=F:\DingSai\Mongodb\replication\rs4\data --logpath=F:\DingSai\Mongodb\replication\rs4\logs\mongodb.log 

登录到

mongolocalhost:20004;

发现 4节点不存在与集群中; 

登录到primary3机

rs.add("localhost:20004");--增加节点 

登录到secondary4机器

rs.slaveOk();授权访问

db.c1.find();发现数据已同步过来。

 

关闭普通节点设置: 

关闭secondary2机器

登录到primary3机器

rs.status();--发现映射关系还存在

db.c1.insert({name:"user4"}) 

再次开启secondary2机器

mongod--replSet rs1  --port 20002 --dbpath=F:\DingSai\Mongodb\replication\rs2\data --logpath=F:\DingSai\Mongodb\replication\rs2\logs\mongodb.log

 登录到secondary2机器

db.c1.find();--发现新数据已存在(从primary主机同步过来)

  

关闭primary主机:

关闭primary3节点 

随便登录到一台机器

rs.slaveOk();--授权

rs.status();

                {

                        "_id" : 0,

                        "name" :"localhost:20001",

                        "health" : 1,

                        "state" : 2,

                        "stateStr" :"SECONDARY",

                        "uptime" :707,

                        "optime" :Timestamp(1422585325, 1),

                        "optimeDate": ISODate("2015-01-30T02:35:25Z"),

                       "lastHeartbeat" : ISODate("2015-01-30T02:41:03Z"),

                       "lastHeartbeatRecv" : ISODate("2015-01-30T02:41:03Z"),

                        "pingMs" : 0,

                       "lastHeartbeatMessage" : "syncing to:localhost:20002",

                        "syncingTo" :"localhost:20002"

                },

                {

                        "_id" : 1,

                        "name" :"localhost:20002",

                        "health" : 1,

                        "state" : 1,

                        "stateStr" :"PRIMARY",--原2号机变成primary

                        "uptime" :308,

                        "optime" :Timestamp(1422585325, 1),

                        "optimeDate": ISODate("2015-01-30T02:35:25Z"),

                       "lastHeartbeat" : ISODate("2015-01-30T02:41:03Z"),

                       "lastHeartbeatRecv" : ISODate("2015-01-30T02:41:03Z"),

                        "pingMs" : 0,

                       "electionTime" : Timestamp(1422585613, 1),

                       "electionDate" : ISODate("2015-01-30T02:40:13Z")

                },

                {

                        "_id" : 2,

                        "name" :"localhost:20003",

                        "health" : 0,

                        "state" : 8,

                        "stateStr" :"(not reachable/healthy)",--原primary3号机不可达

                        "uptime" : 0,

                        "optime" :Timestamp(1422585325, 1),

                        "optimeDate": ISODate("2015-01-30T02:35:25Z"),

                       "lastHeartbeat" : ISODate("2015-01-30T02:40:57Z"),

                        "lastHeartbeatRecv": ISODate("2015-01-30T02:40:05Z"),

                        "pingMs" : 0

                },

 

原primary3号机不可达,原secondary2号机变成primary。

mongolocalhost:20002

db.c1.insert({name:"user5"});  

重新打开原primary 3号机

mongod--replSet rs1  --port 20003--dbpath=F:\DingSai\Mongodb\replication\rs3\data --logpath=F:\DingSai\Mongodb\replication\rs3\logs\mongodb.log

 登录到原primary 3号机

rs.isMaster()

{

        "setName" : "rs1",

        "setVersion" : 2,

        "ismaster" : true,   --重启后又变回成primary。

        "secondary" : false,

        "hosts" : [

                "localhost:20003",

                "localhost:20004",

                "localhost:20002",

                "localhost:20001"

        ],

        "primary" :"localhost:20003",

        "me" :"localhost:20003",

        "maxBsonObjectSize" : 16777216,

        "maxMessageSizeBytes" :48000000,

        "maxWriteBatchSize" : 1000,

        "localTime" :ISODate("2015-01-30T02:46:45.978Z"),

        "maxWireVersion" : 2,

        "minWireVersion" : 0,

        "ok" : 1

}

 原primary3号机又成为primary, 2号机又变回 sencondary2。


总结:
1.新增的所有节点都会从primary主机中同步数据。
2.primary节点坏掉,会有次优先级的机器替代它。
3.primary节点恢复,立刻变回primary。


rs.slaveOk();--sencondary机器授权查询

rs.isMaster()--查看副本集状态

rs.status()--查看副本集状态

rs.remove("localhost:20001");--删除节点 在主服务器执行

rs.add("localhost:20001");--增加节点  

rs.initiate(config_rs1);--Initializes a new replica set.
rs.reconfig(config_rs1);--Re-configures a replica set by applying a new replica set configuration object.


rs.的命令官网

http://docs.mongodb.org/manual/reference/method/js-replication/ 

Replica Set官网说明:

http://docs.mongodb.org/manual/core/replica-set-architecture-three-members/

 

你可能感兴趣的:(数据库)