Home > Software engineering >  Mongodb .net drive connect to mongodb secondary not working
Mongodb .net drive connect to mongodb secondary not working

Time:10-31

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.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)

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)

CodePudding user response:

this format works but I get the results from primary instead of secondary - I would say it's unlikely and you see data from a secondary. To make direct connection to any secondary, you should use readPreference=secondary. If you need direct connecting to a particular server by endpoint, you should specify directConnection=true

  • Related