Mongodb单台机器多实例集群搭建

Mongodb单台机器多实例集群搭建

PS:因为条件有限,只能使用单机器多实例集群搭建,多机器配置一样

搭建一个三节点的集群,一个主副本节点一个辅助副本节点,还有一个仲裁节点,仲裁节点作用主要是在当一台机器DOWN掉时,投票机制还能正常运行,所以使用仲裁节点满足过半票数的要求。

一、预先准备配置:

  1. 三个节点使用不同的数据目录,所以预先创建三个数据目录:

​ /data/db1

​ /data/db2

​ /data/db3

  1. 还有三个日志目录

​ /var/log/mongod1

​ /var/log/mongod2

​ /var/log/mongod3

  1. 准备配置文件mongod1.conf如下:
 1 systemLog:
 2    destination: file
 3    path: "/var/log/mongodb1/mongod.log"
 4    logAppend: true
 5 storage:
 6    dbPath: /data/db1
 7    journal:
 8       enabled: true
 9 processManagement:
10    fork: true
11 security:
12    authorization: disabled
13 net:
14    bindIp: 127.0.0.1,192.168.70.128
15    port: 27017
16 setParameter:
17    enableLocalhostAuthBypass: false
18 replication:
19    replSetName: test

 

  1. 并复制两份分别为mongod2.conf和mongod3.conf 内容如下:

mongod2.conf :

 1 systemLog:
 2    destination: file
 3    path: "/var/log/mongodb2/mongod.log"
 4    logAppend: true
 5 storage:
 6    dbPath: /data/db2
 7    journal:
 8       enabled: true
 9 processManagement:
10    fork: true
11 security:
12    authorization: disabled
13 net:
14    bindIp: 127.0.0.1,192.168.70.128
15    port: 27018
16 setParameter:
17    enableLocalhostAuthBypass: false
18 replication:
19    replSetName: test

 

​ mongod3.conf :

systemLog:
   destination: file
   path: "/var/log/mongodb3/mongod.log"
   logAppend: true
storage:
   dbPath: /data/db3
   journal:
      enabled: true
processManagement:
   fork: true
security:
   authorization: disabled
net:
   bindIp: 127.0.0.1,192.168.70.128
   port: 27019
setParameter:
   enableLocalhostAuthBypass: false
replication:
   replSetName: test

 

  1. 然后根据配置文件分别启动三个实例 :

​ /mongod --config /conf/mongod1.conf

/mongod --config /conf/mongod2.conf

/mongod --config /conf/mongod3.conf

二、目前配置集群的前期准备工作已经完成。下面开始初始化副本集:

我们选择第一台机连接执行以下命令:

 1 cfg={
 2     _id:'test',
 3     members:[
 4         {_id:0,host:'192.168.70.128:27017'},
 5         {_id:1,host:'192.168.70.128:27018'},
 6         {_id:2,host:'192.168.70.128:27019',arbiterOnly:true}
 7     ]
 8 }
 9 
10 rs.initiate(cfg)

 

服务器返回表示成功:

 1 {
 2         "ok" : 1,
 3         "operationTime" : Timestamp(1531351517, 1),
 4         "$clusterTime" : {
 5                 "clusterTime" : Timestamp(1531351517, 1),
 6                 "signature" : {
 7                         "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
 8                         "keyId" : NumberLong(0)
 9                 }
10         }
11 }

 

执行rs.status()查看集群信息:

 1 {
 2         "set" : "test",
 3         "date" : ISODate("2018-07-11T23:25:32.808Z"),
 4         "myState" : 1,
 5         "term" : NumberLong(1),
 6         "heartbeatIntervalMillis" : NumberLong(2000),
 7         "optimes" : {
 8                 "lastCommittedOpTime" : {
 9                         "ts" : Timestamp(1531351531, 1),
10                         "t" : NumberLong(1)
11                 },
12                 "readConcernMajorityOpTime" : {
13                         "ts" : Timestamp(1531351531, 1),
14                         "t" : NumberLong(1)
15                 },
16                 "appliedOpTime" : {
17                         "ts" : Timestamp(1531351531, 1),
18                         "t" : NumberLong(1)
19                 },
20                 "durableOpTime" : {
21                         "ts" : Timestamp(1531351531, 1),
22                         "t" : NumberLong(1)
23                 }
24         },
25         "members" : [
26                 {
27                         "_id" : 0,
28                         "name" : "192.168.70.128:27017",
29                         "health" : 1,
30                         "state" : 1,
31                         "stateStr" : "PRIMARY",
32                         "uptime" : 194,
33                         "optime" : {
34                                 "ts" : Timestamp(1531351531, 1),
35                                 "t" : NumberLong(1)
36                         },
37                         "optimeDate" : ISODate("2018-07-11T23:25:31Z"),
38                         "infoMessage" : "could not find member to sync from",
39                         "electionTime" : Timestamp(1531351528, 1),
40                         "electionDate" : ISODate("2018-07-11T23:25:28Z"),
41                         "configVersion" : 1,
42                         "self" : true
43                 },
44                 {
45                         "_id" : 1,
46                         "name" : "192.168.70.128:27018",
47                         "health" : 1,
48                         "state" : 2,
49                         "stateStr" : "SECONDARY",
50                         "uptime" : 15,
51                         "optime" : {
52                                 "ts" : Timestamp(1531351517, 1),
53                                 "t" : NumberLong(-1)
54                         },
55                         "optimeDurable" : {
56                                 "ts" : Timestamp(1531351517, 1),
57                                 "t" : NumberLong(-1)
58                         },
59                         "optimeDate" : ISODate("2018-07-11T23:25:17Z"),
60                         "optimeDurableDate" : ISODate("2018-07-11T23:25:17Z"),
61                         "lastHeartbeat" : ISODate("2018-07-11T23:25:30.816Z"),
62                         "lastHeartbeatRecv" : ISODate("2018-07-11T23:25:30.121Z"),
63                         "pingMs" : NumberLong(0),
64                         "configVersion" : 1
65                 },
66                 {
67                         "_id" : 2,
68                         "name" : "192.168.70.128:27019",
69                         "health" : 1,
70                         "state" : 7,
71                         "stateStr" : "ARBITER",
72                         "uptime" : 15,
73                         "lastHeartbeat" : ISODate("2018-07-11T23:25:30.816Z"),
74                         "lastHeartbeatRecv" : ISODate("2018-07-11T23:25:29.592Z"),
75                         "pingMs" : NumberLong(0),
76                         "configVersion" : 1
77                 }
78         ],
79         "ok" : 1,
80         "operationTime" : Timestamp(1531351531, 1),
81         "$clusterTime" : {
82                 "clusterTime" : Timestamp(1531351531, 1),
83                 "signature" : {
84                         "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
85                         "keyId" : NumberLong(0)
86                 }
87         }
88 }

 

