MongoDB 4.2副本集配置(一主二副)

本文详细介绍了在CentOS7.5环境下,如何部署MongoDB 4.2.12的复制集,包括系统包安装、用户与目录创建、配置文件设置、服务启动及复制集初始化。通过步骤演示,成功配置了一个包含三个节点的复制集,并验证了数据同步功能。
摘要由CSDN通过智能技术生成

项目环境:

CentOS 7.5

MongoDB 4.2.12

一、部署 MongoDB 

1、三个节点安装以下包

yum install -y cyrus-sasl cyrus-sasl-plain cyrus-sasl-gssapi krb5-libs lm_sensors-libs net-snmp-agent-libs \
net-snmp openssl openssl-devel rpm-libs tcp_wrappers-libs

2、创建MongoDB用户和组

groupadd -g 10001 mongodb
useradd -u 10001 -g mongodb mongodb

3、修改/etc/security/limits.conf

mongodb hard nofile 25600
mongodb soft nofile 25600
mongodb hard nproc 25600
mongodb soft nproc 25600

4、创建目录

mkdir -p /mongodb/apps
mkdir -p /mongodb/data
mkdir -p /mongodb/logs
mkdir -p /mongodb/conf

5、修改mongodb.conf

172.16.254.131

dbpath=/mongodb/data
logpath=/mongodb/logs/mongodb.log
port=27017
fork=true
#auth=true
noauth=true
#verbose=true
#vvvv=true
journal = true
maxConns=500
logappend=true
directoryperdb=true
#pidfilepath=/var/run/mongo.pid
#cpu=true
#nohttpinterface=false
#notablescan=false
#profile=0
#slowms=200
#quiet=true
#syncdelay=60
bind_ip=0.0.0.0
replSet=handong1

172.16.254.132

dbpath=/mongodb/data
logpath=/mongodb/logs/mongodb.log
port=27017
fork=true
#auth=true
noauth=true
#verbose=true
#vvvv=true
journal = true
maxConns=500
logappend=true
directoryperdb=true
#pidfilepath=/var/run/mongo.pid
#cpu=true
#nohttpinterface=false
#notablescan=false
#profile=0
#slowms=200
#quiet=true
#syncdelay=60
bind_ip=0.0.0.0
replSet=handong1

172.16.254.133

dbpath=/mongodb/data
logpath=/mongodb/logs/mongodb.log
port=27017
fork=true
#auth=true
noauth=true
#verbose=true
#vvvv=true
journal = true
maxConns=500
logappend=true
directoryperdb=true
#pidfilepath=/var/run/mongo.pid
#cpu=true
#nohttpinterface=false
#notablescan=false
#profile=0
#slowms=200
#quiet=true
#syncdelay=60
bind_ip=0.0.0.0
replSet=handong1

6、启动MongoDB

三个节点分别执行

mongod -f /mongodb/conf/mongodb.conf
mongod -f /mongodb/conf/mongodb.conf
mongod -f /mongodb/conf/mongodb.conf

二、配置复制集

1、创建复制集

rs.initiate({
  _id:"handong1",
  members:[{
   _id:0,
   host:"172.16.254.131:27017"
  },{
   _id:1,
   host:"172.16.254.132:27017"
  },{
   _id:2,
   host:"172.16.254.133:27017"
  }]
})

2、开启副本节点查询

MongoDB复制集副本节点默认不支持查询,需要手动开启。

副本节点执行:

rs.secondaryOk();

3、查看复制集信息

