环境:
OS:Centos 7
mongodb:5.0
当前的ip 变更后的ip
192.168.1.108 192.168.1.105 PRIMARY
192.168.1.109 192.168.1.106 SECONDARY
192.168.1.110 192.168.1.107 SECONDARY
1.查看当前的集群
登录一个节点上查看
/usr/local/services/mongodb/bin/mongo 192.168.1.108:29001 myrepl:PRIMARY> use admin myrepl:PRIMARY> db.auth("test","test123"); myrepl:PRIMARY> rs.conf() { "_id" : "myrepl", "version" : 1, "term" : 6, "members" : [ { "_id" : 0, "host" : "192.168.1.108:29001", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 }, { "_id" : 1, "host" : "192.168.1.109:29001", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 }, { "_id" : 2, "host" : "192.168.1.110:29001", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 } ], "protocolVersion" : NumberLong(1), "writeConcernMajorityJournalDefault" : true, "settings" : { "chainingAllowed" : true, "heartbeatIntervalMillis" : 2000, "heartbeatTimeoutSecs" : 10, "electionTimeoutMillis" : 10000, "catchUpTimeoutMillis" : -1, "catchUpTakeoverDelayMillis" : 30000, "getLastErrorModes" : { }, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : 0 }, "replicaSetId" : ObjectId("64d58c8b10dc4e229f96f8a7") } }
2.停掉mongodb
每个节点上操作,先停掉从再停主
/usr/local/services/mongodb/bin/mongo localhost:29001
use admin
db.auth("test","test123");
db.shutdownServer()
3.修改每个节点的ip
cd /etc/sysconfig/network-scripts
vi ifcfg-enp0s3
重启网络
systemctl restart network
4.单机模式启动每一个节点
我这里以其中一个节点为例子
vi /home/middle/mongodb/conf/mongo.cnf
bind_ip=192.168.1.108,127.0.0.1 ##注意修改ip
##shardsvr=true
##replSet=myrepl
##auth=true
##keyFile=/home/middle/mongodb/key/keyfile
单机模式启动
/usr/local/services/mongodb/bin/mongod -f /home/middle/mongodb/conf/mongo.cnf
5.更新配置表
每个节点都要执行该操作
/usr/local/services/mongodb/bin/mongo localhost:29001 use local db.system.replset.findOne() cfg = db.system.replset.findOne({_id: 'myrepl'}) cfg.members[0].host = "192.168.1.105:29001" cfg.members[1].host = "192.168.1.106:29001" cfg.members[2].host = "192.168.1.107:29001" db.system.replset.update({_id: 'myrepl'}, cfg) db.system.replset.findOne()
注意这里我的副本集名称为:myrepl,要根据自己的实际情况替换.
6.停掉每个节点
/usr/local/services/mongodb/bin/mongo localhost:29001
use admin
db.shutdownServer()
另外的节点也做步骤4 和步骤5、步骤6的操作
7.去掉每个节的注释参数
vi /home/middle/mongodb/conf/mongo.cnf
shardsvr=true
replSet=myrepl
auth=true
keyFile=/home/middle/mongodb/key/keyfile
8.重新启动
每个节点都执行
/usr/local/services/mongodb/bin/mongod -f /home/middle/mongodb/conf/mongo.cnf
9.登录检查
/usr/local/services/mongodb/bin/mongo localhost:29001 use admin db.auth("test","test123"); myrepl:PRIMARY> rs.status() { "set" : "myrepl", "date" : ISODate("2023-09-02T07:08:30.560Z"), "myState" : 1, "term" : NumberLong(7), "syncSourceHost" : "", "syncSourceId" : -1, "heartbeatIntervalMillis" : NumberLong(2000), "majorityVoteCount" : 2, "writeMajorityCount" : 2, "votingMembersCount" : 3, "writableVotingMembersCount" : 3, "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1693638501, 1), "t" : NumberLong(7) }, "lastCommittedWallTime" : ISODate("2023-09-02T07:08:21.308Z"), "readConcernMajorityOpTime" : { "ts" : Timestamp(1693638501, 1), "t" : NumberLong(7) }, "appliedOpTime" : { "ts" : Timestamp(1693638501, 1), "t" : NumberLong(7) }, "durableOpTime" : { "ts" : Timestamp(1693638501, 1), "t" : NumberLong(7) }, "lastAppliedWallTime" : ISODate("2023-09-02T07:08:21.308Z"), "lastDurableWallTime" : ISODate("2023-09-02T07:08:21.308Z") }, "lastStableRecoveryTimestamp" : Timestamp(1693637577, 1), "electionCandidateMetrics" : { "lastElectionReason" : "electionTimeout", "lastElectionDate" : ISODate("2023-09-02T07:07:51.248Z"), "electionTerm" : NumberLong(7), "lastCommittedOpTimeAtElection" : { "ts" : Timestamp(0, 0), "t" : NumberLong(-1) }, "lastSeenOpTimeAtElection" : { "ts" : Timestamp(1693637587, 1), "t" : NumberLong(6) }, "numVotesNeeded" : 2, "priorityAtElection" : 1, "electionTimeoutMillis" : NumberLong(10000), "numCatchUpOps" : NumberLong(0), "newTermStartDate" : ISODate("2023-09-02T07:07:51.301Z"), "wMajorityWriteAvailabilityDate" : ISODate("2023-09-02T07:07:51.867Z") }, "members" : [ { "_id" : 0, "name" : "192.168.1.105:29001", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 51, "optime" : { "ts" : Timestamp(1693638501, 1), "t" : NumberLong(7) }, "optimeDate" : ISODate("2023-09-02T07:08:21Z"), "lastAppliedWallTime" : ISODate("2023-09-02T07:08:21.308Z"), "lastDurableWallTime" : ISODate("2023-09-02T07:08:21.308Z"), "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "Could not find member to sync from", "electionTime" : Timestamp(1693638471, 1), "electionDate" : ISODate("2023-09-02T07:07:51Z"), "configVersion" : 1, "configTerm" : 7, "self" : true, "lastHeartbeatMessage" : "" }, { "_id" : 1, "name" : "192.168.1.106:29001", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 47, "optime" : { "ts" : Timestamp(1693638501, 1), "t" : NumberLong(7) }, "optimeDurable" : { "ts" : Timestamp(1693638501, 1), "t" : NumberLong(7) }, "optimeDate" : ISODate("2023-09-02T07:08:21Z"), "optimeDurableDate" : ISODate("2023-09-02T07:08:21Z"), "lastAppliedWallTime" : ISODate("2023-09-02T07:08:21.308Z"), "lastDurableWallTime" : ISODate("2023-09-02T07:08:21.308Z"), "lastHeartbeat" : ISODate("2023-09-02T07:08:29.328Z"), "lastHeartbeatRecv" : ISODate("2023-09-02T07:08:30.344Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncSourceHost" : "192.168.1.105:29001", "syncSourceId" : 0, "infoMessage" : "", "configVersion" : 1, "configTerm" : 7 }, { "_id" : 2, "name" : "192.168.1.107:29001", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 44, "optime" : { "ts" : Timestamp(1693638501, 1), "t" : NumberLong(7) }, "optimeDurable" : { "ts" : Timestamp(1693638501, 1), "t" : NumberLong(7) }, "optimeDate" : ISODate("2023-09-02T07:08:21Z"), "optimeDurableDate" : ISODate("2023-09-02T07:08:21Z"), "lastAppliedWallTime" : ISODate("2023-09-02T07:08:21.308Z"), "lastDurableWallTime" : ISODate("2023-09-02T07:08:21.308Z"), "lastHeartbeat" : ISODate("2023-09-02T07:08:29.328Z"), "lastHeartbeatRecv" : ISODate("2023-09-02T07:08:30.345Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncSourceHost" : "192.168.1.105:29001", "syncSourceId" : 0, "infoMessage" : "", "configVersion" : 1, "configTerm" : 7 } ], "ok" : 1 }
标签:02T07,mongodb,08,09,update,NumberLong,ip,ISODate,2023 From: https://www.cnblogs.com/hxlasky/p/17673711.html