信息中显示27017为主节点,27018为辅助节点,27019为仲裁节点,

试着把当前主节点关闭验证集群状态,在27017执行db.shutdownServer();

关闭成功后,登录27018执行rs.status()查看集群状态:

 1 test:PRIMARY> rs.status()rs.status()
 2 {
 3         "set" : "test",
 4         "date" : ISODate("2018-07-11T23:35:05.728Z"),
 5         "myState" : 1,
 6         "term" : NumberLong(2),
 7         "heartbeatIntervalMillis" : NumberLong(2000),
 8         "optimes" : {
 9                 "lastCommittedOpTime" : {
10                         "ts" : Timestamp(1531352009, 1),
11                         "t" : NumberLong(1)
12                 },
13                 "readConcernMajorityOpTime" : {
14                         "ts" : Timestamp(1531352009, 1),
15                         "t" : NumberLong(1)
16                 },
17                 "appliedOpTime" : {
18                         "ts" : Timestamp(1531352101, 1),
19                         "t" : NumberLong(2)
20                 },
21                 "durableOpTime" : {
22                         "ts" : Timestamp(1531352101, 1),
23                         "t" : NumberLong(2)
24                 }
25         },
26         "members" : [
27                 {
28                         "_id" : 0,
29                         "name" : "192.168.70.128:27017",
30                         "health" : 0,
31                         "state" : 8,
32                         "stateStr" : "(not reachable/healthy)",
33                         "uptime" : 0,
34                         "optime" : {
35                                 "ts" : Timestamp(0, 0),
36                                 "t" : NumberLong(-1)
37                         },
38                         "optimeDurable" : {
39                                 "ts" : Timestamp(0, 0),
40                                 "t" : NumberLong(-1)
41                         },
42                         "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
43                         "optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
44                         "lastHeartbeat" : ISODate("2018-07-11T23:35:04.928Z"),
45                         "lastHeartbeatRecv" : ISODate("2018-07-11T23:33:29.998Z"),
46                         "pingMs" : NumberLong(0),
47                         "lastHeartbeatMessage" : "Connection refused",
48                         "configVersion" : -1
49                 },
50                 {
51                         "_id" : 1,
52                         "name" : "192.168.70.128:27018",
53                         "health" : 1,
54                         "state" : 1,
55                         "stateStr" : "PRIMARY",
56                         "uptime" : 660,
57                         "optime" : {
58                                 "ts" : Timestamp(1531352101, 1),
59                                 "t" : NumberLong(2)
60                         },
61                         "optimeDate" : ISODate("2018-07-11T23:35:01Z"),
62                         "infoMessage" : "could not find member to sync from",
63                         "electionTime" : Timestamp(1531352020, 1),
64                         "electionDate" : ISODate("2018-07-11T23:33:40Z"),
65                         "configVersion" : 1,
66                         "self" : true
67                 },
68                 {
69                         "_id" : 2,
70                         "name" : "192.168.70.128:27019",
71                         "health" : 1,
72                         "state" : 7,
73                         "stateStr" : "ARBITER",
74                         "uptime" : 586,
75                         "lastHeartbeat" : ISODate("2018-07-11T23:35:04.877Z"),
76                         "lastHeartbeatRecv" : ISODate("2018-07-11T23:35:04.854Z"),
77                         "pingMs" : NumberLong(0),
78                         "configVersion" : 1
79                 }
80         ],
81         "ok" : 1,
82         "operationTime" : Timestamp(1531352101, 1),
83         "$clusterTime" : {
84                 "clusterTime" : Timestamp(1531352101, 1),
85                 "signature" : {
86                         "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
87                         "keyId" : NumberLong(0)
88                 }
89         }
90 }

 

可以看到27018节点已经提升为PRIMARY节点 ,自动提升主节点正常,

重新启动27017节点,执行rs.status()可以看到节点启动后自动成为SECONDARY节点

到此集群配置成功

后面可以各自扩展配置信息,比如身份验证,oplog默认大小

posted @ 2020-06-11 17:21  回青  阅读(1318)  评论(0编辑  收藏  举报