MongoDB副本集配置好后修改配置
博客专区 > o00o 的博客 > 博客详情
MongoDB副本集配置好后修改配置
o00o 发表于1个月前
MongoDB副本集配置好后修改配置
  • 发表于 1个月前
  • 阅读 2
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 十分钟定制你的第一个小程序>>>   

摘要: 线上环境已经配置好副本集,但由于使用的是默认端口,做加固处理需要修改端口号,这里记录测试环境测试步骤

测试环境

Linux redhat6.5 1台
端口:27017、27018、27019
修改端口为27000、27001、27002

配置

单台服务器设置了三个配置文件(3个端口),模拟三台mongod服务,并配置副本集。

/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

mongo1.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

mongo1.conf(修改)

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服务

重启mongod服务后,发现副本集状态出现异常:

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
}
标签: MongoDB
共有 人打赏支持
粉丝 2
博文 24
码字总数 25911
×
o00o
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: