环境:
Os:Centos 7
mongodb:4.4.22
调整顺序:从节点-->仲裁节点-->主节点
源ip与新ip对应关系(这里只拿修改从库作为例子)
192.168.107-->192.168.104 从节点
1.关闭从节点
/usr/local/services/mongodb/bin/mongo localhost:29001
myrepl:SECONDARY> use admin
myrepl:SECONDARY> db.auth("test","test123");
myrepl:SECONDARY> db.shutdownServer()
2.修改ip
[root@localhost network-scripts]# more ifcfg-enp0s3 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=enp0s3 UUID=2323beca-b693-4a2a-9fcf-6caa2f095bef DEVICE=enp0s3 ONBOOT=yes IPADDR=192.168.1.104 NETMASK=255.255.255.0 GATEWAY=192.168.1.1
重启网络
[root@localhost /]#service network restart
3.修改配置文件
[root@localhost conf]# vi /home/middle/mongodb/conf/mongo.cnf
bind_ip=192.168.1.104,127.0.0.1
4.启动从库
/usr/local/services/mongodb/bin/mongod -f /home/middle/mongodb/conf/mongo.cnf
5.登陆主库执行如下命令
/usr/local/services/mongodb/bin/mongo localhost:29001
myrepl:PRIMARY> use admin
myrepl:PRIMARY> db.auth("test","test123");
这个时候查看集群状态(rs.status())会提示连接不到原来的ip:
"lastHeartbeatMessage" : "Error connecting to 192.168.1.107:29001 :: caused by :: No route to host",
查看当前的配置
myrepl:PRIMARY> rs.conf() { "_id" : "myrepl", "version" : 23821, "protocolVersion" : NumberLong(1), "writeConcernMajorityJournalDefault" : true, "members" : [ { "_id" : 0, "host" : "192.168.1.102:29001", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 }, { "_id" : 3, "host" : "192.168.1.105:29001", "arbiterOnly" : true, "buildIndexes" : true, "hidden" : false, "priority" : 0, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 }, { "_id" : 4, "host" : "192.168.1.107:29001", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 } ], "settings" : { "chainingAllowed" : true, "heartbeatIntervalMillis" : 2000, "heartbeatTimeoutSecs" : 10, "electionTimeoutMillis" : 10000, "catchUpTimeoutMillis" : -1, "catchUpTakeoverDelayMillis" : 30000, "getLastErrorModes" : { }, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : 0 }, "replicaSetId" : ObjectId("64b0a3f881b811c4931d4d4c") } }
6.进行修改
注意这里的members[2] 中的数值2是member数组的序号
myrepl:PRIMARY>cfg = rs.conf()
myrepl:PRIMARY>cfg.members[2].host="192.168.1.104:29001"
myrepl:PRIMARY>rs.reconfig(cfg)
7.查看副本集状态
myrepl:PRIMARY> rs.status() { "set" : "myrepl", "date" : ISODate("2023-07-17T02:43:37.181Z"), "myState" : 1, "term" : NumberLong(4), "syncSourceHost" : "", "syncSourceId" : -1, "heartbeatIntervalMillis" : NumberLong(2000), "majorityVoteCount" : 2, "writeMajorityCount" : 2, "votingMembersCount" : 3, "writableVotingMembersCount" : 2, "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1689561812, 1), "t" : NumberLong(4) }, "lastCommittedWallTime" : ISODate("2023-07-17T02:43:32.119Z"), "readConcernMajorityOpTime" : { "ts" : Timestamp(1689561812, 1), "t" : NumberLong(4) }, "readConcernMajorityWallTime" : ISODate("2023-07-17T02:43:32.119Z"), "appliedOpTime" : { "ts" : Timestamp(1689561812, 1), "t" : NumberLong(4) }, "durableOpTime" : { "ts" : Timestamp(1689561812, 1), "t" : NumberLong(4) }, "lastAppliedWallTime" : ISODate("2023-07-17T02:43:32.119Z"), "lastDurableWallTime" : ISODate("2023-07-17T02:43:32.119Z") }, "lastStableRecoveryTimestamp" : Timestamp(1689561802, 1), "electionCandidateMetrics" : { "lastElectionReason" : "electionTimeout", "lastElectionDate" : ISODate("2023-07-17T00:43:31.531Z"), "electionTerm" : NumberLong(4), "lastCommittedOpTimeAtElection" : { "ts" : Timestamp(0, 0), "t" : NumberLong(-1) }, "lastSeenOpTimeAtElection" : { "ts" : Timestamp(1689328811, 1), "t" : NumberLong(3) }, "numVotesNeeded" : 2, "priorityAtElection" : 1, "electionTimeoutMillis" : NumberLong(10000), "numCatchUpOps" : NumberLong(0), "newTermStartDate" : ISODate("2023-07-17T00:43:31.548Z"), "wMajorityWriteAvailabilityDate" : ISODate("2023-07-17T00:43:31.655Z") }, "members" : [ { "_id" : 0, "name" : "192.168.1.102:29001", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 7219, "optime" : { "ts" : Timestamp(1689561812, 1), "t" : NumberLong(4) }, "optimeDate" : ISODate("2023-07-17T02:43:32Z"), "lastAppliedWallTime" : ISODate("2023-07-17T02:43:32.119Z"), "lastDurableWallTime" : ISODate("2023-07-17T02:43:32.119Z"), "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "electionTime" : Timestamp(1689554611, 1), "electionDate" : ISODate("2023-07-17T00:43:31Z"), "configVersion" : 23822, "configTerm" : -1, "self" : true, "lastHeartbeatMessage" : "" }, { "_id" : 3, "name" : "192.168.1.105:29001", "health" : 1, "state" : 7, "stateStr" : "ARBITER", "uptime" : 7212, "lastHeartbeat" : ISODate("2023-07-17T02:43:35.680Z"), "lastHeartbeatRecv" : ISODate("2023-07-17T02:43:35.809Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "configVersion" : 23822, "configTerm" : -1 }, { "_id" : 4, "name" : "192.168.1.104:29001", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 363, "optime" : { "ts" : Timestamp(1689561812, 1), "t" : NumberLong(4) }, "optimeDurable" : { "ts" : Timestamp(1689561812, 1), "t" : NumberLong(4) }, "optimeDate" : ISODate("2023-07-17T02:43:32Z"), "optimeDurableDate" : ISODate("2023-07-17T02:43:32Z"), "lastAppliedWallTime" : ISODate("2023-07-17T02:43:32.119Z"), "lastDurableWallTime" : ISODate("2023-07-17T02:43:32.119Z"), "lastHeartbeat" : ISODate("2023-07-17T02:43:35.680Z"), "lastHeartbeatRecv" : ISODate("2023-07-17T02:43:36.680Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncSourceHost" : "192.168.1.102:29001", "syncSourceId" : 0, "infoMessage" : "", "configVersion" : 23822, "configTerm" : -1 } ], "ok" : 1 }
标签:ip,副本,07,mongodb,43,NumberLong,ISODate,2023,17T02 From: https://www.cnblogs.com/hxlasky/p/17559441.html