MongoDB--集群

为什么需要集群

  • 为了让数据安全
  • 高(24* 7)数据可用性
  • 灾难恢复
  • 无停机维护(如备份,索引重建,压实)
  • 读缩放(额外的副本读取)
  • 副本集对应用程序是透明

设置集群

准备工作

在MongoDB的集群是需要奇数个节点才能正常的运行,我们这里设置一个最小的节点的集群:1个主节点和2个从节点

  1. 分别建立三个文件夹来存放MongoDB的三个节点的文件

  1. 设置mongod.cfg文件

    systemLog:
        destination: file
        path: D:\Program Files\MongoDB\Server\3.0\0\log\mongod.log
    storage:
        dbPath: D:\Program Files\MongoDB\Server\3.0\0\blogDB
    #网络
    net:
        port: 27017
    #    bindIp: 127.0.0.1,192.168.1.106 #默认是全部IP
    #授权
    security:
        authorization: disabled
    #    keyFile: D:\Program Files\MongoDB\Server\3.0\rsKey #用于集群的验证,建议使用ssl,我为了简单,rsKey只是一个普通的文本
    #集群
    replication:
        replSetName: rs0 #集群名

三个节点的配置除了端口和路径需要改改,其他基本一致,我的三个节点的端口分别为:27017,27018,27019

启动集群

分别启动三个MongoDB实例,具体怎么启动,请参考我的MongoDB——基础入门

配置集群

C:\Users\Tengit>mongo
MongoDB shell version: 3.0.6
connecting to: test
> rs.initiate()
{
        "info2" : "no configuration explicitly specified -- making one",
        "me" : "tengit-2:27017",
        "ok" : 1
}
rs0:PRIMARY> rs.add("tengit-2:27018")
{ "ok" : 1 }
rs0:PRIMARY> rs.add("tengit-2:27019")
{ "ok" : 1 }
rs0:PRIMARY> rs.status()
{
        "set" : "rs0",
        "date" : ISODate("2015-10-14T02:51:00.788Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "tengit-2:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 122,
                        "optime" : Timestamp(1444791053, 1),
                        "optimeDate" : ISODate("2015-10-14T02:50:53Z"),
                        "electionTime" : Timestamp(1444790978, 2),
                        "electionDate" : ISODate("2015-10-14T02:49:38Z"),
                        "configVersion" : 3,
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "tengit-2:27018",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 32,
                        "optime" : Timestamp(1444791053, 1),
                        "optimeDate" : ISODate("2015-10-14T02:50:53Z"),
                        "lastHeartbeat" : ISODate("2015-10-14T02:50:59.873Z"),
                        "lastHeartbeatRecv" : ISODate("2015-10-14T02:51:00.118Z"
),
                        "pingMs" : 0,
                        "syncingTo" : "tengit-2:27017",
                        "configVersion" : 3
                },
                {
                        "_id" : 2,
                        "name" : "tengit-2:27019",
                        "health" : 1,
                        "state" : 5,
                        "stateStr" : "STARTUP2",
                        "uptime" : 6,
                        "optime" : Timestamp(0, 0),
                        "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
                        "lastHeartbeat" : ISODate("2015-10-14T02:50:59.871Z"),
                        "lastHeartbeatRecv" : ISODate("2015-10-14T02:51:00.138Z"
),
                        "pingMs" : 0,
                        "configVersion" : 3
                }
        ],
        "ok" : 1
}
rs0:PRIMARY> rs.status()
{
        "set" : "rs0",
        "date" : ISODate("2015-10-14T02:51:50.492Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "tengit-2:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 172,
                        "optime" : Timestamp(1444791053, 1),
                        "optimeDate" : ISODate("2015-10-14T02:50:53Z"),
                        "electionTime" : Timestamp(1444790978, 2),
                        "electionDate" : ISODate("2015-10-14T02:49:38Z"),
                        "configVersion" : 3,
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "tengit-2:27018",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 82,
                        "optime" : Timestamp(1444791053, 1),
                        "optimeDate" : ISODate("2015-10-14T02:50:53Z"),
                        "lastHeartbeat" : ISODate("2015-10-14T02:51:49.886Z"),
                        "lastHeartbeatRecv" : ISODate("2015-10-14T02:51:50.125Z"
),
                        "pingMs" : 0,
                        "syncingTo" : "tengit-2:27017",
                        "configVersion" : 3
                },
                {
                        "_id" : 2,
                        "name" : "tengit-2:27019",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 56,
                        "optime" : Timestamp(1444791053, 1),
                        "optimeDate" : ISODate("2015-10-14T02:50:53Z"),
                        "lastHeartbeat" : ISODate("2015-10-14T02:51:49.886Z"),
                        "lastHeartbeatRecv" : ISODate("2015-10-14T02:51:50.150Z"
),
                        "pingMs" : 0,
                        "configVersion" : 3
                }
        ],
        "ok" : 1
}
rs0:PRIMARY>

附带一份C#访问MongoDB的测试程序,点击下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值