You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET驱动连接MongoDB从节点失败及读取主节点异常问题

MongoDB .NET驱动连接从节点问题

问题场景

尝试以非副本集模式连接MongoDB从节点,执行日志轮转操作,并通过.NET驱动调用命令获取配置的日志路径:

  • 使用的.NET MongoDB驱动版本:2.14.1
  • 连接字符串:
    "mongodb://superuser:password@localhost:27017/?authSource=admin"
    
  • 执行命令:
    db.adminCommand(
      {
        getCmdLineOpts: 1
      }
    )
    

遇到的错误

出现连接超时错误,错误详情如下:

Error occurred while running StartMongoLogCleanup A timeout occurred after 30000ms selecting a server using CompositeServerSelector{ Selectors = ReadPreferenceServerSelector{ ReadPreference = { Mode : Primary } }, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 }, OperationsCountServerSelector }. Client view of cluster state is { ClusterId : "1", Type : "ReplicaSet", State : "Connected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/localhost:27017" }", EndPoint: "Unspecified/localhost:27017", ReasonChanged: "Heartbeat", State: "Connected", ServerVersion: 4.4.0, TopologyVersion: { "processId" : ObjectId("635faf2c862466cca6786229"), "counter" : NumberLong(3) }, Type: "ReplicaSetSecondary", WireVersionRange: "[0, 9]", LastHeartbeatTimestamp: "2022-10-31T14:04:20.4815165Z", LastUpdateTimestamp: "2022-10-31T14:04:20.4815167Z" }] }. at MongoDB.Driver.Core.Clusters.Cluster.ThrowTimeoutException(IServerSelector selector, ClusterDescription description)
at MongoDB.Driver.Core.Clusters.Cluster.WaitForDescriptionChangedHelper.HandleCompletedTask(Task completedTask)
at MongoDB.Driver.Core.Clusters.Cluster.WaitForDescriptionChangedAsync(IServerSelector selector, ClusterDescription description, Task descriptionChangedTask, TimeSpan timeout, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Clusters.Cluster.SelectServerAsync(IServerSelector selector, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Clusters.IClusterExtensions.SelectServerAndPinIfNeededAsync(ICluster cluster, ICoreSessionHandle session, IServerSelector selector, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Bindings.ReadPreferenceBinding.GetReadChannelSourceAsync(CancellationToken cancellationToken)
at MongoDB.Driver.Core.Operations.RetryableReadContext.InitializeAsync(CancellationToken cancellationToken)
at MongoDB.Driver.Core.Operations.RetryableReadContext.CreateAsync(IReadBinding binding, Boolean retryRequested, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Operations.ReadCommandOperation1.ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken) at MongoDB.Driver.OperationExecutor.ExecuteReadOperationAsync[TResult](IReadBinding binding, IReadOperation1 operation, CancellationToken cancellationToken)
at MongoDB.Driver.MongoDatabaseImpl.ExecuteReadOperationAsync[T](IClientSessionHandle session, IReadOperation1 operation, ReadPreference readPreference, CancellationToken cancellationToken) at MongoDB.Driver.MongoDatabaseImpl.UsingImplicitSessionAsync[TResult](Func2 funcAsync, CancellationToken cancellationToken)

额外情况

  • 使用相同连接字符串在Mongo客户端可正常连接。
  • 若在连接字符串中添加副本集参数(如下),可成功连接,但返回的却是主节点的数据:
    "mongodb://superuser:password@localhost:27017/?replicaSet=rsWordWatch&authSource=admin&readPreference=secondary"
    

内容的提问来源于stack exchange,提问作者Sameer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 02:40:39