尝试连接到mongodb单节点副本集的超时异常

时间:2018-09-22 21:33:44

标签: mongodb mongodb-.net-driver mongodb-csharp-2.0 mongodb-replica-set mongodb-replica

任何关于出问题的想法将不胜感激。谢谢!

服务器启动时带有以下标志

mongodb-win32-x86_64-2008plus-ssl-4.0.2\bin\mongod --sslMode disabled --dbpath "C:\Users\foobar\AppData\Local\Temp\sj5ufivo.hgy" --port 27017  --bind_ip 127.0.0.1 --replSet singleNodeReplSet

应用代码

MongoClient client = new MongoClient(@"mongodb://127.0.0.1:{0}/?connect=replicaSet;replicaSet=singleNodeReplSet".Formatted(port));
            var admin = client.GetDatabase("admin");
var replConfig = new BsonDocument(new List<BsonElement>()
            {
                new BsonElement("_id", "singleNodeReplSet"),
                new BsonElement("members", new BsonArray { new BsonDocument { { "_id", 0 }, { "host", "127.0.0.1:27017" } } })
            });
  var commandDocument = new BsonDocument("replSetInitiate", replConfig);
            var replSet = admin.RunCommand<BsonDocument>(commandDocument);

例外

System.TimeoutException
  HResult=0x80131505
  Message=A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "ReplicaSet", Type : "ReplicaSet", State : "Connected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "127.0.0.1:27017" }", EndPoint: "127.0.0.1:27017", State: "Connected", Type: "ReplicaSetGhost", WireVersionRange: "[0, 7]" }] }.
  Source=MongoDB.Driver.Core
  StackTrace:
   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.WaitForDescriptionChanged(IServerSelector selector, ClusterDescription description, Task descriptionChangedTask, TimeSpan timeout, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Clusters.Cluster.SelectServer(IServerSelector selector, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoClient.AreSessionsSupportedAfterServerSelection(CancellationToken cancellationToken)
   at MongoDB.Driver.MongoClient.AreSessionsSupported(CancellationToken cancellationToken)
   at MongoDB.Driver.OperationExecutor.StartImplicitSession(CancellationToken cancellationToken)
   at MongoDB.Driver.MongoDatabaseImpl.UsingImplicitSession[TResult](Func`2 func, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoDatabaseImpl.RunCommand[TResult](Command`1 command, ReadPreference readPreference, CancellationToken cancellationToken)

1 个答案:

答案 0 :(得分:0)

将连接类型更改为直接连接,将驱动程序更新为2.7.0,然后开始工作!

MongoClient client = new MongoClient(@"mongodb://127.0.0.1:{0}/?connect=direct;replicaSet=singleNodeReplSet".Formatted(port));