标签:
摘要:
前面介绍完了副本集的搭建、用户的管理、参数和日常操作的说明,那副本集搭建好该如何管理呢?现在来说明下副本集的日常查看和管理。
说明:
1)查看命令行参数:db.serverCmdLineOpts()
zjy:PRIMARY> db.serverCmdLineOpts() { "argv" : [ "mongod", "-f", "/etc/mongodb/mongodb_27017.conf" ], "parsed" : { "config" : "/etc/mongodb/mongodb_27017.conf", "diaglog" : 3, "net" : { "maxIncomingConnections" : 50, "port" : 27017, "unixDomainSocket" : { "pathPrefix" : "/tmp" } }, "processManagement" : { "fork" : true, "pidFilePath" : "/var/run/mongo_27017.pid" }, "replication" : { "replSet" : "zjy/127.0.0.1:27018" }, "storage" : { "dbPath" : "/usr/local/mongo1/", "mmapv1" : { "nsSize" : 16 } }, "systemLog" : { "destination" : "file", "logAppend" : true, "path" : "/var/log/mongodb/mongodb1.log" } }, "ok" : 1 }
2)查看副本集状态:rs.status()
zjy:PRIMARY> rs.status() { "set" : "zjy", #副本集名称 "date" : ISODate("2015-06-30T04:07:29.380Z"), #执行时间 "myState" : 1, "members" : [ #成员 { "_id" : 1, "name" : "127.0.0.1:27017", #成员名称 "health" : 1, "state" : 1, #成员状态,1:Primary "stateStr" : "PRIMARY", #状态描述 "uptime" : 27104, #副本集运行时间,Primary为MongoDB运行时间,单位秒 "optime" : Timestamp(1435301307, 10), "optimeDate" : ISODate("2015-06-26T06:48:27Z"),#最后一个操作发生的时间 "electionTime" : Timestamp(1435610150, 1), "electionDate" : ISODate("2015-06-29T20:35:50Z"),#最后选举时间 "configVersion" : 31300, "self" : true #执行该命令函数的成员 }, { "_id" : 2, "name" : "127.0.0.1:27018", "health" : 1, "state" : 2, #成员状态,2:Secondary "stateStr" : "SECONDARY", "uptime" : 27098, "optime" : Timestamp(1435301307, 10), "optimeDate" : ISODate("2015-06-26T06:48:27Z"), "lastHeartbeat" : ISODate("2015-06-30T04:07:28.589Z"), #最后一次收到其他成员心跳时间 "lastHeartbeatRecv" : ISODate("2015-06-30T04:07:27.784Z"), "pingMs" : 0, #心跳发送到达时间,根据其选择从哪个成员进行同步 "syncingTo" : "127.0.0.1:27017", #同步成员地址 "configVersion" : 31300 }, { "_id" : 3, "name" : "127.0.0.1:27019", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 27094, "optime" : Timestamp(1435301307, 10), "optimeDate" : ISODate("2015-06-26T06:48:27Z"), "lastHeartbeat" : ISODate("2015-06-30T04:07:28.521Z"), "lastHeartbeatRecv" : ISODate("2015-06-30T04:07:28.521Z"), "pingMs" : 0, "syncingTo" : "127.0.0.1:27017", "configVersion" : 31300 } ], "ok" : 1 }
3)添加、删除副本集成员:rs.add,rs.remove(),rs.reconfig()
1,添加成员 rs.add(‘127.0.0.1:27020‘) 在添加成员之前,需要在目标成员里加上repset参数。 2,添加复杂的成员: rs.add({"_id":4,"host":"192.168.200.252:27017","priority":0,"hidden":true,"slaveDelay":60}) 不会成为主节点(被动节点),隐藏节点、延迟60s同步数据。适用于当作备份; rs.add({"_id":4,"host":"192.168.200.252:27017","votes":0}) 没有选举权限的节点,及时只剩下单个服务器的副本集,也不会称谓Secondary rs.add({"_id":4,"host":"192.168.200.252:27017","buildIndexes":false}) 不会同步索引的创建 rs.add({"_id":4,"host":"192.168.200.252:27017","arbiterOnly":true}) 只选举,不会同步数据 以上都可以通过rs.reconfig()来实现。
3,删除成员 rs.remove(‘127.0.0.1:27020‘)
4)查看副本集的配置:rs.config()
zjy:PRIMARY> rs.config() { "_id" : "zjy", #副本集名称 "version" : 31300, "members" : [ #各成员的配置选项 { "_id" : 1, "host" : "127.0.0.1:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 3, "tags" : { }, "slaveDelay" : 0, "votes" : 1 }, ...
...
... ], "settings" : { "chainingAllowed" : false, "heartbeatTimeoutSecs" : 10, "getLastErrorModes" : { }, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : 0 } } }
5)修改任一Secondary的同步源:replSetSyncFrom,需要在Secondary上执行。
查看其中的一个Secondary的同步源 zjy:SECONDARY> db.adminCommand({"replSetGetStatus":1}).syncingTo 127.0.0.1:27017 # 本机是27019实例,暂时需要把这个实例从27018上同步:
zjy:SECONDARY> db.adminCommand({"replSetSyncFrom":"127.0.0.1:27018"}) { "syncFromRequested" : "127.0.0.1:27018", "prevSyncTarget" : "127.0.0.1:27017", "ok" : 1 } zjy:SECONDARY> db.adminCommand({"replSetGetStatus":1}).syncingTo 127.0.0.1:27018
也可以通过rs.status()来查看其同步源 zjy:SECONDARY> rs.status().syncingTo 127.0.0.1:27018
6)让任一Secondary进入维护模式:replSetMaintenance,必须在Secondary里的admin下运行,之后会进入RECOVERING状态。
在副本集上执行某个耗时的操作,会让成员进入recovering模式,不会有读请求发送给他,如何一个成员远落后主,可以强制让其进入维护模式。
zjy:SECONDARY> rs.status().members [ ...
... { "_id" : 3, "name" : "127.0.0.1:27019", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 39275, "optime" : Timestamp(1435301307, 10), "optimeDate" : ISODate("2015-06-26T06:48:27Z"), "syncingTo" : "127.0.0.1:27018", "configVersion" : 31300, "self" : true } ] zjy:SECONDARY> db.adminCommand({"replSetMaintenance":true}) { "ok" : 1 } zjy:RECOVERING> rs.status().members [ ...
...
{ "_id" : 3, "name" : "127.0.0.1:27019", "health" : 1, "state" : 3, "stateStr" : "RECOVERING", "uptime" : 39313, "optime" : Timestamp(1435301307, 10), "optimeDate" : ISODate("2015-06-26T06:48:27Z"), "syncingTo" : "127.0.0.1:27018", "maintenanceMode" : 1, "configVersion" : 31300, "self" : true } ] 看到127.0.0.1:27019 进入了RECOVERING
当进入维护模式之后,该节点的数据就不能读取了:
zjy:RECOVERING> db.aoe.find() Error: error: { "$err" : "not master or secondary; cannot currently read from this replSet member", "code" : 13436 }
退出维护模式:
zjy:RECOVERING> db.adminCommand({"replSetMaintenance":false}) { "ok" : 1 } zjy:SECONDARY> rs.status().members [ ...
... { "_id" : 3, "name" : "127.0.0.1:27019", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 39425, "optime" : Timestamp(1435301307, 10), "optimeDate" : ISODate("2015-06-26T06:48:27Z"), "syncingTo" : "127.0.0.1:27017", "configVersion" : 31300, "self" : true } ]
7)限制副本集自动寻找数据源的功能:chainingAllowed
由于配置好的副本集都是自动的对同步源进行分配,根据pingMS来寻找最新的数据源,可能某个Secondary是另一个Secondary的同步源。下面就可以关闭自动分配,同步源指的到Primary上
zjy:PRIMARY> rs.config().settings { "chainingAllowed" : true, "heartbeatTimeoutSecs" : 10, "getLastErrorModes" : { }, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : 0 } } zjy:PRIMARY> var cfg=rs.config() zjy:PRIMARY> cfg.settings.chainingAllowed true zjy:PRIMARY> cfg.settings.chainingAllowed=false false zjy:PRIMARY> cfg.settings.chainingAllowed false zjy:PRIMARY> rs.reconfig(cfg) { "ok" : 1 } zjy:PRIMARY> rs.config().settings { "chainingAllowed" : false, "heartbeatTimeoutSecs" : 10, "getLastErrorModes" : { }, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : 0 } }
取消自动数据源分配的功能,可以有效的防止复制链的出现。但在一定的程度上会加大对Primary的压力。
8)
标签:
原文地址:http://www.cnblogs.com/zhoujinyi/p/4614710.html