1)命令行的查看命令:db.serverCmdLineOpts()
点击(此处)折叠或打开
arps:SECONDARY> db.serverCmdLineOpts() { "argv" : [ "/opt/software/mongodb-linux-x86_64-3.0.1/bin/mongod",//启动参数mongod "-f", "/opt/software/mongodb-linux-x86_64-3.0.1/bin/mongodb.conf" ],//参数配置文件 "parsed" : { "config" : "/opt/software/mongodb-linux-x86_64-3.0.1/bin/mongodb.conf", "net" : { "port" : 27017 //端口 }, "processManagement" : { "fork" : true, "pidFilePath" : "/opt/data/mongodb/mongodb.pid" //进程文件 }, "replication" : { "oplogSizeMB" : 10000, //oplog日志大小 "replSet" : "arps" //副本集的名称arps }, "storage" : { "dbPath" : "/opt/data/mongodb", //数据存储的位置 "directoryPerDB" : true, "mmapv1" : { "preallocDataFiles" : false } }, "systemLog" : { "destination" : "file", "logAppend" : true, "path" : "/opt/data/mongodb/log/mongodb.log" //系统日志的位置 } }, "ok" : 1 } 2)查看副本集状态的命令:rs.status()点击(此处)折叠或打开
arps:SECONDARY> rs.status() { "set" : "arps", //副本集名称 "date" : ISODate("2017-12-22T06:12:21.904Z"), //执行时间 "myState" : 2, "syncingTo" : "172.17.4.37:27017", //同步源:172.17.4.37:27017 "members" : [ //成员 { "_id" : 0, "name" : "172.17.4.37:27017", "health" : 1, //健康状态值,1为UP,0为down "state" : 2, //成员状态值,2为secondary "stateStr" : "SECONDARY", "uptime" : 12354, "optime" : Timestamp(1513923135, 3), //最近一次更改数据库的时间。 "optimeDate" : ISODate("2017-12-22T06:12:15Z"), //最后一个操作发生的时间 "lastHeartbeat" : ISODate("2017-12-22T06:12:20.999Z"), //最后一次心跳接收的时间值 "lastHeartbeatRecv" : ISODate("2017-12-22T06:12:20.999Z"), //最近心跳的发送到达的时间值 "pingMs" : 0, "syncingTo" : "172.17.4.38:27017", "configVersion" : 1 }, { "_id" : 1, "name" : "172.17.4.38:27017", "health" : 1, "state" : 1, //成员状态值,1位primary "stateStr" : "PRIMARY", "uptime" : 12354, "optime" : Timestamp(1513923135, 3), "optimeDate" : ISODate("2017-12-22T06:12:15Z"), "lastHeartbeat" : ISODate("2017-12-22T06:12:21.805Z"), "lastHeartbeatRecv" : ISODate("2017-12-22T06:12:20.956Z"), "pingMs" : 1, "electionTime" : Timestamp(1506330005, 1), "electionDate" : ISODate("2017-09-25T09:00:05Z"), "configVersion" : 1 }, { "_id" : 2, "name" : "172.17.4.39:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 12356, "optime" : Timestamp(1513923135, 3), "optimeDate" : ISODate("2017-12-22T06:12:15Z"), "syncingTo" : "172.17.4.37:27017", "configVersion" : 1, "self" : true } ], "ok" : 1 } mongod实例每隔两秒就会向其他成员发送一个心跳包,并且通过rs.status()中返回的成员的health来判断成员的状态。如果primary节点不可用了,那么复制集中的所有secondary节点都会触发一次选举操作。选出新的primary节点。如果secondary节点有多个,则会选举拥有最新oplog时间戳记录的或者有较高权限的节点成为primary3)添加、删除副本集成员:rs.add,rs.addArb(),rs.remove(),rs.reconfig()点击(此处)折叠或打开
1)添加成员 rs.add('172.17.4.39:27017') //在添加成员之前,需要在目标成员里加上repset参数。 2)添加选举节点,不会同步数据 rs.add({,"host":"192.168.200.252:27017","arbiterOnly":true}) rs.addArb() 3)修改配置 通过rs.reconfig()来实现。 4)删除成员 rs.remove('172.17.4.39:27017') 4)副本集的配置:rs.config()点击(此处)折叠或打开
arps:PRIMARY> rs.config() { "_id" : "arps", "version" : 3, "members" : [ { "_id" : 0, "host" : "172.17.4.37:27017",//主机名 "arbiterOnly" : false, //是否为选举节点 "buildIndexes" : true, //是否建立索引 "hidden" : false, //是否隐藏 "priority" : 1, //优先级 "tags" : { }, "slaveDelay" : 0, "votes" : 1 }, { "_id" : 1, "host" : "172.17.4.38:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 }, { "_id" : 2, "host" : "172.17.4.39:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 } ], "settings" : { "chainingAllowed" : true, //自动寻找数据源功能 "heartbeatTimeoutSecs" : 10, //心跳连接超时时间 "getLastErrorModes" : { }, "getLastErrorDefaults" : { //write concern写入安全机制 "w" : 1, //1为(应答时写入) //确认写入操作已经发送到指定的单个mongodb实例或者副本集的Primary "wtimeout" : 0 } } } 5.Secondary进入维护模式:replSetMaintenance 如何让一个成员远落后于Primary的,进入维护模式点击(此处)折叠或打开
arps:RECOVERING> db.adminCommand({"replSetMaintenance":true}) //进入维护模式 arps:RECOVERING> rs.status().members [ { "_id" : 0, "name" : "172.17.4.37:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 15241, "optime" : Timestamp(1513926020, 4), "optimeDate" : ISODate("2017-12-22T07:00:20Z"), "lastHeartbeat" : ISODate("2017-12-22T07:00:28.772Z"), "lastHeartbeatRecv" : ISODate("2017-12-22T07:00:28.771Z"), "pingMs" : 0, "syncingTo" : "172.17.4.38:27017", "configVersion" : 3 }, { "_id" : 1, "name" : "172.17.4.38:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 15241, "optime" : Timestamp(1513926020, 4), "optimeDate" : ISODate("2017-12-22T07:00:20Z"), "lastHeartbeat" : ISODate("2017-12-22T07:00:28.772Z"), "lastHeartbeatRecv" : ISODate("2017-12-22T07:00:26.849Z"), "pingMs" : 0, "electionTime" : Timestamp(1506330005, 1), "electionDate" : ISODate("2017-09-25T09:00:05Z"), "configVersion" : 3 }, { "_id" : 2, "name" : "172.17.4.39:27017", "health" : 1, "state" : 3, "stateStr" : "RECOVERING", //进入维护模式,会强制进入RECOVERING状态,不提供服务 "uptime" : 15243, "optime" : Timestamp(1513926020, 4), "optimeDate" : ISODate("2017-12-22T07:00:20Z"), "syncingTo" : "172.17.4.38:27017", "maintenanceMode" : 1, "configVersion" : 3, "self" : true } ] arps:RECOVERING> db.adminCommand({"replSetMaintenance":false}) //退出维护模式 6.主节点变成备份节点:rs.stepDown(time),rs.freeze(time)rs.stepDown(time) 可以让主节点退成备份节点,timie单位是秒,默认60s。60s内主被副本集的其他成员获得,时间到后,会重新进行选举,一般都会重新成为主(优先级)。rs.freeze(time):阻止选举,始终出于备份节点状态。比如主节点需要做一些维护,不希望其他成员选举为主节点,可以在每个备份节点上执行。强制他们出于备份节点状态。点击(此处)折叠或打开
arps:PRIMARY> rs.stepDown(30) //退出主节点,成为secondary节点 ....... ....... 2017-12-22T15:05:52.110+0800 I NETWORK trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed 2017-12-22T15:05:52.111+0800 I NETWORK reconnect 127.0.0.1:27017 (127.0.0.1) ok arps:SECONDARY> arps:SECONDARY> rs.status().members [ { "_id" : 0, "name" : "172.17.4.37:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", //升级为主节点 "uptime" : 15797, "optime" : Timestamp(1513926601, 3), "optimeDate" : ISODate("2017-12-22T07:10:01Z"), "lastHeartbeat" : ISODate("2017-12-22T07:09:43.092Z"), "lastHeartbeatRecv" : ISODate("2017-12-22T07:09:43.130Z"), "pingMs" : 0, "electionTime" : Timestamp(1513926357, 1), "electionDate" : ISODate("2017-12-22T07:05:57Z"), "configVersion" : 3 }, { "_id" : 1, "name" : "172.17.4.38:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", //退位至备份节点 "uptime" : 15797, "optime" : Timestamp(1513926601, 3), "optimeDate" : ISODate("2017-12-22T07:10:01Z"), "lastHeartbeat" : ISODate("2017-12-22T07:09:43.092Z"), "lastHeartbeatRecv" : ISODate("2017-12-22T07:09:43.245Z"), "pingMs" : 0, "syncingTo" : "172.17.4.37:27017", "configVersion" : 3 }, { "_id" : 2, "name" : "172.17.4.39:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 15799, "optime" : Timestamp(1513926601, 3), "optimeDate" : ISODate("2017-12-22T07:10:01Z"), "syncingTo" : "172.17.4.37:27017", "configVersion" : 3, "self" : true } ] 7.复制延迟状态查看db.printReplicationInfo()和db.printSlaveReplicationInfo()点击(此处)折叠或打开
arps:PRIMARY> db.printReplicationInfo() //主上面执行的复制状态信息 configured oplog size: 10000.003845214844MB log length start to end: 31637996secs (8788.33hrs) oplog first event time: Wed Dec 21 2016 10:54:05 GMT+0800 (CST) oplog last event time: Fri Dec 22 2017 15:14:01 GMT+0800 (CST) now: Fri Dec 22 2017 15:14:01 GMT+0800 (CST) arps:SECONDARY> db.printSlaveReplicationInfo() //从上面执行的复制延迟信息 source: 172.17.4.38:27017 syncedTo: Fri Dec 22 2017 15:14:01 GMT+0800 (CST) 0 secs (0 hrs) behind the primary source: 172.17.4.39:27017 syncedTo: Fri Dec 22 2017 15:14:01 GMT+0800 (CST) 0 secs (0 hrs) behind the primary