【问题标题】:Unable to connect to Mongo DB with Azure Cosmos DB无法使用 Azure Cosmos DB 连接到 Mongo DB
【发布时间】:2019-03-30 20:24:34
【问题描述】:

我在 Azure 中使用 Mongo API 创建了一个 Cosmos DB 数据库。我已经创建了客户端并像这样配置-

_mongoDbConnectionString = configuration["MongoDBConnectionString"];
_databaseName = configuration["MongoDBName"];
_client = new MongoClient(_mongoDbConnectionString);
_database = _client.GetDatabase(_databaseName);
_collectionName = configuration["MongoDBCollectionName"];

然后尝试写入数据-

_database.GetCollection<dynamic>(_collectionName).InsertOne(data);

失败并出现错误-

在选择服务器 30000 毫秒后发生超时 CompositeServerSelector{ 选择器 = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }。集群状态的客户端视图是 { ClusterId : "1", ConnectionMode :“ReplicaSet”,类型:“ReplicaSet”,状态:“断开连接”,服务器: [{ ServerId: "{ ClusterId: 1, EndPoint: "未指定/botframeworkcosmos.documents.azure.com:10255" }", 端点:“未指定/botframeworkcosmos.documents.azure.com:10255”, 状态:“断开连接”,类型:“未知”,HeartbeatException: “MongoDB.Driver.MongoConnectionException:发生异常时 打开与服务器的连接。 ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException:一个 连接尝试失败,因为连接方没有正确 一段时间后响应,或建立连接失败 因为连接的主机没有响应

我尝试了这个解决方案-A timeout occured after 30000ms selecting a server using CompositeServerSelector,但没有奏效。

我也尝试设置这样的 SSL 策略来配置客户端-

_mongoDbConnectionString = configuration["MongoDBConnectionString"];
_databaseName = configuration["MongoDBName"];
MongoClientSettings settings = MongoClientSettings.FromUrl(
  new MongoUrl(_mongoDbConnectionString)
);
settings.SslSettings =
  new SslSettings() { EnabledSslProtocols = SslProtocols.Tls12 };
_client = new MongoClient(settings);
_database = _client.GetDatabase(_databaseName);
_collectionName = configuration["MongoDBCollectionName"];

我仍然遇到同样的错误。奇怪的是,同样的代码,昨天还在工作。

更新 我删除了数据库并创建了一个新数据库。还是同样的问题。

知道可能是什么问题吗?

【问题讨论】:

  • 您确定 appsettings.json 中的连接字符串/url 正确吗?端点入口与Unspecified/botframeworkcosmos.documents.azure.com:10255前面的Unspecified/有点奇怪
  • @Tseng 我的连接字符串是这样的-mongodb://chatbotcosmos:&lt;secret_key&gt;@chatbotcosmos.documents.azure.com:10255/?ssl=true&amp;replicaSet=globaldb。这是我重新创建完全从 Azure 复制的数据库后的新连接字符串。
  • 确定没有从其他来源获取连接字符串?当您在本地调试时像 appsettings.development.json 一样?既然上面说的是botframeworkcosmos.documents.azure.com和你的chatbotcosmos.documents.azure.com
  • @Tseng 是的,在我删除并重新创建数据库后,我现在有了一个新的连接字符串。另外,配置是正确的,我已经在调试中验证了。
  • 您的密码中有一些特殊字符吗?例如/:@?你不能在那里有特殊字符,因为它是一个 url(或者可能必须转义它)

标签: c# mongodb asp.net-core azure-cosmosdb


【解决方案1】:

我收到了同样的错误信息(A timeout occured after 30000msUnspecified 也被提及)。这是因为我公司的防火墙阻止了 Cosmos Mongo 端口上的出站连接,例如TCP 10255。

我通过在我们公司的网络之外临时运行代码进行了测试,并且错误消失了(我验证当我在公司网络之外再次重新运行它时仍然失败)。

因此,添加网络防火墙规则以允许到 TCP 端口 10255 的出站连接应该可以解决此问题。

【讨论】:

  • 谢谢。我现在无法检查,但可能会稍后。
【解决方案2】:

要考虑的检查:

  1. 来自 Azure COSMOS DB 环境的连接字符串
  2. MongoDB 驱动版本

     private string userName = "FILLME";
            private string host = "FILLME";            
            private string dbName = "Tasks";
            private string collectionName = "TasksList";
    
          private IMongoCollection<MyTask> GetTasksCollection()
          {
            MongoClientSettings settings = new MongoClientSettings();
            settings.Server = new MongoServerAddress(host, 10255);
            settings.UseSsl = true;
            settings.SslSettings = new SslSettings();
            settings.SslSettings.EnabledSslProtocols = SslProtocols.Tls12;
    
            MongoIdentity identity = new MongoInternalIdentity(dbName, userName);
            PasswordEvidence evidence = new PasswordEvidence(password);
            settings.Credential = new MongoCredential("SCRAM-SHA-1", identity, evidence);
            MongoClient client = new MongoClient(settings);
            var database = client.GetDatabase(dbName);
            var todoTaskCollection = database.GetCollection<MyTask>(collectionName);
            return todoTaskCollection;
        }
    
         public List<MyTask> GetAllTasks()
         {
           try
           {
            var collection = GetTasksCollection();
            return collection.Find(new BsonDocument()).ToList();
            }
            catch (MongoConnectionException ex)
            {
              return new List<MyTask>();
            }
          }
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多