快速部署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

root@shard0:~# ps -ef|grep mongod
root     24591 24566  1 10:28 pts/1    00:00:12 mongod -dbpath /m01/mongo1 --replSet rs_maclean --port 35001 --storageEngine wiredTiger

root     24970 24566  0 10:41 pts/1    00:00:00 grep --color=auto mongod
root@shard0:~# 
root@shard0:~# kill -9 24591




此时触发 replSet possible election tie


2015-05-12T10:59:59.476-0400 E REPL     [rsBackgroundSync] sync producer problem: 10278 dbclient error communicating with server: shard0.dbdao.com:35001
2015-05-12T10:59:59.478-0400 I REPL     [ReplicationExecutor] could not find member to sync from
2015-05-12T11:00:00.382-0400 I NETWORK  [ReplExecNetThread-1] DBClientCursor::init call() failed
2015-05-12T11:00:00.382-0400 I REPL     [ReplicationExecutor] Standing for election
2015-05-12T11:00:00.384-0400 I REPL     [ReplicationExecutor] replSet possible election tie; sleeping 961ms until 2015-05-12T11:00:01.345-0400
2015-05-12T11:00:01.345-0400 I REPL     [ReplicationExecutor] Standing for election
2015-05-12T11:00:01.346-0400 I REPL     [ReplicationExecutor] replSet info electSelf
2015-05-12T11:00:01.348-0400 I REPL     [ReplicationExecutor] replSet election succeeded, assuming primary role
2015-05-12T11:00:01.348-0400 I REPL     [ReplicationExecutor] transition to PRIMARY
2015-05-12T11:00:01.605-0400 I REPL     [rsSync] transition to primary complete; database writes are now permitted
2015-05-12T11:00:04.853-0400 I NETWORK  [conn15] end connection 127.0.0.1:34149 (3 connections now open)
2015-05-12T11:00:05.735-0400 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:34160 #18 (4 connections now open)
2015-05-12T11:00:08.011-0400 I NETWORK  [conn18] end connection 127.0.0.1:34160 (3 connections now open)
2015-05-12T11:00:21.642-0400 I NETWORK  [conn17] end connection 104.236.21.67:44231 (2 connections now open)
2015-05-12T11:00:21.643-0400 I NETWORK  [initandlisten] connection accepted from 104.236.21.67:44264 #19 (3 connections now open)
2015-05-12T11:00:22.548-0400 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:34189 #20 (4 connections now open)
2015-05-12T11:00:23.932-0400 I NETWORK  [conn20] end connection 127.0.0.1:34189 (3 connections now open)
2015-05-12T11:00:51.671-0400 I NETWORK  [conn19] end connection 104.236.21.67:44264 (2 connections now open)
2015-05-12T11:00:51.671-0400 I NETWORK  [initandlisten] connection accepted from 104.236.21.67:44310 #21 (3 connections now open)
2015-05-12T11:01:21.700-0400 I NETWORK  [conn21] end connection 104.236.21.67:44310 (2 connections now open)
2015-05-12T11:01:21.701-0400 I NETWORK  [initandlisten] connection accepted from 104.236.21.67:44356 #22 (3 connections now open)


secondary 接管primary 



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

Comment

*

沪ICP备14014813号-2

沪公网安备 31010802001379号