:MongoDB.Driver.MongoConnectionException:打开与服务器的连接时发生异常 [英] : MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server
问题描述
因此,我在Azure VM
上使用MongoDB
,并且我在Azure网站上托管了一个网站作为服务.
我的问题是:有时我会收到这样的错误:
So, I am using MongoDB
on a Azure VM
and I have a web site hosted on Azure web Sites as a service.
My problem is: Sometimes I get an error like this:
异常:MongoDB.Driver.MongoConnectionException:打开与服务器的连接时发生异常.---> System.Net.Sockets.SocketException:连接尝试失败,因为经过一段时间后连接方未正确响应时间或已建立的连接失败,因为已连接的主机未能响应"
"Exception: MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond"
出现此错误后,只需再次访问端点即可立即获得成功.
After I got this error, just I access the endpoint again and I have success immediately.
详细信息:
ConnectionString:mongodb://xxx.aaa.net:1000,xxx.aaa.net:1001,xxx.aaa.net:1002/?readPreference = nearest
ConnectionString: mongodb://xxx.aaa.net:1000, xxx.aaa.net:1001, xxx.aaa.net:1002/?readPreference=nearest
在使用Mongo打开连接之前,请设置MaxConnectioIdleTimeout属性,例如"MongoDefaults.MaxConnectionIdleTime = TimeSpan.FromSeconds(30);". ,请保持活动连接,因为默认情况下4分钟后,天蓝色LoadBalancer的空闲超时将变为无效.
Before open the conection with Mongo I set the MaxConnectioIdleTimeout property like this "MongoDefaults.MaxConnectionIdleTime = TimeSpan.FromSeconds(30);" , To keep alive connection, because the idle timeout for azure LoadBalancer become inactivy after 4 minutes by default.
所以,我不知道发生了什么事.
So, I don't know what's going on.
有人可以帮助我吗?
推荐答案
是的,需要4分钟.如果Azure保持空闲状态最多4分钟,它将关闭一个连接会话.但是mongodb驱动程序不知道它,它仍然会从连接池中获得此连接. 您可以将 MaxConnectionIdleTime (在mongodb驱动程序设置中)设置为少于4分钟,或者通过Powershell设置更大的Azure虚拟机的最大会话空闲时间(最多30分钟).
Yes, 4 minutes. Azure closes a connection session if it keeping on idling up to 4 minutes.But the mongodb driver don't know it,it will still get this connection form connection pool. You can either set the MaxConnectionIdleTime(in mongodb driver settings) less than 4 minutes, or set the max session idle time of azure VM more larger by powershell (30 minutes max).
顺便说一句,如果您的网站(也位于azure上)使用私有IP(PIP)访问mongodb服务器,则不会出现此问题.
BTW, if your web site which also hosted on azure access the mongodb server with the private IP(PIP), there won't be this problem.
这篇关于:MongoDB.Driver.MongoConnectionException:打开与服务器的连接时发生异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!