MongoDB副本集合

#简述:副本集合(Replica Sets),是一个基于主/从复制机制的复制功能,但增加了自动故障转移和恢复特性,一个集群最多
可以支持7个服务器,并且任意节点都可以是主节点。所有的写操作都被分发到主节点,而读操作可以在任何节点上进行;
#系统环境
OS			CentOS6.5
server1		192.168.3.100:27017
server1		192.168.3.100:27018
node1		192.168.3.101:27017
node2		192.168.3.102:27017
#配置副本集方法之一(自动选定某一台数据库为主数据库)
#server1启动节点
mkdir /application/mongodb/database /application/mongodb/logs
/application/mongodb/bin/mongod --rest --replSet fuben/192.168.3.100:27017 --port 27017 --dbpath=/application/mongodb/database --logpath=/application/mongodb/logs/mongodb.log --fork

#node1启动节点
mkdir /application/mongodb/database /application/mongodb/logs
/application/mongodb/bin/mongod --rest --replSet fuben/192.168.3.101:27017 --port 27017 --dbpath=/application/mongodb/database --logpath=/application/mongodb/logs/mongodb.log --fork

#node2启动节点
mkdir /application/mongodb/database /application/mongodb/logs
/application/mongodb/bin/mongod --rest --replSet fuben/192.168.3.102:27017 --port 27017 --dbpath=/application/mongodb/database --logpath=/application/mongodb/logs/mongodb.log --fork

#server1启动仲裁节点
mkdir /application/mongodb/arb
/application/mongodb/bin/mongod --rest --replSet fuben --dbpath=/application/mongodb/arb --port 27018 --logpath=/application/mongodb/logs/arb.log --fork
#Server1登陆mongo
/application/mongodb/bin/mongo --port 27017
#初始化节点(server1上操作)
> rs.initiate({_id:"fuben",members:[{_id:1,host:"192.168.3.100:27017"},{_id:2,host:"192.168.3.101:27017"},{_id:3,host:"192.168.3.102:27017"},{_id:4,host:"192.168.3.100:27018","arbiterOnly":true}]});
{
	"info" : "Config now saved locally.  Should come online in about a minute.",
	"ok" : 1
}
#查看副本集状态
fuben:PRIMARY> rs.status()
{
	"set" : "fuben",
	"date" : ISODate("2015-05-18T13:27:21Z"),
	"myState" : 1,
	"members" : [
		{
			"_id" : 1,
			"name" : "192.168.3.100:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",						#主
			"uptime" : 352,
			"optime" : Timestamp(1431955576, 1),
			"optimeDate" : ISODate("2015-05-18T13:26:16Z"),
			"electionTime" : Timestamp(1431955587, 1),
			"electionDate" : ISODate("2015-05-18T13:26:27Z"),
			"self" : true
		},
		{
			"_id" : 2,
			"name" : "192.168.3.101:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",					#从
			"uptime" : 64,
			"optime" : Timestamp(1431955576, 1),
			"optimeDate" : ISODate("2015-05-18T13:26:16Z"),
			"lastHeartbeat" : ISODate("2015-05-18T13:27:21Z"),
			"lastHeartbeatRecv" : ISODate("2015-05-18T13:27:20Z"),
			"pingMs" : 0,
			"syncingTo" : "192.168.3.100:27017"
		},
		{
			"_id" : 3,
			"name" : "192.168.3.102:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",					#从
			"uptime" : 62,
			"optime" : Timestamp(1431955576, 1),
			"optimeDate" : ISODate("2015-05-18T13:26:16Z"),
			"lastHeartbeat" : ISODate("2015-05-18T13:27:21Z"),
			"lastHeartbeatRecv" : ISODate("2015-05-18T13:27:21Z"),
			"pingMs" : 0,
			"syncingTo" : "192.168.3.100:27017"
		},
		{
			"_id" : 4,
			"name" : "192.168.3.100:27018",
			"health" : 1,
			"state" : 7,
			"stateStr" : "ARBITER",						#仲裁
			"uptime" : 62,
			"lastHeartbeat" : ISODate("2015-05-18T13:27:21Z"),
			"lastHeartbeatRecv" : ISODate("2015-05-18T13:27:21Z"),
			"pingMs" : 0
		}
	],
	"ok" : 1
}
#在PRIMARY上面操作,建库建表,从数据库只有读的权限
fuben:PRIMARY> show dbs;
local	1.078125GB
fuben:PRIMARY> use testdb;
switched to db testsb
fuben:PRIMARY> db.document01.insert({"name":"wsq","sex":"M","age":"20"});
fuben:PRIMARY> show tables;
document01
system.indexes
fuben:PRIMARY> db.document01.find();
{ "_id" : ObjectId("5511551aa51f85ff07b6df0c"), "name" : "wsq", "sex" : "M", "age" : "20" }

