环境:
OS:Centos 7
mongodb:5.0
当前的ip 变更后的ip
192.168.1.105 192.168.1.108 PRIMARY
192.168.1.106 192.168.1.109 SECONDARY
192.168.1.107 192.168.1.110 ARBITER
1.查看当前的集群
登录一个节点上查看
[root@localhost key]# /usr/local/services/mongodb/bin/mongo localhost:29001 use admin db.auth("test","test123"); myrepl:PRIMARY> rs.conf() { "_id" : "myrepl", "version" : 1, "term" : 4, "members" : [ { "_id" : 0, "host" : "192.168.1.105:29001", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 }, { "_id" : 1, "host" : "192.168.1.106:29001", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 }, { "_id" : 2, "host" : "192.168.1.107:29001", "arbiterOnly" : true, "buildIndexes" : true, "hidden" : false, "priority" : 0, "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("64f30914ff7c531f5e18d8f0") } }
2.停掉mongodb
每个节点上操作,先停掉从节点和冲裁节点再停主节点
/usr/local/services/mongodb/bin/mongo localhost:29001
use admin
db.auth("test","test123");
db.shutdownServer()
仲裁节点不需要验证
myrepl:ARBITER> use admin
switched to db admin
myrepl:ARBITER> 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.108:29001" cfg.members[1].host = "192.168.1.109:29001" cfg.members[2].host = "192.168.1.110: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.仲裁节点需要修改ip
9.重新启动
每个节点都执行
/usr/local/services/mongodb/bin/mongod -f /home/middle/mongodb/conf/mongo.cnf
10.登录检查
/usr/local/services/mongodb/bin/mongo localhost:29001 use admin db.auth("test","test123"); "members" : [ { "_id" : 0, "name" : "192.168.1.108:29001", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 59, "optime" : { "ts" : Timestamp(1693651795, 1), "t" : NumberLong(5) }, "optimeDate" : ISODate("2023-09-02T10:49:55Z"), "lastAppliedWallTime" : ISODate("2023-09-02T10:49:55.084Z"), "lastDurableWallTime" : ISODate("2023-09-02T10:49:55.084Z"), "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "Could not find member to sync from", "electionTime" : Timestamp(1693651755, 1), "electionDate" : ISODate("2023-09-02T10:49:15Z"), "configVersion" : 1, "configTerm" : 5, "self" : true, "lastHeartbeatMessage" : "" }, { "_id" : 1, "name" : "192.168.1.109:29001", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 55, "optime" : { "ts" : Timestamp(1693651795, 1), "t" : NumberLong(5) }, "optimeDurable" : { "ts" : Timestamp(1693651795, 1), "t" : NumberLong(5) }, "optimeDate" : ISODate("2023-09-02T10:49:55Z"), "optimeDurableDate" : ISODate("2023-09-02T10:49:55Z"), "lastAppliedWallTime" : ISODate("2023-09-02T10:49:55.084Z"), "lastDurableWallTime" : ISODate("2023-09-02T10:49:55.084Z"), "lastHeartbeat" : ISODate("2023-09-02T10:50:01.111Z"), "lastHeartbeatRecv" : ISODate("2023-09-02T10:50:02.118Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncSourceHost" : "192.168.1.108:29001", "syncSourceId" : 0, "infoMessage" : "", "configVersion" : 1, "configTerm" : 5 }, { "_id" : 2, "name" : "192.168.1.110:29001", "health" : 1, "state" : 7, "stateStr" : "ARBITER", "uptime" : 43, "lastHeartbeat" : ISODate("2023-09-02T10:50:01.119Z"), "lastHeartbeatRecv" : ISODate("2023-09-02T10:50:02.407Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "configVersion" : 1, "configTerm" : 5 } ], "ok" : 1 }
标签:02T10,29001,mongodb,09,update,192.168,ip,节点 From: https://www.cnblogs.com/hxlasky/p/17674990.html