查看 SECONDARY 何时成为 PRIMARY

Find out when SECONDARY became PRIMARY

假设我有一个三服务器设置。两台服务器存储数据,一台服务器是仲裁者。

上周,我的 'PRIMARY' 服务器宕机了,正如预期的那样 'SECONDARY' 升级了,事情继续按预期进行。

但是,我现在正在调试我的应用程序中的另一个错误,我认为可能与复制设置中的此更改有关。

我有什么办法可以查明(从日志或诸如此类的东西)什么时候选举发生了?

您可以在(新 'PRIMARY' 的)日志中找到以下行:

2018-08-02T03:56:49.817+0000 I REPL     [ReplicationExecutor] Standing for election
2018-08-02T03:56:49.818+0000 I REPL     [ReplicationExecutor] not electing self, ip-xxx-xxx-xxx-xxx.eu-west-1.compute.internal:27017 has same OpTime as us: { : Timestamp 1533182831000|1 }
2018-08-02T03:56:49.818+0000 I REPL     [ReplicationExecutor] possible election tie; sleeping 445ms until 2018-08-02T03:56:50.263+0000
2018-08-02T03:56:50.263+0000 I REPL     [ReplicationExecutor] Standing for election
2018-08-02T03:56:50.265+0000 I REPL     [ReplicationExecutor] not electing self, ip-xxx-xxx-xxx-xxx.eu-west-1.compute.internal:27017 has same OpTime as us: { : Timestamp 1533182831000|1 }
2018-08-02T03:56:50.265+0000 I REPL     [ReplicationExecutor] running for election; slept last election, so running regardless of possible tie
2018-08-02T03:56:50.265+0000 I REPL     [ReplicationExecutor] received vote: 1 votes from ip-xxx-xxx-xxx-xxx.eu-west-1.compute.internal:27017
2018-08-02T03:56:50.265+0000 I REPL     [ReplicationExecutor] election succeeded, assuming primary role
2018-08-02T03:56:50.265+0000 I REPL     [ReplicationExecutor] transition to PRIMARY

您可以看到选举发生在 3:56am UTC。

我建议您使用 less 工具在您的日志文件中搜索:

less /var/log/mongodb/mongod.log

然后,使用 G 在文件末尾导航,然后使用 ? 向后搜索,然后搜索 'Standing for election'.