无法使用Azure Cosmos DB连接Mongo DB

6

我在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);

遇到了以下错误-

使用CompositeServerSelector选择服务器时,在30000ms之后发生超时,其中CompositeServerSelector选择器包括MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector和LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 }。 客户端查看集群状态为{ ClusterId: "1",ConnectionMode: "ReplicaSet",Type: "ReplicaSet",State: "Disconnected",Servers:[{ ServerId:“ {ClusterId:1,EndPoint:“Unspecified / botframeworkcosmos.documents.azure.com:10255”}”,EndPoint:“ Unspecified / botframeworkcosmos.documents.azure.com:10255”,State:“ Disconnected”,Type:“ Unknown”,HeartbeatException:“ MongoDB.Driver.MongoConnectionException:在连接到服务器时发生异常 ---> System.Net.Internals.SocketExceptionFactory + ExtendedSocketException:连接尝试失败,因为已连接方在一段时间后未正确响应,或已建立的连接失败,因为连接的主机未能正确响应

我尝试了这个解决方案-使用CompositeServerSelector选择服务器时在30000ms之后出现超时,但没有成功。

我还尝试像这样设置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/Unspecified/botframeworkcosmos.documents.azure.com:10255 前面看起来有点奇怪。 - Tseng
@Tseng 我的连接字符串是这样的 - mongodb://chatbotcosmos:<secret_key>@chatbotcosmos.documents.azure.com:10255/?ssl=true&replicaSet=globaldb。这是我重新创建数据库后从Azure精确复制的新连接字符串。 - Souvik Ghosh
你确定它没有从其他来源获取连接字符串吗?例如当你在本地进行调试时,是否使用了appsettings.development.json文件?因为上面的那个文件是说botframeworkcosmos.documents.azure.com,而你的是chatbotcosmos.documents.azure.com - Tseng
@Tseng 是的,我现在有一个新的连接字符串,因为我已经删除并重新创建了数据库。此外,配置是正确的,我已经在调试中验证过了。 - Souvik Ghosh
你的密码里有一些特殊字符吗?比如说 /: 或者 @?因为它是一个URL(或者需要进行转义),所以你不能在密码中使用特殊字符。 - Tseng
@Tseng 不是的。密码是由Azure自动生成的。就像这样 - 12pVpf5Y5spByBjHJkok2D0PfvaQYaTzmDGhZDXO96S0eH6RnQRuPkss4MksNYQjU6mx6d4YSbG3iajuFSkCxQ==。这是我的旧密码,昨天还可以使用。 - Souvik Ghosh
2个回答

1

我曾经遇到过同样的错误信息(发生了30秒的超时,并且还提到了未指定)。原因是我的公司防火墙阻止了对于Cosmos Mongo端口的出站连接,例如TCP 10255。

我通过暂时在公司网络之外运行代码来进行测试,结果错误消失了(再次在公司内部重新运行代码后,它仍会失败)。

因此,添加允许对TCP端口10255进行出站连接的网络防火墙规则即可解决问题。


谢谢。我现在不能检查,但也许以后可以。 - Souvik Ghosh

-1

需要考虑的事项:

  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>();
            }
          }
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接