MongoDB-Replica-Set

MongoDB的Replication是一种确保冗余、备份和自动故障转移的机制。测试环境中一般做3个复制,1个是主要的,另外两个是二级的。

构建复制数据集

构建包括3个复制的数据集,数据集的名称是rs0。
先建立存储3个复制的目录

sudo mkdir -p /data/db/rs0-0 /data/db/rs0-1 /data/db/rs0-2

然后分别在3个不同的命令行窗口输入,以下命令启动mongod

sudo /opt/mongodb/bin/mongod --port 27001 --dbpath /data/db/rs0-0 --replSet rs0
sudo /opt/mongodb/bin/mongod --port 27002 --dbpath /data/db/rs0-1 --replSet rs0
sudo /opt/mongodb/bin/mongod --port 27003 --dbpath /data/db/rs0-2 --replSet rs0

连接到其中的一个mongod,设置配置信息(使用rs.initiate())

wendyeq@wendyeq-mint /data/db $ /opt/mongodb/bin/mongo --port 27001
MongoDB shell version: 2.2.3
connecting to: 127.0.0.1:27001/test
> rsconf={_id:"rs0",members:[{_id:0,host:"localhost:27001"},{_id:1,host:"localhost:27002"},{_id:2,host:"localhost:27003"}]}
{
    "_id" : "rs0",
    "members" : [
        {
            "_id" : 0,
            "host" : "localhost:27001"
        },
        {
            "_id" : 1,
            "host" : "localhost:27002"
        },
        {
            "_id" : 2,
            "host" : "localhost:27003"
        }
    ]
}
> rs.initiate(rsconf)
{
    "info" : "Config now saved locally.  Should come online in about a minute.",
    "ok" : 1
}
> 

使用rs.status()查看状态,用db.isMaster()验证是否是主要数据集。

> rs.status()
{
    "set" : "rs0",
    "date" : ISODate("2013-03-10T12:51:04Z"),
    "myState" : 1,
    "members" : [
        {
            "_id" : 0,
            "name" : "localhost:27001",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 385,
            "optime" : Timestamp(1362919747000, 1),
            "optimeDate" : ISODate("2013-03-10T12:49:07Z"),
            "self" : true
        },
        {
            "_id" : 1,
            "name" : "localhost:27002",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 114,
            "optime" : Timestamp(1362919747000, 1),
            "optimeDate" : ISODate("2013-03-10T12:49:07Z"),
            "lastHeartbeat" : ISODate("2013-03-10T12:51:04Z"),
            "pingMs" : 1
        },
        {
            "_id" : 2,
            "name" : "localhost:27003",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 114,
            "optime" : Timestamp(1362919747000, 1),
            "optimeDate" : ISODate("2013-03-10T12:49:07Z"),
            "lastHeartbeat" : ISODate("2013-03-10T12:51:04Z"),
            "pingMs" : 1
        }
    ],
    "ok" : 1
}
rs0:PRIMARY> db.isMaster()
{
    "setName" : "rs0",
    "ismaster" : true,
    "secondary" : false,
    "hosts" : [
        "localhost:27001",
        "localhost:27003",
        "localhost:27002"
    ],
    "primary" : "localhost:27001",
    "me" : "localhost:27001",
    "maxBsonObjectSize" : 16777216,
    "localTime" : ISODate("2013-03-10T12:51:30.816Z"),
    "ok" : 1
}

可以在PRIMARY是通过rs.stepDown()来切换PRIMARY,执行此命令后会在剩余的常规节点选一个来充当PRIMARY。

rs0:PRIMARY> rs.stepDown()
Sun Mar 10 21:01:15 DBClientCursor::init call() failed
Sun Mar 10 21:01:15 query failed : admin.$cmd { replSetStepDown: 60.0 } to: 127.0.0.1:27001
Sun Mar 10 21:01:15 Error: error doing query: failed src/mongo/shell/collection.js:155
Sun Mar 10 21:01:15 trying reconnect to 127.0.0.1:27001
Sun Mar 10 21:01:15 reconnect 127.0.0.1:27001 ok
rs0:SECONDARY> db.isMaster()
{
    "setName" : "rs0",
    "ismaster" : false,
    "secondary" : true,
    "hosts" : [
        "localhost:27001",
        "localhost:27003",
        "localhost:27002"
    ],
    "primary" : "localhost:27003",
    "me" : "localhost:27001",
    "maxBsonObjectSize" : 16777216,
    "localTime" : ISODate("2013-03-10T13:01:30.647Z"),
    "ok" : 1
}

到现在最简单的复制数据集就建立完成了。