Mongodb副本集基本操作

来源:互联网 发布:java完全自学第一步 编辑:程序博客网 时间:2024/06/06 04:04

Mongodb副本集基本操作

实验环境

操作系统:CentOS Linux release 7.3.1611 (Core)数据库系统: Mongodb 3.4.9-1.el7mongo1 IP: 192.168.58.129mongo2 IP: 192.168.58.130mongo3 IP: 192.168.58.131

查看副本集状态

可以查看集群详细运行状态

在主节点主节点:

sbc:PRIMARY> rs.status(){    "set" : "sbc",  #副本集名称    "date" : ISODate("2017-11-24T02:14:18.056Z"),   #执行时间    "myState" : 1,    "term" : NumberLong(3),     "heartbeatIntervalMillis" : NumberLong(2000),    "optimes" : {        "lastCommittedOpTime" : {            "ts" : Timestamp(1511489649, 1),            "t" : NumberLong(3)        },        "appliedOpTime" : {            "ts" : Timestamp(1511489649, 1),            "t" : NumberLong(3)        },        "durableOpTime" : {            "ts" : Timestamp(1511489649, 1),            "t" : NumberLong(3)        }    },    "members" : [        {            "_id" : 0,            "name" : "192.168.58.129:27017",    #成员名称            "health" : 1,            "state" : 1,    #成员状态            "stateStr" : "PRIMARY", #状态描述            "uptime" : 23,  #副本集运行时间,单位秒            "optime" : {                "ts" : Timestamp(1511489649, 1),    #最近一次更改数据库的时间/每秒执行操作数据库的次数                "t" : NumberLong(3)            },            "optimeDate" : ISODate("2017-11-24T02:14:09Z"), #最后一个操作发生时间            "electionTime" : Timestamp(1511489647, 1),            "electionDate" : ISODate("2017-11-24T02:14:07Z"),   最后选举时间            "configVersion" : 1,            "self" : true   #执行该命令的成员        },        {            "_id" : 1,            "name" : "192.168.58.130:27017",            "health" : 1,            "state" : 2,            "stateStr" : "SECONDARY",            "uptime" : 16,            "optime" : {                "ts" : Timestamp(1511489649, 1),                "t" : NumberLong(3)            },            "optimeDurable" : {                "ts" : Timestamp(1511489649, 1),                "t" : NumberLong(3)            },            "optimeDate" : ISODate("2017-11-24T02:14:09Z"),            "optimeDurableDate" : ISODate("2017-11-24T02:14:09Z"),            "lastHeartbeat" : ISODate("2017-11-24T02:14:17.687Z"),            "lastHeartbeatRecv" : ISODate("2017-11-24T02:14:17.443Z"),            "pingMs" : NumberLong(0),            "syncingTo" : "192.168.58.129:27017",            "configVersion" : 1        },        {            "_id" : 2,            "name" : "192.168.58.131:27017",            "health" : 1,            "state" : 2,            "stateStr" : "SECONDARY",            "uptime" : 11,            "optime" : {                "ts" : Timestamp(1511489649, 1),                "t" : NumberLong(3)            },            "optimeDurable" : {                "ts" : Timestamp(1511489649, 1),                "t" : NumberLong(3)            },            "optimeDate" : ISODate("2017-11-24T02:14:09Z"),            "optimeDurableDate" : ISODate("2017-11-24T02:14:09Z"),            "lastHeartbeat" : ISODate("2017-11-24T02:14:17.687Z"),            "lastHeartbeatRecv" : ISODate("2017-11-24T02:14:17.243Z"),            "pingMs" : NumberLong(0),            "syncingTo" : "192.168.58.129:27017",            "configVersion" : 1        }    ],    "ok" : 1}

查看副本集配置

可以查看集群详细运行状态

在主节点执行

sbc:PRIMARY> rs.conf(){    "_id" : "sbc",    "version" : 3,    "protocolVersion" : NumberLong(1),    "members" : [        {            "_id" : 0,            "host" : "192.168.58.129:27017",            "arbiterOnly" : false,            "buildIndexes" : true,            "hidden" : false,            "priority" : 1,            "tags" : {            },            "slaveDelay" : NumberLong(0),            "votes" : 1        },        {            "_id" : 1,            "host" : "192.168.58.130:27017",            "arbiterOnly" : false,            "buildIndexes" : true,            "hidden" : false,            "priority" : 1,            "tags" : {            },            "slaveDelay" : NumberLong(0),            "votes" : 1        },        {            "_id" : 2,            "host" : "192.168.58.131:27017",            "arbiterOnly" : false,            "buildIndexes" : true,            "hidden" : false,            "priority" : 1,            "tags" : {            },            "slaveDelay" : NumberLong(0),            "votes" : 1        }    ],    "settings" : {        "chainingAllowed" : true,        "heartbeatIntervalMillis" : 2000,        "heartbeatTimeoutSecs" : 10,        "electionTimeoutMillis" : 10000,        "catchUpTimeoutMillis" : 60000,        "getLastErrorModes" : {        },        "getLastErrorDefaults" : {            "w" : 1,            "wtimeout" : 0        },        "replicaSetId" : ObjectId("5a1288509b4aa41f86920d04")    }}

配置从节点添加可读

默认情况下,从节点是不可读的,当执行查询命令时会报错not master and slaveOk=false

sbc:SECONDARY> db.test.find()Error: error: {    "ok" : 0,    "errmsg" : "not master and slaveOk=false",    "code" : 13435,    "codeName" : "NotMasterNoSlaveOk"}

开启可读后查询,可正常返回数据

sbc:SECONDARY> rs.slaveOk()sbc:SECONDARY> db.test.find(){ "_id" : ObjectId("5a1646a3e63ee6dcc81bebcc"), "name" : "test0", "age" : 123 }{ "_id" : ObjectId("5a1646a3e63ee6dcc81bebcd"), "name" : "test1", "age" : 123 }{ "_id" : ObjectId("5a1646a3e63ee6dcc81bebce"), "name" : "test2", "age" : 123 }{ "_id" : ObjectId("5a1646a3e63ee6dcc81bebcf"), "name" : "test3", "age" : 123 }{ "_id" : ObjectId("5a1646a3e63ee6dcc81bebd0"), "name" : "test4", "age" : 123 }{ "_id" : ObjectId("5a1646a3e63ee6dcc81bebd1"), "name" : "test5", "age" : 123 }{ "_id" : ObjectId("5a1646a3e63ee6dcc81bebd2"), "name" : "test6", "age" : 123 }{ "_id" : ObjectId("5a1646a3e63ee6dcc81bebd4"), "name" : "test8", "age" : 123 }{ "_id" : ObjectId("5a1646a3e63ee6dcc81bebd3"), "name" : "test7", "age" : 123 }{ "_id" : ObjectId("5a1646a3e63ee6dcc81bebd5"), "name" : "test9", "age" : 123 }{ "_id" : ObjectId("5a1646a3e63ee6dcc81bebd6"), "name" : "test10", "age" : 123 }{ "_id" : ObjectId("5a1646a3e63ee6dcc81bebd7"), "name" : "test11", "age" : 123 }{ "_id" : ObjectId("5a1646a3e63ee6dcc81bebd8"), "name" : "test12", "age" : 123 }{ "_id" : ObjectId("5a1646a3e63ee6dcc81bebda"), "name" : "test14", "age" : 123 }{ "_id" : ObjectId("5a1646a3e63ee6dcc81bebd9"), "name" : "test13", "age" : 123 }{ "_id" : ObjectId("5a1646a3e63ee6dcc81bebdb"), "name" : "test15", "age" : 123 }{ "_id" : ObjectId("5a1646a3e63ee6dcc81bebdc"), "name" : "test16", "age" : 123 }{ "_id" : ObjectId("5a1646a3e63ee6dcc81bebdd"), "name" : "test17", "age" : 123 }{ "_id" : ObjectId("5a1646a3e63ee6dcc81bebde"), "name" : "test18", "age" : 123 }{ "_id" : ObjectId("5a1646a3e63ee6dcc81bebdf"), "name" : "test19", "age" : 123 }Type "it" for more

删除节点

实验删除集群中的一个节点,指定ip删除节点3

在主节点上执行:

sbc:PRIMARY> rs.remove("192.168.58.131:27017"){ "ok" : 1 }

再次查看整个集群状态,发现ip为192.168.58.131的节点以及被删除

sbc:PRIMARY> rs.status(){    "set" : "sbc",    "date" : ISODate("2017-11-24T02:18:36.820Z"),    "myState" : 1,    "term" : NumberLong(3),    "heartbeatIntervalMillis" : NumberLong(2000),    "optimes" : {        "lastCommittedOpTime" : {            "ts" : Timestamp(1511489912, 1),            "t" : NumberLong(3)        },        "appliedOpTime" : {            "ts" : Timestamp(1511489912, 1),            "t" : NumberLong(3)        },        "durableOpTime" : {            "ts" : Timestamp(1511489912, 1),            "t" : NumberLong(3)        }    },    "members" : [        {            "_id" : 0,            "name" : "192.168.58.129:27017",            "health" : 1,            "state" : 1,            "stateStr" : "PRIMARY",            "uptime" : 281,            "optime" : {                "ts" : Timestamp(1511489912, 1),                "t" : NumberLong(3)            },            "optimeDate" : ISODate("2017-11-24T02:18:32Z"),            "electionTime" : Timestamp(1511489647, 1),            "electionDate" : ISODate("2017-11-24T02:14:07Z"),            "configVersion" : 2,            "self" : true        },        {            "_id" : 1,            "name" : "192.168.58.130:27017",            "health" : 1,            "state" : 2,            "stateStr" : "SECONDARY",            "uptime" : 275,            "optime" : {                "ts" : Timestamp(1511489912, 1),                "t" : NumberLong(3)            },            "optimeDurable" : {                "ts" : Timestamp(1511489912, 1),                "t" : NumberLong(3)            },            "optimeDate" : ISODate("2017-11-24T02:18:32Z"),            "optimeDurableDate" : ISODate("2017-11-24T02:18:32Z"),            "lastHeartbeat" : ISODate("2017-11-24T02:18:36.794Z"),            "lastHeartbeatRecv" : ISODate("2017-11-24T02:18:32.797Z"),            "pingMs" : NumberLong(0),            "configVersion" : 2        }    ],    "ok" : 1}

此时节点3按下回车后

sbc:SECONDARY> 2017-11-23T18:19:02.932-0800 I NETWORK  [thread1] trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed2017-11-23T18:19:02.932-0800 I NETWORK  [thread1] reconnect 127.0.0.1:27017 (127.0.0.1) oksbc:OTHER>

节点状态由SECONDARY变为了OTHER,也说明已经被移出集群

添加节点

在主节点查看各个从节点状态

sbc:PRIMARY> db.printSlaveReplicationInfo()source: 192.168.58.130:27017    syncedTo: Thu Nov 23 2017 18:21:09 GMT-0800 (PST)    0 secs (0 hrs) behind the primary     ```按照ip加入节点```vimsbc:PRIMARY> rs.add("192.168.58.131:27017"){ "ok" : 1 }<div class="se-preview-section-delimiter"></div>

尝试第二次加入这个集群

由于集群已存在,会报错Found two member configurations with same host field,而导致添加失败

sbc:PRIMARY> rs.add("192.168.58.131:27017"){    "ok" : 0,    "errmsg" : "Found two member configurations with same host field, members.2.host == members.3.host == 192.168.58.131:27017",    "code" : 103,    "codeName" : "NewReplicaSetConfigurationIncompatible"}<div class="se-preview-section-delimiter"></div>

此时再查看集群状态

节点3已经成功加入集群

sbc:PRIMARY> db.printSlaveReplicationInfo()source: 192.168.58.130:27017    syncedTo: Thu Nov 23 2017 18:23:29 GMT-0800 (PST)    0 secs (0 hrs) behind the primary source: 192.168.58.131:27017    syncedTo: Thu Nov 23 2017 18:23:29 GMT-0800 (PST)    0 secs (0 hrs) behind the primary 
原创粉丝点击