MongoDB:Replica Set 增加节点

前一篇 blog 介绍了三节点 Replica Set 环境的搭建,那么出于各种原因,可能需要增加 mongdb 节点,例如原来是单节点,为了具有高可用,需要增加节点,那么接下来学习增加节点的操作:

一 准备

  1. 已经有一套 Replica Set 环境:
  2. 具有适当容量的另一套 mongodb 系统,以满足数据存储需求,并且网络通;

二 现有环境信息

2.1 数据情况

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
[mongo@redhatB mongodb]$ mongo 127.0.0.1:27018  
MongoDB shell version: 2.2.1
connecting to: 127.0.0.1:27018/test

rs0:PRIMARY> show collections;
system.indexes
test_1
things

rs0:PRIMARY> show dbs;
local 0.078125GB
test 0.0625GB

rs0:PRIMARY> db.things.find();
{ "_id" : ObjectId("50ae202524a46399c488c0c6"), "id" : 1, "name" : "aaa" }
{ "_id" : ObjectId("50ae202524a46399c488c0c7"), "id" : 2, "name" : "aaa" }
{ "_id" : ObjectId("50ae202524a46399c488c0c8"), "id" : 3, "name" : "aaa" }
{ "_id" : ObjectId("50ae202524a46399c488c0c9"), "id" : 4, "name" : "aaa" }
{ "_id" : ObjectId("50ae202524a46399c488c0ca"), "id" : 5, "name" : "aaa" }
{ "_id" : ObjectId("50ae202524a46399c488c0cb"), "id" : 6, "name" : "aaa" }
{ "_id" : ObjectId("50ae202524a46399c488c0cc"), "id" : 7, "name" : "aaa" }
{ "_id" : ObjectId("50ae202524a46399c488c0cd"), "id" : 8, "name" : "aaa" }
{ "_id" : ObjectId("50ae202524a46399c488c0ce"), "id" : 9, "name" : "aaa" }
{ "_id" : ObjectId("50ae202524a46399c488c0cf"), "id" : 10, "name" : "aaa" }
{ "_id" : ObjectId("50ae202524a46399c488c0d0"), "id" : 11, "name" : "aaa" }
{ "_id" : ObjectId("50ae202524a46399c488c0d1"), "id" : 12, "name" : "aaa" }
{ "_id" : ObjectId("50ae202524a46399c488c0d2"), "id" : 13, "name" : "aaa" }
{ "_id" : ObjectId("50ae202524a46399c488c0d3"), "id" : 14, "name" : "aaa" }
{ "_id" : ObjectId("50ae202524a46399c488c0d4"), "id" : 15, "name" : "aaa" }
{ "_id" : ObjectId("50ae202524a46399c488c0d5"), "id" : 16, "name" : "aaa" }
{ "_id" : ObjectId("50ae202524a46399c488c0d6"), "id" : 17, "name" : "aaa" }
{ "_id" : ObjectId("50ae202524a46399c488c0d7"), "id" : 18, "name" : "aaa" }
{ "_id" : ObjectId("50ae202524a46399c488c0d8"), "id" : 19, "name" : "aaa" }
{ "_id" : ObjectId("50ae202524a46399c488c0d9"), "id" : 20, "name" : "aaa" }

2.2 Replica Set 节点信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
rs0:PRIMARY> rs.conf();  
{
"_id" : "rs0",
"version" : 3,
"members" : [
{
"_id" : 0,
"host" : "redhatB.example.com:27018"
},
{
"_id" : 1,
"host" : "redhatB.example.com:27019"
},
{
"_id" : 2,
"host" : "redhatB.example.com:27020"
}
]
}

备注:从上面看出,目前 Replica Set 共有 3 节点。

三 增加节点

3.1 创建数据目录

1
mkdir -p /mongodb/data04

3.2 创建新从节点配置文件

1
2
3
4
5
6
7
touch /mongodb/data04/mongodb_27021.conf, 写入以下:
fork = true
port = 27021
dbpath = /mongodb/data04
logpath = /mongodb/data04/mongo.log
logappend = true
replSet = rs0

3.3 启动新从节点

1
2
3
4
[mongo@redhatB mongodb]$ mongod -f /mongodb/data04/mongodb_27021.conf  
forked process: 11733
all output going to: /mongodb/data04/mongo.log
child process started successfully, parent exiting

3.4 连接主节点

1
2
3
4
[mongo@redhatB mongodb]$ mongo 127.0.0.1:27018  
MongoDB shell version: 2.2.1
connecting to: 127.0.0.1:27018/test
rs0:PRIMARY>

备注:根据“rs0:PRIMARY”标识,即可确认为主节点,也可通过以下命令确认是否是主节点。

3.5 判断当前库是否是主节点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
rs0:PRIMARY> rs.isMaster();{  
"setName" : "rs0",
"ismaster" : true,
"secondary" : false,
"hosts" : [
"redhatB.example.com:27018",
"redhatB.example.com:27020",
"redhatB.example.com:27019"
],
"primary" : "redhatB.example.com:27018",
"me" : "redhatB.example.com:27018",
"maxBsonObjectSize" : 16777216,
"localTime" : ISODate("2012-11-22T13:04:36.501Z"),
"ok" : 1
}

3.6 增加新从节点到 Replica Set

1
2
rs0:PRIMARY> rs.add("redhatB.example.com:27021");  
{ "ok" : 1 }

3.7 再次查看 Replica Set 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
rs0:PRIMARY> rs.conf();  
{
"_id" : "rs0",
"version" : 4,
"members" : [
{
"_id" : 0,
"host" : "redhatB.example.com:27018"
},
{
"_id" : 1,
"host" : "redhatB.example.com:27019"
},
{
"_id" : 2,
"host" : "redhatB.example.com:27020"
},
{
"_id" : 3,
"host" : "redhatB.example.com:27021"
}
]
}

备注:新节点已经加入 Replica Set 了,到了这步已完成增加节点所有步骤,接下来验证下新节点。

四 测试

4.1 登陆新节点

1
2
3
4
5
6
7
8
9
[mongo@redhatB mongodb]$ mongo 127.0.0.1:27021  
MongoDB shell version: 2.2.1
connecting to: 127.0.0.1:27021/test
rs0:SECONDARY> show dbs;
local 0.125GB
test 0.0625GB

rs0:SECONDARY> show collections;
Thu Nov 22 21:08:20 uncaught exception: error: { "$err" : "not master and slaveOk=false", "code" : 13435 }

4.2 开启从节点只读

1
rs0:SECONDARY> rs.slars.slaveOk();

4.3 查看是否有数据

1
2
3
4
5
6
rs0:SECONDARY> show collections;  
system.indexes
test_1
things
rs0:SECONDARY> db.things.count();
30

备注:增加节点后,新节点会自动从主节点复制数据。

四 参考

http://docs.mongodb.org/manual/tutorial/expand-replica-set/#procedure-assumption-add-member-rs
http://docs.mongodb.org/manual/administration/replica-sets/#replica-set-admin-procedure-add-member

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/237922.html

(0)
上一篇 2022年1月29日
下一篇 2022年1月29日

相关推荐

发表回复

登录后才能评论