handong1:PRIMARY> rs.status()
{
	"set" : "handong1",
	"date" : ISODate("2021-03-17T11:46:06.442Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"syncingTo" : "",
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"majorityVoteCount" : 2,
	"writeMajorityCount" : 2,
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1615981565, 1),
			"t" : NumberLong(1)
		},
		"lastCommittedWallTime" : ISODate("2021-03-17T11:46:05.489Z"),
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1615981565, 1),
			"t" : NumberLong(1)
		},
		"readConcernMajorityWallTime" : ISODate("2021-03-17T11:46:05.489Z"),
		"appliedOpTime" : {
			"ts" : Timestamp(1615981565, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1615981565, 1),
			"t" : NumberLong(1)
		},
		"lastAppliedWallTime" : ISODate("2021-03-17T11:46:05.489Z"),
		"lastDurableWallTime" : ISODate("2021-03-17T11:46:05.489Z")
	},
	"lastStableRecoveryTimestamp" : Timestamp(1615981555, 1),
	"lastStableCheckpointTimestamp" : Timestamp(1615981555, 1),
	"electionCandidateMetrics" : {
		"lastElectionReason" : "electionTimeout",
		"lastElectionDate" : ISODate("2021-03-17T09:21:47.969Z"),
		"electionTerm" : NumberLong(1),
		"lastCommittedOpTimeAtElection" : {
			"ts" : Timestamp(0, 0),
			"t" : NumberLong(-1)
		},
		"lastSeenOpTimeAtElection" : {
			"ts" : Timestamp(1615972897, 1),
			"t" : NumberLong(-1)
		},
		"numVotesNeeded" : 2,
		"priorityAtElection" : 1,
		"electionTimeoutMillis" : NumberLong(10000),
		"numCatchUpOps" : NumberLong(0),
		"newTermStartDate" : ISODate("2021-03-17T09:21:48.059Z"),
		"wMajorityWriteAvailabilityDate" : ISODate("2021-03-17T09:21:49.600Z")
	},
	"members" : [
		{
			"_id" : 0,
			"name" : "172.16.254.131:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 10604,
			"optime" : {
				"ts" : Timestamp(1615981565, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2021-03-17T11:46:05Z"),
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"electionTime" : Timestamp(1615972908, 1),
			"electionDate" : ISODate("2021-03-17T09:21:48Z"),
			"configVersion" : 1,
			"self" : true,
			"lastHeartbeatMessage" : ""
		},
		{
			"_id" : 1,
			"name" : "172.16.254.132:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 8668,
			"optime" : {
				"ts" : Timestamp(1615981565, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1615981565, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2021-03-17T11:46:05Z"),
			"optimeDurableDate" : ISODate("2021-03-17T11:46:05Z"),
			"lastHeartbeat" : ISODate("2021-03-17T11:46:06.180Z"),
			"lastHeartbeatRecv" : ISODate("2021-03-17T11:46:04.538Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "172.16.254.131:27017",
			"syncSourceHost" : "172.16.254.131:27017",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1
		},
		{
			"_id" : 2,
			"name" : "172.16.254.133:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 8668,
			"optime" : {
				"ts" : Timestamp(1615981565, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1615981565, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2021-03-17T11:46:05Z"),
			"optimeDurableDate" : ISODate("2021-03-17T11:46:05Z"),
			"lastHeartbeat" : ISODate("2021-03-17T11:46:06.182Z"),
			"lastHeartbeatRecv" : ISODate("2021-03-17T11:46:04.541Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "172.16.254.131:27017",
			"syncSourceHost" : "172.16.254.131:27017",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1
		}
	],
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1615981565, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1615981565, 1)
}

三、验证MongoDB复制集

创建db

handong1:PRIMARY> use db3
switched to db db3

插入测试数据:

handong1:PRIMARY> db.db3.insert({"name":"mongodb"})
WriteResult({ "nInserted" : 1 })

查询数据:

172.16.254.131

handong1:SECONDARY> use db3
switched to db db3
handong1:PRIMARY> db.db3.find()
{ "_id" : ObjectId("6051d08ac74d627f4b561349"), "name" : "mongodb" }

172.16.254.132

handong1:SECONDARY> use db3
switched to db db3
handong1:SECONDARY> db.db3.find()
{ "_id" : ObjectId("6051d08ac74d627f4b561349"), "name" : "mongodb" }

172.16.254.133

handong1:SECONDARY> use db3
switched to db db3
handong1:SECONDARY> db.db3.find()
{ "_id" : ObjectId("6051d08ac74d627f4b561349"), "name" : "mongodb" }

可以看到副本节点数据已经同步。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值