#node1登陆mongo
/application/mongodb/bin/mongo --port 27017

#设置从库可读(从库上执行)
fuben:SECONDARY> show dbs;
local	1.078125GB
testdb	0.203125GB
fuben:SECONDARY> use testdb;
switched to db testdb
fuben:SECONDARY> show tables;
Tue Mar 24 19:53:32.513 error: { "$err" : "not master and slaveOk=false", "code" : 13435 } at src/mongo/shell/query.js:128		#因为没有读的权限
fuben:SECONDARY> rs.slaveOk();			#如果不执行该步骤从库上不可读数据
fuben:SECONDARY> show tables;
document01
system.indexes
fuben:SECONDARY> db.document01.find();
{ "_id" : ObjectId("5511551aa51f85ff07b6df0c"), "name" : "wsq", "sex" : "M", "age" : "20" }
#配置副本集方法之二(手动选定某一台数据库为主数据库)
#server1启动节点
mkdir -p /mongodb/data/db /mongodb/data/logs
mongod --rest --replSet fuben/192.168.3.171:27017 --master --port 27017 --dbpath=/mongodb/data/db --logpath=/mongodb/data/logs/mongodb.log --fork

#node1启动节点
mkdir -p /mongodb/data/db /mongodb/data/logs
mongod --rest --replSet fuben/192.168.3.204:27017 --port 27017 --dbpath=/mongodb/data/db --logpath=/mongodb/data/logs/mongodb.log --fork

#node2启动节点
mkdir -p /mongodb/data/db /mongodb/data/logs
mongod --rest --replSet fuben/192.168.3.108:27017 --port 27017 --dbpath=/mongodb/data/db --logpath=/mongodb/data/logs/mongodb.log --fork
#初始化节点:(登陆其中任何一个节点操作皆可)
[root@server1 /]# mongo --port 27017
MongoDB shell version: 2.4.12
connecting to: 127.0.0.1:27017/test
> rs.initiate({_id:"fuben",members:[{_id:1,host:"192.168.3.171:27017",priority:2},{_id:2,host:"192.168.3.204:27017",priority:3},{_id:3,host:"192.168.3.108:27017",priority:4}]});
{
	"info" : "Config now saved locally.  Should come online in about a minute.",
	"ok" : 1
}
#priority 是设置优先级的,默认优先级为1,可以是1-1000的数字
#验证
fuben:PRIMARY> rs.status();
{
	"set" : "fuben",
	"date" : ISODate("2015-03-24T12:12:14Z"),
	"myState" : 1,
	"members" : [
		{
			"_id" : 1,
			"name" : "192.168.10.171:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",	#从
			"uptime" : 195,
			"optime" : Timestamp(1427198934, 1),
			"optimeDate" : ISODate("2015-03-24T12:08:54Z"),
			"lastHeartbeat" : ISODate("2015-03-24T12:12:13Z"),
			"lastHeartbeatRecv" : ISODate("2015-03-24T12:12:13Z"),
			"pingMs" : 0,
			"syncingTo" : "192.168.10.108:27017"
		},
		{
			"_id" : 2,
			"name" : "192.168.10.204:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",	#从
			"uptime" : 195,
			"optime" : Timestamp(1427198934, 1),
			"optimeDate" : ISODate("2015-03-24T12:08:54Z"),
			"lastHeartbeat" : ISODate("2015-03-24T12:12:13Z"),
			"lastHeartbeatRecv" : ISODate("2015-03-24T12:12:13Z"),
			"pingMs" : 0,
			"syncingTo" : "192.168.10.171:27017"
		},
		{
			"_id" : 3,
			"name" : "192.168.10.108:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",		#主
			"uptime" : 498,
			"optime" : Timestamp(1427198934, 1),
			"optimeDate" : ISODate("2015-03-24T12:08:54Z"),
			"self" : true
		}
	],
	"ok" : 1
}
#查看当前主库
fuben:SECONDARY> rs.isMaster();
{
	"setName" : "fuben",
	"setVersion" : 1,
	"ismaster" : false,
	"secondary" : true,
	"hosts" : [
		"192.168.3.102:27017",
		"192.168.3.101:27017",
		"192.168.3.100:27017"
	],
	"arbiters" : [
		"192.168.3.100:27018"
	],
	"primary" : "192.168.3.100:27017",
	"me" : "192.168.3.102:27017",
	"maxBsonObjectSize" : 16777216,
	"maxMessageSizeBytes" : 48000000,
	"maxWriteBatchSize" : 1000,
	"localTime" : ISODate("2015-05-18T13:36:41.854Z"),
	"maxWireVersion" : 2,
	"minWireVersion" : 0,
	"ok" : 1
}


你可能感兴趣的:(数据库,服务器,database,replica,server1,mongodb副本集合)