【发布时间】:2019-08-29 15:00:44
【问题描述】:
.Net 控制台应用程序在 4.6.1 框架中,使用 MongoDB.Driver 2.8.0。我在 SO 中引用了很多帖子,但我仍然收到超时错误。以下是我参考的一些帖子
A timeout occured after 30000ms selecting a server using CompositeServerSelector System.TimeoutException: A timeout occured after 30000ms selecting a server using CompositeServerSelector MongoDB C# 2.0 TimeoutException
下面是我用来从集合中访问文档的代码。
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
class Program
{
static void Main(string[] args)
{
string connectionString =
@"mongodb://mongoaccnt:ADASDXZWADAS2VgsqTYcTS4gtADmB1zQ==@mongocnt.documents.azure.com:10255/?ssl=true&replicaSet=globaldb";
MongoClientSettings settings = MongoClientSettings.FromUrl(
new MongoUrl(connectionString)
);
settings.SslSettings = new SslSettings() { EnabledSslProtocols = SslProtocols.Tls12 };
var mongoClient = new MongoClient(settings);
string dbName = "app-db";
string collectionName = "test";
var database = mongoClient.GetDatabase(dbName);
var todoTaskCollection = database.GetCollection<test>(collectionName);
var filter = Builders<test>.Filter.Eq("name", "second");
var results = todoTaskCollection.Find(filter).ToList();
Console.WriteLine(results);
Console.ReadLine();
}
}
public class test
{
public string name { get; set; }
}
以下是 Azure 云门户中显示的数据
db.test.find()
Operation consumed 2.31 RUs
{ "_id" : ObjectId("5ca4949fd59b290e00e35eda"), "id" : 1, "name" : "first" }
{
"_id" : ObjectId("5caafe968f678e0f504c6e64"),
"id" : 2,
"name" : "second"
}
下面是详细的错误
System.TimeoutException H结果=0x80131505 消息=使用 CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } } 选择服务器 30000 毫秒后发生超时。集群状态的客户端视图是 { ClusterId : "1", ConnectionMode : "ReplicaSet", Type : "ReplicaSet", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1,
【问题讨论】:
-
数据库命令默认为 30 秒超时。它可以做得更长。请参阅:mongodb.github.io/node-mongodb-native/3.1/reference/connecting/…
-
@jdweng 我添加了 settings.ConnectTimeout = TimeSpan.FromMinutes(5);到代码,问题仍然存在。
-
超时发生前需要多长时间?如果仍然是 30 秒,那么您没有找到服务器。如果 5 分钟后失败,则查询需要很长时间。我认为错误消息是说它找不到服务器。因此,要么服务器未运行(或未侦听端口 10255),要么没有到服务器的路由(以太网)。我将首先使用 cmd.exe 并尝试 ping 服务器 >Ping mongocnt.documents.azure.com
-
我通过更正连接字符串中的端口来修复此错误。它与我的本地 MongoDB 配置不匹配。