码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2015-05-05 15:45:20      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

接上一篇博客:

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。

 

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

标签:

原文地址:http://www.cnblogs.com/xiaoit/p/4478951.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!