注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

PostgreSQL 中文网

 
 
 

日志

 
 

MongoDB:Replica Set 增加节点  

2012-11-27 15:15:27|  分类: MongoDB |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

 

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


一 前提
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:SECONDARY> rs.slars.slaveOk();
   

--4.3 查看是否有数据

 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  

  评论这张
 
阅读(14990)| 评论(1)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016