一 前提
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; rs0:PRIMARY> db.things.find(); |
--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 |
--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; |
--4.2 开启从节点只读
rs0: |
mongodb的replica set配置还是比较容易掌握的,不知道在真正使用过程中的调优会不会很复杂。。。