[Mongodb] mongodb集群配置一主一从一仲裁


本文总阅读量

1、首先安装在三个节点安装mongodb,安装方法详见

mongodb单点安装

2、集群节点信息:

1
2
3
主:10.0.7.53
从:10.0.7.51
仲裁:10.0.7.50

3、启动主节点,进入主节点控制台,

3.1、创建管理员用户名、密码:

1
2
3
4
5
6
7
8
9
10
shell > /data/mongodb/bin/mongo --port 30000
MongoDB Enterprise testrepl:PRIMARY>use admin
MongoDB Enterprise testrepl:PRIMARY>
db.createUser(
{
user: "admin",
pwd: "abc123",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)

使用userAdminAnyDatabase的role权限去查看rs.status();会提示
“not authorized on admin to execute command”,建议授予root权限:

1
2
3
4
5
6
7
db.createUser(
{
user: "admin1",
pwd: "admin123",
roles: [ { role: "root", db: "admin" } ]
}
)

3.2、查看创建的管理员信息:

1
MongoDB Enterprise testrepl:PRIMARY> db.system.users.find()

3.3、退出控制台,修改配置文件mongodb.conf(每个节点都要配置):

1
2
security:
authorization: enabled

4、在主节点上,生成秘钥文件

4.1、生成密钥文件,用于集群之间互相通信:

1
openssl rand -base64 756 > /data/mongodb/mongodb.keyfile

4.2、更改文件权限以仅为文件所有者提供读取权限

1
chmod 400 /data/mongodb/mongodb.keyfile

4.3、将密钥文件复制到每个副本集成员

1
2
scp /data/mongodb/mongodb.keyfile root@10.0.7.51:/data/mongodb/
scp /data/mongodb/mongodb.keyfile root@10.0.7.50:/data/mongodb/

4.4、修改配置文件mongodb.conf,添加keyfile参数(每个节点都要配置):

1
2
security:
keyFile: /data/mongodb/mongodb.keyfile

5、配置集群

5.1、修改配置文件mongodb.conf,添加集群配置参数(每个节点都要配置):

1
2
replication:
replSetName: "testrepl"

replSetName:设置集群名称,根据自己需求自定义(三个节点要保持一直)

5.2、重新启动mongodb

1
/data/mongodb/bin/mongod -f /data/mongodb/mongodb.conf

5.3、进入主节点控制台

1
/data/mongodb/bin/mongo --port 30000

5.4、添加集群节点:

1
2
3
4
5
6
7
8
9
10
11
use admin;
db.auth('admin','abc123');

rs.initiate( {
_id : "testrepl",
members: [
{ _id: 0, host: "10.0.7.53:30000" },
{ _id: 1, host: "10.0.7.50:30000" },
{ _id: 2, host: "10.0.7.51:30000","arbiterOnly" : true}
]
})

执行结果如下:

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

5.5、查看集群状态

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
MongoDB Enterprise testrepl:PRIMARY> rs.status();
{
"set" : "testrepl",
"date" : ISODate("2018-08-04T13:09:25.894Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1533388160, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1533388160, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1533388160, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1533388160, 1),
"t" : NumberLong(1)
}
},
"lastStableCheckpointTimestamp" : Timestamp(1533388140, 1),
"members" : [
{
"_id" : 0,
"name" : "10.0.7.53:30000",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 289,
"optime" : {
"ts" : Timestamp(1533388160, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-08-04T13:09:20Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1533388139, 1),
"electionDate" : ISODate("2018-08-04T13:08:59Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "10.0.7.50:30000",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 37,
"optime" : {
"ts" : Timestamp(1533388160, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1533388160, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-08-04T13:09:20Z"),
"optimeDurableDate" : ISODate("2018-08-04T13:09:20Z"),
"lastHeartbeat" : ISODate("2018-08-04T13:09:25.051Z"),
"lastHeartbeatRecv" : ISODate("2018-08-04T13:09:25.677Z"),
"pingMs" : NumberLong(3),
"lastHeartbeatMessage" : "",
"syncingTo" : "10.0.7.53:30000",
"syncSourceHost" : "10.0.7.53:30000",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "10.0.7.51:30000",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 37,
"lastHeartbeat" : ISODate("2018-08-04T13:09:25.031Z"),
"lastHeartbeatRecv" : ISODate("2018-08-04T13:09:24.265Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1533388160, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1533388160, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}

5.6、从库默认为不能读写模式,如果要启动从库的读模式,进入从库执行以下命令:

1
rs.slaveOk();

5.7、增加一个仲裁节点,新增节点,配置文件中密钥文件、repl参数一定要保持一致:

1
rs.addArb("m1.example.net:27017")

5.8、添加一个新的从节点,添加之前设置该节点优先级为0,表决为0,以防止该节点数据未同步完成之前参与选举,待该节点数据同步完成之后,使用rs.reconfig()参数再修改其优先级和表决:

1
rs.add( { host: "mongodb3.example.net:27017", priority: 0, votes: 0 } )

rs.reconfig()命令执行如下:

1
2
3
4
var cfg = rs.conf();
cfg.members[4].priority = 1
cfg.members[4].votes = 1
rs.reconfig(cfg)
目录
  1. 1. 1、首先安装在三个节点安装mongodb,安装方法详见
  2. 2. 2、集群节点信息:
  3. 3. 3、启动主节点,进入主节点控制台,
  4. 4. 4、在主节点上,生成秘钥文件
  5. 5. 5、配置集群

Proudly powered by Hexo and Theme by Lap
本站访客数人次
© 2020 zeven0707's blog