mongodb - 带有rs.add()的MongoDB脚本未按预期执行

标签 mongodb docker replicaset

由于某种原因,此非常基本的脚本无法按预期运行:

rs.initiate();
rs.add("ip-10-0-1-11");
rs.add("ip-10-0-1-12");

当我称呼它(在mongod重新启动之后)时,如下所示:
docker exec mongodb mongo localhost:27017/test /script/path.js

我只看到第二个主机的添加。
rs0:PRIMARY> rs.status()
{
        "set" : "rs0",
        "date" : ISODate("2015-12-14T20:27:10.124Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "heartbeatIntervalMillis" : NumberLong(2000),
        "members" : [
                {
                        "_id" : 0,
                        "name" : "ip-10-0-1-10:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 45,
                        "optime" : {
                                "ts" : Timestamp(1450124790, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2015-12-14T20:26:30Z"),
                        "infoMessage" : "could not find member to sync from",
                        "electionTime" : Timestamp(1450124789, 2),
                        "electionDate" : ISODate("2015-12-14T20:26:29Z"),
                        "configVersion" : 2,
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "ip-10-0-1-12:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 40,
                        "optime" : {
                                "ts" : Timestamp(1450124790, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2015-12-14T20:26:30Z"),
                        "lastHeartbeat" : ISODate("2015-12-14T20:27:09.828Z"),
                        "lastHeartbeatRecv" : ISODate("2015-12-14T20:27:06.827Z"),
                        "pingMs" : NumberLong(0),
                        "configVersion" : 2
                }
        ],
        "ok" : 1
}

但是,如果我跳回cli并手动添加丢失的主机-则没有问题。另外-如果我再次运行该脚本,则第二个脚本将正常添加。

查看日志不会给我任何有用的信息。我只是看到添加了ip-10-0-1-12主机,而没有引用ip-10-0-1-11。我100%肯定在我运行这些命令时其他mongod实例可以访问。

我已经尝试了几种方法-即使这样:
docker exec mongodb mongo localhost:27017/test --eval 'rs.initiate();rs.add("ip-10-0-1-11");rs.add("ip-10-0-1-12");'

以及跳入cli并运行load("path/to/script.js")
任何帮助将非常感激。

编辑:

这是脚本运行时的大量日志。上面的所有内容都是初始化信息,下面的所有内容都是已接受的连接:
2015-12-14T20:26:29.759+0000 I COMMAND  [conn1] initiate : no configuration specified. Using a default configuration for the set
2015-12-14T20:26:29.759+0000 I COMMAND  [conn1] created this configuration for initiation : { _id: "rs0", version: 1, members: [ { _id: 0, host: "ip-10-0-1-10:27017" } ] }
2015-12-14T20:26:29.759+0000 I REPL     [conn1] replSetInitiate admin command received from client
2015-12-14T20:26:29.759+0000 I REPL     [conn1] replSetInitiate config object with 1 members parses ok
2015-12-14T20:26:29.759+0000 I REPL     [conn1] ******
2015-12-14T20:26:29.759+0000 I REPL     [conn1] creating replication oplog of size: 990MB...
2015-12-14T20:26:29.765+0000 I STORAGE  [conn1] Starting WiredTigerRecordStoreThread local.oplog.rs
2015-12-14T20:26:29.765+0000 I STORAGE  [conn1] The size storer reports that the oplog contains 0 records totaling to 0 bytes
2015-12-14T20:26:29.765+0000 I STORAGE  [conn1] Scanning the oplog to determine where to place markers for truncation
2015-12-14T20:26:29.784+0000 I REPL     [conn1] ******
2015-12-14T20:26:29.793+0000 I REPL     [ReplicationExecutor] New replica set config in use: { _id: "rs0", version: 1, protocolVersion: 1, members: [ { _id: 0, host: "ip-10-0-1-10:27017", arbiterOnly: false, buildIndexes: true, hidden: false, priority: 1.0, tags: {}, slaveDelay: 0, votes: 1 } ], settings: { chainingAllowed: true, heartbeatIntervalMillis: 2000, heartbeatTimeoutSecs: 10, electionTimeoutMillis: 10000, getLastErrorModes: {}, getLastErrorDefaults: { w: 1, wtimeout: 0 } } }
2015-12-14T20:26:29.793+0000 I REPL     [ReplicationExecutor] This node is ip-10-0-1-10:27017 in the config
2015-12-14T20:26:29.793+0000 I REPL     [ReplicationExecutor] transition to STARTUP2
2015-12-14T20:26:29.793+0000 I REPL     [conn1] Starting replication applier threads
2015-12-14T20:26:29.794+0000 I REPL     [ReplicationExecutor] transition to RECOVERING
2015-12-14T20:26:29.795+0000 I REPL     [conn1] replSetReconfig admin command received from client
2015-12-14T20:26:29.795+0000 I REPL     [ReplicationExecutor] transition to SECONDARY
2015-12-14T20:26:29.795+0000 I REPL     [ReplicationExecutor] conducting a dry run election to see if we could be elected
2015-12-14T20:26:29.795+0000 I REPL     [ReplicationExecutor] dry election run succeeded, running for election
2015-12-14T20:26:29.803+0000 I REPL     [ReplicationExecutor] election succeeded, assuming primary role in term 1
2015-12-14T20:26:29.803+0000 I REPL     [ReplicationExecutor] transition to PRIMARY
2015-12-14T20:26:29.804+0000 I REPL     [conn1] replSetReconfig admin command received from client
2015-12-14T20:26:29.806+0000 I REPL     [conn1] replSetReconfig config object with 2 members parses ok
2015-12-14T20:26:29.807+0000 I ASIO     [NetworkInterfaceASIO-Replication-0] Successfully connected to ip-10-0-1-12:27017
2015-12-14T20:26:29.808+0000 I REPL     [ReplicationExecutor] New replica set config in use: { _id: "rs0", version: 2, protocolVersion: 1, members: [ { _id: 0, host: "ip-10-0-1-10:27017", arbiterOnly: false, buildIndexes: true, hidden: false, priority: 1.0, tags: {}, slaveDelay: 0, votes: 1 }, { _id: 1, host: "ip-10-0-1-12:27017", arbiterOnly: false, buildIndexes: true, hidden: false, priority: 1.0, tags: {}, slaveDelay: 0, votes: 1 } ], settings: { chainingAllowed: true, heartbeatIntervalMillis: 2000, heartbeatTimeoutSecs: 10, electionTimeoutMillis: 10000, getLastErrorModes: {}, getLastErrorDefaults: { w: 1, wtimeout: 0 } } }

最佳答案

rs.initiate()需要几秒钟的时间,副本才能更改状态。这可能会导致第一个rs.add()无法正常工作。奇怪的是我们没有收到错误消息。

为避免发生此问题,您可以尝试在单个命令中启动和配置副本集中的所有成员:

rs.initiate(
  {_id:'rs0',
   members:[ { _id:1, host:'ip-10-0-1-10:27017'},
             { _id:2, host:'ip-10-0-1-11:27017'},
             { _id:3, host:'ip-10-0-1-12:27017'}]
   });

关于mongodb - 带有rs.add()的MongoDB脚本未按预期执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34276246/

相关文章:

java - MongoDB如何使用Java驱动程序计算数组元素

python - 无效的URI方案: mongodb+srv , Mongodb错误

json - 使用 NoSQL 数据库对 JSON 数据进行高效且可扩展的存储

node.js - 有没有办法使用聚合从 Mongoose 的 ObjectId 获取日期?

jenkins - 使用 jenkins 更新 kubernetes 部署

docker - 使用webpack-dev-server从webpack 2代理请求到docker容器中的后端api

docker - 我们如何在docker history命令的输出中看到完整的命令?

docker desktop wsl 2 后端意外停止

mongodb - 使用 docker 和 fig 设置 mongodb 副本集的正确方法是什么?

database - 通过删除的成员连接到副本集成员