Mongodb .net drive connect to mongodb secondary not working

While trying to connect to mongodb secondary node non replicaset mode to rotate logs and trying to run this command from .net to get the configured log path

Connectionstring: "mongodb://superuser:password@localhost:27017/?authSource=admin"

    db.adminCommand(
   {
     getCmdLineOpts: 1
   }
)

.net mongdb driver: 2.14.1

Getting connection error.

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.ReadCommandOperation 1.ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken) at MongoDB.Driver.OperationExecutor.ExecuteReadOperationAsync[TResult](IReadBinding binding, IReadOperation 1 operation, CancellationToken cancellationToken) at MongoDB.Driver.MongoDatabaseImpl.ExecuteReadOperationAsync[T](IClientSessionHandle session, IReadOperation 1 operation, ReadPreference readPreference, CancellationToken cancellationToken) at MongoDB.Driver.MongoDatabaseImpl.UsingImplicitSessionAsync[TResult](Func 2 funcAsync, CancellationToken cancellationToken)

Using the same connectionstring from mongo client works.

And need to mention replicaset in connectionstring to be able to connect from .net driver. “mongodb://superuser:password@localhost:27017/?replicaSet=rsWordWatch&authSource=admin&readPreference=secondary” ( this format works but I get the results from primary instead of secondary)

use directConnection=true in connectionstring

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.