汪微的博客
zane,做一个有思维的开发者

汪微的博客

MongoDB主从副本集架构

2018年09月30日81 browse

文章内容来自网络,经过自己实践,梳理成文档,仅供参考:

备注:MongoDB版本3.6以上

基本架构

Replication:主从结构,一个Primary,多个Secondary,可能会有Arbitry。Primary挂掉之后,会选举出一个Secondary作为Primary。


安装MongoDB

参考链接:https://blog.seosiwei.com/detail/40


测试部署过程

备注:以下内容以同一台机器多个启动多个mongod服务为例(mac电脑)


一:创建3份mongod.conf启动配置文件 分别为(mongod27017.conf,mongod27018.conf,mongod27019.conf)

基本配置如下:

processManagement:
  fork: true
systemLog:
  destination: file
  path: /usr/local/var/log/mongodb/mongo.log
  logAppend: true
storage:
  dbPath: /Users/MacBook/mongodb
net:
  bindIp: 127.0.0.1
  port: 27017
//主从复制核心
replication:
  oplogSizeMB: 1024
  replSetName: performance

其他两份配置相同,端口号分别为27018,27019即可。

mongod.confg详细参数配置可参考:

MongoDB - mongod.conf重要配置


二:启动27017实例

mongod -f /usr/local/etc/mongod27017.conf

或者

brew services start mongodb

进入27017 实例:

> mongo
> use admin;
switched to db admin
> rs.status();
{
	"info" : "run rs.initiate(...) if not yet done for the set",
	"ok" : 0,
	"errmsg" : "no replset config has been received",
	"code" : 94,
	"codeName" : "NotYetInitialized",
	"$clusterTime" : {
		"clusterTime" : Timestamp(0, 0),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}

提示要初始化复制集:

> rs.initiate({_id:'performance',members:[{_id:1,host:'127.0.0.1:27017'}]});
{
	"ok" : 1,
	"operationTime" : Timestamp(1538288305, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1538288305, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
performance:SECONDARY> rs.status();
{
	"set" : "performance",
	"date" : ISODate("2018-09-30T06:18:46.273Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"heartbeatIntervalMillis" : NumberLong(2000),
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1538288307, 5),
			"t" : NumberLong(1)
		},
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1538288307, 5),
			"t" : NumberLong(1)
		},
		"appliedOpTime" : {
			"ts" : Timestamp(1538288307, 5),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1538288307, 5),
			"t" : NumberLong(1)
		}
	},
	"members" : [
		{
			"_id" : 1,
			"name" : "127.0.0.1:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 170,
			"optime" : {
				"ts" : Timestamp(1538288307, 5),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2018-09-30T06:18:27Z"),
			"infoMessage" : "could not find member to sync from",
			"electionTime" : Timestamp(1538288305, 2),
			"electionDate" : ISODate("2018-09-30T06:18:25Z"),
			"configVersion" : 1,
			"self" : true
		}
	],
	"ok" : 1,
	"operationTime" : Timestamp(1538288307, 5),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1538288307, 5),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}

这时复制集已经可以了,只是现在只有一个成员,并且自动为 PRIMARY

启动另外两个实例,27018和27019,并增加到复制集。

启动完成后,进入 27017 实例:

mongod -f /usr/local/etc/mongod27018.conf
mongod -f /usr/local/etc/mongod27019.conf
performance:PRIMARY> rs.add("127.0.0.1:27018");
performance:PRIMARY> rs.add("127.0.0.1:27019");
performance:PRIMARY> rs.status();
{
	"set" : "performance",
	"date" : ISODate("2018-09-30T06:20:56.252Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"heartbeatIntervalMillis" : NumberLong(2000),
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1538288449, 1),
			"t" : NumberLong(1)
		},
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1538288449, 1),
			"t" : NumberLong(1)
		},
		"appliedOpTime" : {
			"ts" : Timestamp(1538288449, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1538288449, 1),
			"t" : NumberLong(1)
		}
	},
	"members" : [
		{
			"_id" : 1,
			"name" : "127.0.0.1:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 300,
			"optime" : {
				"ts" : Timestamp(1538288449, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2018-09-30T06:20:49Z"),
			"electionTime" : Timestamp(1538288305, 2),
			"electionDate" : ISODate("2018-09-30T06:18:25Z"),
			"configVersion" : 2,
			"self" : true
		},
		{
			"_id" : 2,
			"name" : "127.0.0.1:27018",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 6,
			"optime" : {
				"ts" : Timestamp(1538288449, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1538288449, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2018-09-30T06:20:49Z"),
			"optimeDurableDate" : ISODate("2018-09-30T06:20:49Z"),
			"lastHeartbeat" : ISODate("2018-09-30T06:20:55.467Z"),
			"lastHeartbeatRecv" : ISODate("2018-09-30T06:20:52.498Z"),
			"pingMs" : NumberLong(0),
			"configVersion" : 2
		},
		{
			"_id" : 3,
			"name" : "127.0.0.1:27019",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 6,
			"optime" : {
				"ts" : Timestamp(1538288449, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1538288449, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2018-09-30T06:20:49Z"),
			"optimeDurableDate" : ISODate("2018-09-30T06:20:49Z"),
			"lastHeartbeat" : ISODate("2018-09-30T06:20:55.467Z"),
			"lastHeartbeatRecv" : ISODate("2018-09-30T06:20:52.498Z"),
			"pingMs" : NumberLong(0),
			"configVersion" : 2
		}
	],
	"ok" : 1,
	"operationTime" : Timestamp(1538288449, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1538288449, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}


测试复制集:

(测试代码略过...)

经过测试,配置成功。


mac mongodb可视化工具推荐 Robomongo: 

链接地址:https://robomongo.org/download


相关项目推荐:

前端性能监控系统egg-mongoose-performance-system(开发中...)



博主 zane 发表于 2018-09-30 14:59:26,添加在了 mongodb 标签下

打赏

您的支持将鼓励我继续努力与分享。

扫码打赏,建议金额1-10元

提醒:打赏金额将直接进此方账号,无法退款,请您谨慎操作。

评论

正在加载验证码......

提交