快速部署MongoDB Replica Set复制集

本文介绍如何快速部署MongoDB  Replica Set复制集,注意本文中的配置是不涉及授权验证的。

MongoDB版本为3.0.2 , 该部署全部在同一台服务器上完成,实际产品环境 Replica Set 中master和slave显然应当在不同的服务器上, 仅仅需要对本文中涉及IP的地方有所修改即可

我们假定用户已经成功安装mongoDB 3.0.2

 

启动 replica set的primary节点上的mongodb

sudo mkdir -p /m01/mongo1
sudo mkdir -p /m01/mongo2
sudo mkdir -p /m01/mongo3



mongod -dbpath /m01/mongo1 --replSet "rs_maclean" --port 35001 --storageEngine wiredTiger   &
mongod -dbpath /m01/mongo2 --replSet "rs_maclean" --port 35002 --storageEngine wiredTiger   &
mongod -dbpath /m01/mongo3 --replSet "rs_maclean" --port 35003 --storageEngine wiredTiger   &


登陆到已经启动的primary mongodb

mongo localhost:35001
MongoDB shell version: 3.0.2
connecting to: localhost:35001/test


初始化 replica set  成员

rs.initiate()

获得 replica set配置信息

rs.conf()

rs_maclean:PRIMARY> rs.conf();
{
	"_id" : "rs_maclean",
	"version" : 1,
	"members" : [
		{
			"_id" : 0,
			"host" : "shard0.dbdao.com:35001",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : 0,
			"votes" : 1
		}
	],
	"settings" : {
		"chainingAllowed" : true,
		"heartbeatTimeoutSecs" : 10,
		"getLastErrorModes" : {
			
		},
		"getLastErrorDefaults" : {
			"w" : 1,
			"wtimeout" : 0
		}
	}
}



添加其他2个replica set member 

rs.add("ip2:35002")
rs.add("ip3:35003")


检查 Replica set 状态

rs_maclean:PRIMARY> rs.status()
{
	"set" : "rs_maclean",
	"date" : ISODate("2015-05-12T14:34:57.205Z"),
	"myState" : 1,
	"members" : [
		{
			"_id" : 0,
			"name" : "shard0.dbdao.com:35001",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 398,
			"optime" : Timestamp(1431441212, 1),
			"optimeDate" : ISODate("2015-05-12T14:33:32Z"),
			"electionTime" : Timestamp(1431440950, 2),
			"electionDate" : ISODate("2015-05-12T14:29:10Z"),
			"configVersion" : 2,
			"self" : true
		},
		{
			"_id" : 1,
			"name" : "shard1.dbdao.com:35002",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 85,
			"optime" : Timestamp(1431441212, 1),
			"optimeDate" : ISODate("2015-05-12T14:33:32Z"),
			"lastHeartbeat" : ISODate("2015-05-12T14:34:56.044Z"),
			"lastHeartbeatRecv" : ISODate("2015-05-12T14:34:56.061Z"),
			"pingMs" : 0,
			"configVersion" : 2
		}
.......
	],
	"ok" : 1
}


此时我们触发 primary的mongod 进程被kill

当原primary节点重新启动会成为SECONDARY member

Comment

*

沪ICP备14014813号

沪公网安备 31010802001379号