MongoDB Replica Set集群

NoSQL

MongoDB副本集自动复制 Replica Set是Mongodb官方推荐的主从复制和高可用方案。Replicat Set具有自动切换功能,当Primary挂掉之后,可以自动由Replica Set中的某一个Secondary来切换到Primary,以实现高可用的目的。下面看一下MongoDB Replica Set集群的具体步骤。

1、创建MongoDB数据文件夹

1
2
[root@MongoDb ~]# mkdir /mongodb && cd /mongodb/
[root@MongoDb mongodb]# mkdir node1 node2 node3

2、以副本集mongos启动MongoDB

1
2
3
4
5
6
7
8
[root@MongoDb mongodb]# /usr/local/bin/mongod --dbpath /mongodb/node3 --port 10003 --replSet mongos --fork --logpath /mongodb/node3/node3.log
[root@MongoDb mongodb]# /usr/local/bin/mongod --dbpath /mongodb/node2 --port 10002 --replSet mongos --fork --logpath /mongodb/node2/node2.log
[root@MongoDb mongodb]# /usr/local/bin/mongod --dbpath /mongodb/node1 --port 10001 --replSet mongos --fork --logpath /mongodb/node1/node1.log
[root@MongoDb mongodb]# ps -ef | grep mongod #查看mongod进程
root 3956 1 1 15:21 ? 00:00:23 /usr/local/bin/mongodd --dbpath /mongodb/node2 --port 10002 --replSet mongos --fork --logpath /mongodb/node2/node2.log
root 3970 1 1 15:21 ? 00:00:22 /usr/local/bin/mongodd --dbpath /mongodb/node1 --port 10001 --replSet mongos --fork --logpath /mongodb/node1/node1.log
root 4274 1 1 15:24 ? 00:00:21 /usr/local/bin/mongodd --dbpath /mongodb/node3 --port 10003 --replSet mongos --fork --logpath /mongodb/node3/node3.log
root 4959 1116 0 15:50 pts/0 00:00:00 grep mongod

3、初始化副本集mongos(只能初始化一次)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
[root@MongoDb mongodb]# /usr/local/bin/mongod localhost:10003
> rs.initiate({_id:"mongos",members:[{_id:1,host:"localhost:10001"},{_id:2,host:"localhost:10002"},{_id:3,host:"localhost:10003"}]})
> rs.status();//查看副本集mongos的状态,也可以使用db.isMaster()查看集群中的主从节点信息。
{
"set" : "mongos",
"date" : ISODate("2015-10-28T07:53:00.240Z"),
"myState" : 2,
"members" : [
{
"_id" : 1,
"name" : "localhost:10001",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY", #主节点
"uptime" : 1696,
"optime" : Timestamp(1446017006, 2),
"optimeDate" : ISODate("2015-10-28T07:23:26Z"),
"lastHeartbeat" : ISODate("2015-10-28T07:52:58.600Z"),
"lastHeartbeatRecv" : ISODate("2015-10-28T07:52:58.728Z"),
"pingMs" : 1,
"electionTime" : Timestamp(1446017034, 1),
"electionDate" : ISODate("2015-10-28T07:23:54Z"),
"configVersion" : 1
},
{
"_id" : 2,
"name" : "localhost:10002",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY", #从节点
"uptime" : 1696,
"optime" : Timestamp(1446017006, 2),
"optimeDate" : ISODate("2015-10-28T07:23:26Z"),
"lastHeartbeat" : ISODate("2015-10-28T07:52:58.611Z"),
"lastHeartbeatRecv" : ISODate("2015-10-28T07:52:58.793Z"),
"pingMs" : 1,
"lastHeartbeatMessage" : "could not find member to sync from",
"configVersion" : 1
},
{
"_id" : 3,
"name" : "localhost:10003",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY", #从节点
"uptime" : 1697,
"optime" : Timestamp(1446017006, 2),
"optimeDate" : ISODate("2015-10-28T07:23:26Z"),
"configVersion" : 1,
"self" : true
}
],
"ok" : 1
}
mongos:PRIMARY> db.test.insert({name:"雷雪松"})

注意:
state: 1表示当前可以进行读写,2表示不能读写
health: 1表示是正常的,0异常
只用PRIMARY才可以读写,SECONDARY会提示”not master”不能读写操作。在从节点中使用rs.slaveOk(),就可以执行读操作了。

4、删除Replica Set节点

1
mongos:PRIMARY> rs.remove("localhost:10001")

5、添加Replica Set节点

1
mongos:PRIMARY> rs.add("localhost:10001")

6、停止PRIMARY主节点,查看自动切换。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
[root@MongoDb mongodb]# kill 3970
[root@MongoDb mongodb]# /usr/local/bin/mongod localhost:10003
mongos:SECONDARY> rs.status();
{
"set" : "mongos",
"date" : ISODate("2015-10-28T07:54:10.272Z"),
"myState" : 2,
"members" : [
{
"_id" : 1,
"name" : "localhost:10001",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)", #节点不可用或者不健康
"uptime" : 0,
"optime" : Timestamp(0, 0),
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2015-10-28T07:54:08.706Z"),
"lastHeartbeatRecv" : ISODate("2015-10-28T07:54:00.819Z"),
"pingMs" : 1,
"lastHeartbeatMessage" : "Failed attempt to connect to localhost:10001; couldn't connect to server localhost:10001 (127.0.0.1), connection attempt failed",
"configVersion" : -1
},
{
"_id" : 2,
"name" : "localhost:10002",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY", #主节点
"uptime" : 1766,
"optime" : Timestamp(1446017006, 2),
"optimeDate" : ISODate("2015-10-28T07:23:26Z"),
"lastHeartbeat" : ISODate("2015-10-28T07:54:08.699Z"),
"lastHeartbeatRecv" : ISODate("2015-10-28T07:54:08.870Z"),
"pingMs" : 0,
"electionTime" : Timestamp(1446018843, 1),
"electionDate" : ISODate("2015-10-28T07:54:03Z"),
"configVersion" : 1
},
{
"_id" : 3,
"name" : "localhost:10003",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY", #从节点
"uptime" : 1767,
"optime" : Timestamp(1446017006, 2),
"optimeDate" : ISODate("2015-10-28T07:23:26Z"),
"configVersion" : 1,
"self" : true
}
],
"ok" : 1
}
mongos:PRIMARY> db.test.find();
{ "_id" : ObjectId("56307fc46b9990f94c860ac2"), "name" : "雷雪松" }

当节点恢复后,会自动成为SECONDARY从节点。

来源:MongoDB Replica Set集群

发表评论

电子邮件地址不会被公开。 必填项已用*标注