MongoDB 3.0.1复制设置

时间:2015-05-18 07:15:15

标签: mongodb

我正在使用MongoDB 3.0.1版本。我正在尝试为mongodb replicaiton设置 我们的机器。我在隔膜机上使用了三个mongodb进行复制。

machine1 - master
machine2 - slave
machine3 - slave

我引用此网址http://docs.mongodb.org/manual/tutorial/deploy-replica-set/

我已经为mongodb.conf文件中的每台机器配置了

replSet = rs1
fork = true

我使用此命令在主机中添加了两个成员

rs.add(192.168.1.2)
rs.add(192.168.1.3)

但我在主机中插入文件但不复制其他两台从机。我检查奴隶机器抛出以下错误

> show dbs
2015-05-18T12:43:22.020+0530 E QUERY    Error: listDatabases failed:{ "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" }
    at Error (<anonymous>)
    at Mongo.getDBs (src/mongo/shell/mongo.js:47:15)
    at shellHelper.show (src/mongo/shell/utils.js:630:33)
    at shellHelper (src/mongo/shell/utils.js:524:36)
    at (shellhelp2):1:1 at src/mongo/shell/mongo.js:47
> 
> rs.conf()
2015-05-18T12:43:38.692+0530 E QUERY    Error: Could not retrieve replica set config: {
    "info" : "run rs.initiate(...) if not yet done for the set",
    "ok" : 0,
    "errmsg" : "no replset config has been received",
    "code" : 94
}
    at Function.rs.conf (src/mongo/shell/utils.js:1011:11)
    at (shell):1:4 at src/mongo/shell/utils.js:1011
> 

请帮我解决问题。谢谢&amp;进展。

编辑:

rs1:PRIMARY> rs.conf()
{
    "_id" : "rs1",
    "version" : 4,
    "members" : [
        {
            "_id" : 0,
            "host" : "analyzer-xubuntu:27017",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 1,
            "tags" : {

            },
            "slaveDelay" : 0,
            "votes" : 1
        },
        {
            "_id" : 1,
            "host" : "192.168.1.31:27017",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 0.75,
            "tags" : {

            },
            "slaveDelay" : 0,
            "votes" : 1
        },
        {
            "_id" : 2,
            "host" : "192.168.1.33:27017",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 0.5,
            "tags" : {

            },
            "slaveDelay" : 0,
            "votes" : 1
        }
    ],
    "settings" : {
        "chainingAllowed" : true,
        "heartbeatTimeoutSecs" : 10,
        "getLastErrorModes" : {

        },
        "getLastErrorDefaults" : {
            "w" : 1,
            "wtimeout" : 0
        }
    }
}

rs1:PRIMARY> rs.status()
{
    "set" : "rs1",
    "date" : ISODate("2015-05-18T09:07:31.767Z"),
    "myState" : 1,
    "members" : [
        {
            "_id" : 0,
            "name" : "analyzer-xubuntu:27017",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 9236,
            "optime" : Timestamp(1431939509, 2),
            "optimeDate" : ISODate("2015-05-18T08:58:29Z"),
            "electionTime" : Timestamp(1431931054, 2),
            "electionDate" : ISODate("2015-05-18T06:37:34Z"),
            "configVersion" : 4,
            "self" : true
        },
        {
            "_id" : 1,
            "name" : "192.168.1.31:27017",
            "health" : 1,
            "state" : 0,
            "stateStr" : "STARTUP",
            "uptime" : 8953,
            "optime" : Timestamp(0, 0),
            "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
            "lastHeartbeat" : ISODate("2015-05-18T09:07:29.831Z"),
            "lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
            "pingMs" : 1,
            "configVersion" : -2
        },
        {
            "_id" : 2,
            "name" : "192.168.1.33:27017",
            "health" : 1,
            "state" : 0,
            "stateStr" : "STARTUP",
            "uptime" : 8946,
            "optime" : Timestamp(0, 0),
            "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
            "lastHeartbeat" : ISODate("2015-05-18T09:07:30.533Z"),
            "lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
            "pingMs" : 1,
            "configVersion" : -2
        }
    ],
    "ok" : 1
}

1 个答案:

答案 0 :(得分:13)

让我们一步一步走。

你的MongoDB配置没问题,剩下的工作可以在shell中完成。我将使用机器名而不是IP。

首先连接到machine1,然后在那里运行以下内容:

> conf = {
            _id: "rs1",
            members:
                      [
                         {_id : 0, host : "machine1:27017"},
                         {_id : 1, host : "machine2:27017"},
                         {_id : 2, host : "machine3:27017"}
                      ]
         }
> rs.initiate(conf)

然后在辅助设备上运行rs.slaveOk()secondaries将开始复制,您将能够查询并查看从primary插入的数据。