mongodb replica set配置与mongoose连接实例

mongodb的replica set是一个强大的功能,可以实现读写分离功能,有heartbeat功能,可以实现自动故障转移,比master-slave更强大。

配置

主机1

保存在/etc/mongodb_1.conf

1
systemLog:
   path: "/var/log/mongodb/mongodb_1.log"
   logAppend: true
storage:
   journal:
      enabled: true
   dbPath: "/var/lib/mongodb_1"
processManagement:
   fork: true
   pidFilePath: "/var/run/mongodb/mongodb_1.pid"
net:
   bindIp: 127.0.0.1
   port: 27017
setParameter:
   enableLocalhostAuthBypass: false
replication:
   oplogSizeMB: 1000
   replSetName: 'local-test'

主机2

保存在/etc/mongodb_2.conf

1
systemLog:
   path: "/var/log/mongodb/mongodb_2.log"
   logAppend: true
storage:
   journal:
      enabled: true
   dbPath: "/var/lib/mongodb_2"
processManagement:
   fork: true
   pidFilePath: "/var/run/mongodb/mongodb_2.pid"
net:
   bindIp: 127.0.0.1
   port: 27017
setParameter:
   enableLocalhostAuthBypass: false
replication:
   oplogSizeMB: 1000
   replSetName: 'local-test'

主机3

保存在/etc/mongodb_3.conf

1
systemLog:
   path: "/var/log/mongodb/mongodb_3.log"
   logAppend: true
storage:
   journal:
      enabled: true
   dbPath: "/var/lib/mongodb_3"
processManagement:
   fork: true
   pidFilePath: "/var/run/mongodb/mongodb_3.pid"
net:
   bindIp: 127.0.0.1
   port: 27017
setParameter:
   enableLocalhostAuthBypass: false
replication:
   oplogSizeMB: 1000
   replSetName: 'local-test'

启动

1
mongod -f /etc/mongodb.conf  
mongod -f /etc/mongodb_2.conf  
mongod -f /etc/mongodb_3.conf

设置

通过mongo命令,连接某一台机器,输入下面的配置命令,priority用来标示机器的优先权,优先权高的机器为primary机器,其他机器为secondary,当primary机器故障时,replica set 自动选择剩下机器中priority最高的作为primary机器。

1
// 配置信息
var config = {_id:"repmore",members:[{_id:0,host:'127.0.0.1:27017',priority :2},{_id:1,host:'127.0.0.1:27018',priority:1},{_id:2,host:'127.0.0.1:27019',priority:1}]};
// replica set 初始化
rs.initiate(config);

测试

目前,通过mongo链接三台机器,会发现27017位primiary,其他为secondary。

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
local-test:SECONDARY> rs.slaveOk()
local-test:SECONDARY> rs.status()
{
"set" : "local-test",
"date" : ISODate("2016-05-15T23:56:34.146Z"),
"myState" : 2,
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 43848,
"optime" : Timestamp(1463323447, 1),
"optimeDate" : ISODate("2016-05-15T14:44:07Z"),
"lastHeartbeat" : ISODate("2016-05-15T23:56:32.643Z"),
"lastHeartbeatRecv" : ISODate("2016-05-15T23:56:32.641Z"),
"pingMs" : 0,
"electionTime" : Timestamp(1463312747, 1),
"electionDate" : ISODate("2016-05-15T11:45:47Z"),
"configVersion" : 1
},
{
"_id" : 1,
"name" : "127.0.0.1:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 48134,
"optime" : Timestamp(1463323447, 1),
"optimeDate" : ISODate("2016-05-15T14:44:07Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 2,
"name" : "127.0.0.1:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 45603,
"optime" : Timestamp(1463323447, 1),
"optimeDate" : ISODate("2016-05-15T14:44:07Z"),
"lastHeartbeat" : ISODate("2016-05-15T23:56:32.279Z"),
"lastHeartbeatRecv" : ISODate("2016-05-15T23:56:32.279Z"),
"pingMs" : 0,
"configVersion" : 1
}
],
"ok" : 1
}

我们kill掉primary机器后,27018、27019会随机选择一台变为primary,因为他们两者的priority是一样的。

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
local-test:SECONDARY> rs.status()
{
"set" : "local-test",
"date" : ISODate("2016-05-15T23:58:06.082Z"),
"myState" : 2,
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : Timestamp(0, 0),
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2016-05-15T23:58:04.925Z"),
"lastHeartbeatRecv" : ISODate("2016-05-15T23:57:56.777Z"),
"pingMs" : 0,
"lastHeartbeatMessage" : "Failed attempt to connect to 127.0.0.1:27017; couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed",
"configVersion" : -1
},
{
"_id" : 1,
"name" : "127.0.0.1:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 48226,
"optime" : Timestamp(1463323447, 1),
"optimeDate" : ISODate("2016-05-15T14:44:07Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 2,
"name" : "127.0.0.1:27019",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 45695,
"optime" : Timestamp(1463323447, 1),
"optimeDate" : ISODate("2016-05-15T14:44:07Z"),
"lastHeartbeat" : ISODate("2016-05-15T23:58:04.452Z"),
"lastHeartbeatRecv" : ISODate("2016-05-15T23:58:04.451Z"),
"pingMs" : 0,
"electionTime" : Timestamp(1463356678, 1),
"electionDate" : ISODate("2016-05-15T23:57:58Z"),
"configVersion" : 1
}
],
"ok" : 1
}

mongoose链接实例

1
'use strict';

var mongoose = require('mongoose');

mongoose.connect('mongodb://127.0.0.1:27017/test', {
	db: { native_parser: true },
  	server: { poolSize: 5 },
  	replset: { rs_name: 'local-test' }
}, function(err) {
	if (err) throw err;
});

var Cat = mongoose.model('Cat', { name: String });

var kitty = new Cat({ name: 'Zildjian' });
kitty.save(function (err) {
  if (err) {
    console.log(err);
  } else {
    console.log('meow');
  }
});

Cat.findOne({}, function(err, data) {
	if (err) throw err;
	console.log(data);
});
文章目录
  1. 1. 配置
    1. 1.1. 主机1
    2. 1.2. 主机2
    3. 1.3. 主机3
  2. 2. 启动
  3. 3. 设置
  4. 4. 测试
  5. 5. mongoose链接实例
,