一 前提

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


二 现有环境信息
--2.1 数据情况

 [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 节点信息

 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 创建数据目录

 mkdir -p /mongodb/data04


--3.2 创建新从节点配置文件

 touch /mongodb/data04/mongodb_27021.conf, 写入以下:

fork = true
port = 27021
dbpath = /mongodb/data04
logpath = /mongodb/data04/mongo.log
logappend = true
replSet = rs0

   


--3.3 启动新从节点

 [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 连接主节点

 [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 判断当前库是否是主节点

 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

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

   


--3.7 再次查看 Replica Set 配置

 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 登陆新节点

 [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 开启从节点只读

 rs0: