Linux redhat6.5 1臺 端口:2701七、2701八、27019 修改端口爲27000、2700一、27002
單臺服務器設置了三個配置文件(3個端口),模擬三臺mongod服務,並配置副本集。mongodb
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/mongo1.conf /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/mongo2.conf /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/mongo3.conf
port=27017 bind_ip=127.0.0.1,192.168.249.181 pidfilepath=/var/run/mongod/mongod1.pid dbpath=/usr/local/mongodb/data/mongod1 logpath=/usr/local/mongodb/log/mongod1.log logappend=true fork=true auth=true replSet=replication/192.168.249.181:27018 nohttpinterface=false
port=27000 bind_ip=127.0.0.1,192.168.249.181 pidfilepath=/var/run/mongod/mongod1.pid dbpath=/usr/local/mongodb/data/mongod1 logpath=/usr/local/mongodb/log/mongod1.log logappend=true fork=true auth=true replSet=replication/192.168.249.181:27001 nohttpinterface=false
重啓mongod服務後,發現副本集狀態出現異常:json
replication:OTHER> rs.status() { "state" : 10, "stateStr" : "REMOVED", "uptime" : 282, "optime" : { "ts" : Timestamp(1504690040, 1), "t" : NumberLong(8) }, "optimeDate" : ISODate("2017-09-06T09:27:20Z"), "ok" : 0, "errmsg" : "Our replica set config is invalid or we are not a member of it", "code" : 93, "codeName" : "InvalidReplicaSetConfig" }
恍然,配置的副本集端口也須要修改:服務器
# 獲取副本集配置 replication:OTHER> cfg=rs.conf() replication:OTHER> printjson(cfg) # 查看配置節點 replication:OTHER> printjson(cfg.members[0]) # 修改節點端口 replication:OTHER> cfg.members[0].host="192.168.249.181:27000" replication:OTHER> cfg.members[1].host="192.168.249.181:27001" replication:OTHER> cfg.members[2].host="192.168.249.181:27002" # 從新配置 replication:OTHER> rs.reconfig(cfg) # 可能會報錯"errmsg" : "replSetReconfig should only be run on PRIMARY, but my state is REMOVED; use the \"force\" argument to override", # 報錯的話,根據報錯信息增強制指令執行 replication:OTHER> rs.reconfig(cfg, {force : true})
replication:PRIMARY> rs.status() replication:PRIMARY> rs.isMaster() { "hosts" : [ "192.168.249.181:27000", "192.168.249.181:27001", "192.168.249.181:27002" ], "setName" : "replication", "setVersion" : 52478, "ismaster" : true, "secondary" : false, "primary" : "192.168.249.181:27002", "me" : "192.168.249.181:27002", "electionId" : ObjectId("7fffffff0000000000000009"), "lastWrite" : { "opTime" : { "ts" : Timestamp(1504693178, 1), "t" : NumberLong(9) }, "lastWriteDate" : ISODate("2017-09-06T10:19:38Z") }, "maxBsonObjectSize" : 16777216, "maxMessageSizeBytes" : 48000000, "maxWriteBatchSize" : 1000, "localTime" : ISODate("2017-09-06T10:19:46.167Z"), "maxWireVersion" : 5, "minWireVersion" : 0, "readOnly" : false, "ok" : 1 }