MongoDB副本集配置系列二:配置MongoDB副本集

接上一篇博客:

http://www.cnblogs.com/xiaoit/p/4479066.html

 

1:首先创建3台虚拟机作为配置环境

IP1:192.168.91.128

IP2:192.168.91.129

IP3:192.168.91.130

 

2:下载MongoDB 2.6版本

https://fastdl.mongodb.org/win32/mongodb-win32-x86_64-2008plus-2.6.9.zip

 

3:解压

tar -zxvf mongodb-linux-x86_64-2.6.9.zip


mv mongodb-linux-x86_64-2.6.9 mongodb

  

技术分享

 

4:在mongodb目录创建 data目录和 log 目录 和conf配置文件

技术分享

同时保证data和log目录有可读写权限

技术分享

在3台服务器上做如上配置

5:编辑配置文件 vi mongo.conf

在IP1:192.168.91.128上

#日志目录
logpath=/root/software/mongodb/log/mongod

#数据目录
dbpath=/root/software/mongodb/data

#日志以追加方式添加
logappend=true

# 开启后台进程运行
fork = true

#端口
port = 27017

#副本集名称
replSet=gechongrepl/192.168.91.129:27017

  

在IP2:192.168.91.129上:

#日志目录
logpath=/root/software/mongodb/log/mongod

#数据目录
dbpath=/root/software/mongodb/data

#日志以追加方式添加
logappend=true

# 开启后台进程运行
fork = true

#端口
port = 27017

#副本集名称
#192.168.91.128#replSet=gechongrepl/192.168.91.129:27017
replSet=gechongrepl/192.168.91.128:27017

在IP3:192.168.91.130上

#日志目录
logpath=/root/software/mongodb/log/mongod

#数据目录
dbpath=/root/software/mongodb/data

#日志以追加方式添加
logappend=true

# 开启后台进程运行
fork = true

#端口
port = 27017

#副本集名称
#192.168.91.128#replSet=gechongrepl/192.168.91.129:27017
#192.168.91.129#replSet=gechongrepl/192.168.91.128:27017
replSet=gechongrepl/192.168.91.129:27017 , 192.168.91.128:27017

  

6:用如上的配置文件分别启动mongod

在IP1:192.168.91.128上启动mongod

> config_repl={_id:‘gechongrepl‘,members:[
... {_id:0,host:‘192.168.91.128:27017‘,priority:10},
... {_id:1,host:‘192.168.91.129:27017‘,priority:9},
... {_id:2,host:‘192.168.91.130:27017‘,priority:9}]}
{
	"_id" : "gechongrepl",
	"members" : [
		{
			"_id" : 0,
			"host" : "192.168.91.128:27017",
			"priority" : 10
		},
		{
			"_id" : 1,
			"host" : "192.168.91.129:27017",
			"priority" : 9
		},
		{
			"_id" : 2,
			"host" : "192.168.91.130:27017",
			"priority" : 9
		}
	]
}

  

> rs.initiate(config_repl);
{
	"startupStatus" : 4,
	"info" : "gechongrepl/192.168.91.129:27017",
	"ok" : 0,
	"errmsg" : "all members and seeds must be reachable to initiate set"
}

报错了。尝试在IP1上面:telnet 192.168.91.130 27017 发现不通。关掉这几台服务器的iptables重新执行如上命令。

> rs.initiate(config_repl);
{
	"info" : "Config now saved locally.  Should come online in about a minute.",
	"ok" : 1
}

至此,初始化已经OK。在上面3台机器任意一台完成初始化都可以的。

 

7:验证配置的副本集。

> rs.status()
{
	"set" : "gechongrepl",
	"date" : ISODate("2015-05-05T05:54:20Z"),
	"myState" : 1,
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.91.128:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 1936,
			"optime" : Timestamp(1430803801, 1),
			"optimeDate" : ISODate("2015-05-05T05:30:01Z"),
			"electionTime" : Timestamp(1430803801, 2),
			"electionDate" : ISODate("2015-05-05T05:30:01Z"),
			"self" : true
		},
		{
			"_id" : 1,
			"name" : "192.168.91.129:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 1459,
			"optime" : Timestamp(1430803801, 1),
			"optimeDate" : ISODate("2015-05-05T05:30:01Z"),
			"lastHeartbeat" : ISODate("2015-05-05T05:54:19Z"),
			"lastHeartbeatRecv" : ISODate("2015-05-05T05:54:19Z"),
			"pingMs" : 1,
			"syncingTo" : "192.168.91.128:27017"
		},
		{
			"_id" : 2,
			"name" : "192.168.91.130:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 1459,
			"optime" : Timestamp(1430803801, 1),
			"optimeDate" : ISODate("2015-05-05T05:30:01Z"),
			"lastHeartbeat" : ISODate("2015-05-05T05:54:19Z"),
			"lastHeartbeatRecv" : ISODate("2015-05-05T05:54:20Z"),
			"pingMs" : 1,
			"syncingTo" : "192.168.91.128:27017"
		}
	],
	"ok" : 1
}

技术分享

配置已经OK。

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。