首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法用Azure Cosmos DB连接到Mongo DB

无法用Azure Cosmos DB连接到Mongo DB
EN

Stack Overflow用户
提问于 2018-10-26 06:18:00
回答 2查看 4.1K关注 0票数 6

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

代码语言:javascript
复制
_mongoDbConnectionString = configuration["MongoDBConnectionString"];
_databaseName = configuration["MongoDBName"];
_client = new MongoClient(_mongoDbConnectionString);
_database = _client.GetDatabase(_databaseName);
_collectionName = configuration["MongoDBCollectionName"];

然后试着写数据-

代码语言:javascript
复制
_database.GetCollection<dynamic>(_collectionName).InsertOne(data);

因为错误而失败-

在使用MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector,{ Selectors =CompositeServerSelector LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 }选择服务器后发生超时。群集状态的客户端视图是{ ClusterId:" 1“、ConnectionMode:"ReplicaSet”、Type:"ReplicaSet“、State:"Disconnected”、Servers:[{ ServerId:"{ ClusterId :1、EndPoint: EndPoint }“、EndPoint:"Unspecified/botframeworkcosmos.documents.azure.com:10255",State:"Disconnected”、Type:“Uniting”、HeartbeatException:"MongoDB.Driver.MongoConnectionException:打开到服务器的连接时发生的异常“。

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

我也尝试过像这样设置SSL策略来配置客户机-

代码语言:javascript
复制
_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"];

我还是会犯同样的错误。奇怪的是,同样的代码,昨天都起作用了。

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

知道会有什么问题吗?

EN

回答 2

Stack Overflow用户

发布于 2019-05-14 13:44:32

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

我通过在公司网络之外临时运行代码来测试这个问题,并且错误消失了(我验证了当我再次运行它时,公司的网络内部仍然失败)。

因此,添加网络防火墙规则以允许到TCP端口10255的出站连接应该修复它。

票数 0
EN

Stack Overflow用户

发布于 2019-05-22 11:52:27

检查须考虑的事项:

  1. 来自Azure COSMOS DB环境的连接字符串
  2. MongoDB驱动程序版本 私有字符串userName = "FILLME";私有字符串主机= "FILLME";私有字符串dbName =“任务”;私有字符串collectionName = "TasksList";私有字符串GetTasksCollection() { MongoClientSettings设置=新MongoClientSettings();settings.Server =新MongoServerAddress(主机,10255);settings.UseSsl = true;settings.SslSettings =新SslSettings();settings.SslSettings.EnabledSslProtocols = SslProtocols.Tls12;MongoIdentity identity = new MongoInternalIdentity(dbName,userName);PasswordEvidence settings.Credential =新PasswordEvidence(密码);settings.Credential=新MongoCredential("SCRAM-SHA-1",identity,return );MongoClient client =新MongoClient(设置);var数据库= client.GetDatabase(dbName);var todoTaskCollection = database.GetCollection(collectionName);返回todoTaskCollection;} public List GetAllTasks() { try { var collection = GetTasksCollection();返回collection.Find(新的BsonDocument()).ToList();} catch (MongoConnectionException ex) {返回新列表();}}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53002562

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档