【问题标题】:A timeout occured after 30000ms selecting a server while accessing MongoDB in Azure using C#使用 C# 在 Azure 中访问 MongoDB 时选择服务器 30000 毫秒后发生超时
【发布时间】: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 配置不匹配。

标签: c# mongodb azure timeout


【解决方案1】:

很明显,您没有在连接代码中添加数据库名称。

这是连接字符串的模板

`var client = new MongoClient("mongodb://<dbuser>:<dbuserpassword>@<mongoaddress>/<dbname>?connect=replicaSet&ssl=true&replicaSet=<replicaset>&authSource=<authsource>");
var database = client.GetDatabase("test");`

您需要填写以下内容

  • dbuser:用户的用户名
  • dbuserpassword:数据库用户密码
  • dbname:数据库名称
  • mongoaddress:mongodb分片地址
  • replicaset:副本集的名称
  • authsource:身份验证源

【讨论】:

    【解决方案2】:

    您是否尝试在连接字符串后添加“?connect=replicaSet”:

    这张 JIRA 票有详细信息:https://jira.mongodb.org/browse/CSHARP-1160

    实际上,他们区分了连接到独立服务器和直接连接到副本集成员,后者相对不常见。不幸的是,MongoLab 的单节点设置实际上是一个单节点副本集,这导致我们不信任它。您可以通过将 ?connect=replicaSet 附加到连接字符串来解决此问题。它将强制驱动程序进入副本集模式,一切都会正常工作。

    您可以在以下位置找到更多详细信息:https://groups.google.com/forum/#!topic/mongodb-csharp/O460OHiFjZs

    希望对你有帮助。

    【讨论】:

    • 我刚用 CosmosDB,不知道为什么 Mongo 不起作用。
    猜你喜欢
    • 1970-01-01
    • 2015-09-27
    • 1970-01-01
    • 2020-02-22
    • 2020-04-02
    • 1970-01-01
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多