/*十有三博客*/
  • 首页
  • 关于本站
  • 网站地图
  • RSS订阅

记录MongoDb连接错误System.TimeoutException: A timeout occured after 30000ms selecting a server

2016-12-19 十有三 0 浏览:3万+ 数据库 MongoDB

网站使用MongoDb数据库,网站发布后运行出错,提示数据库连接超时异常(System.TimeoutException)。其实一般只要仔细阅读错误信息,都能找到问题的源头,这里记录下我曾遇到的情况。

先贴下我遇到的错误截图:

MongoDb连接超时异常System.TimeoutException错误截图

详细的错误信息如下:

Error Details
System.TimeoutException: A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = WritableServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "Automatic", Type : "Unknown", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "192.168.1.113:27017" }", EndPoint: "192.168.1.113:27017", State: "Disconnected", Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> System.Net.Sockets.SocketException: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。 192.168.1.113:27017
   at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MongoDB.Driver.Core.Connections.TcpStreamFactory.d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MongoDB.Driver.Core.Connections.TcpStreamFactory.d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at MongoDB.Driver.Core.Connections.BinaryConnection.d__47.MoveNext()
   --- End of inner exception stack trace ---
   at MongoDB.Driver.Core.Connections.BinaryConnection.d__47.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MongoDB.Driver.Core.Servers.ClusterableServer.d__42.MoveNext()" }] }.
   at MongoDB.Driver.Core.Clusters.Cluster.WaitForDescriptionChangedHelper.HandleCompletedTask(Task completedTask)
   at MongoDB.Driver.Core.Clusters.Cluster.WaitForDescriptionChanged(IServerSelector selector, ClusterDescription description, Task descriptionChangedTask, TimeSpan timeout, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Clusters.Cluster.SelectServer(IServerSelector selector, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Bindings.WritableServerBinding.GetWriteChannelSource(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.BulkMixedWriteOperation.Execute(IWriteBinding binding, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.ExecuteWriteOperation[TResult](IWriteOperation`1 operation, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.BulkWrite(IEnumerable`1 requests, BulkWriteOptions options, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionBase`1.InsertOne(TDocument document, InsertOneOptions options, CancellationToken cancellationToken)
   at CallSite.Target(Closure , CallSite , IMongoCollection`1 , Object )
   at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid2[T0,T1](CallSite site, T0 arg0, T1 arg1)

要注意MongoDb错误信息的格式问题,有的web框架有自己的错误页面模板,会导致浏览器出现横向的滚动条,如果不注意会忽略掉重要的错误信息!仔细查阅错误详情,一般都可以看出问题的关键点!我遇到的情况就是项目在部署时,没有调整正确的数据库IP地址,在内部引发了System.Net.Sockets.SocketException异常,才导致提示连接超时。

我平时在使用mongodb过程中,也常常遇到遇到类似的错误,大部分都是数据库无法正确连接上导致的,归纳下大概有这几种情况:

  1. 项目连接mongodb数据库的IP地址/连接语句设置错误,需要检查项目部署时是否有修改正确的数据库地址。
  2. mongodb所在服务器防火墙相关端口未开启,只要开启对应的端口即可。
  3. 网络影响,例如项目是部署在香港,数据库部署在北京,网络传输跨度大,一旦出现网络波动很容易造成链接超时以及System.TimeoutException。

另外还有可能是数据库版本所造成的BUG,也有框架驱动的BUG,这方面我只在网上有看到过。有很多外国猿类也遇到相同问题,情况也各不相同,如果有其他情况只能在网络上找找了。

由于MongoDb目前的中文资料比较少,在网上找的大部分资料都是英文的,所以对英文不太熟悉的就比较蛋疼了。

这两个网页也有提到相同的问题,可以参考参考:

  1. https://jira.mongodb.org/browse/CSHARP-1435
  2. https://stackoverflow.com/questions/29832622/mongodb-c-sharp-2-0-timeoutexception

作者:十有三

出处:https://shiyousan.com/post/636177837177647637

版权声明:本文采用知识共享许可协议:署名-相同方式共享 4.0 国际(CC BY-SA 4.0)。欢迎转载本文,转载请声明出处或保留此段声明。


  • 上一篇: 记录下自己对网站全局设置存储方式的心得
  • 下一篇: ChinaPay.dll 64位系统导致应用程序池崩溃,异常代码: 0xc0000005

相关文章
  • MongoDB在Windows Server 2012 R2中的安全部署
  • SQL使用ROW_NUMBER函数获取没有ID的当前数据的上下文数据(上一条和下一条)
  • 解决SQL Service创建链接服务器出现(null) 是无效的产品名称的问题
  • MongoDB学习笔记:batchInsert is not a function
  • System.Data.SqlClient.SqlError: 尚未备份数据库的日志尾部
  • MongoDB学习笔记:28663 Cannot start server. The default storage engine 'wiredTiger' is not available

文章分类

.NET技术 123 数据库 24 Web前端 21 网站建设运维 37 操作系统与应用 66 程序猿日常 11 开发工具 12 其他随笔 13

文章标签

ASP.NET ASP.NET MVC C# CSS HTML IIS Javascript Linux MongoDB MySql SQL SQL Server Visual Studio Windows系统 版本控制系统 插件工具 服务器 搞笑娱乐 好文分享 软件应用 生活知识 手机问题 随笔 网络知识 网站设计优化 网站维护 养生保健 异常处理 硬件设备 游戏攻略

热门文章

  • IIS8如何安装和使用URL重写工具-URL Rewrite
  • 林蛋大与楚中天,朱肚皮与朱月坡
  • 解决IE11安装升级失败和在安装前需要更新的问题
  • Windows Server 2012无法安装 .NET3.5-安装角色或功能失败,找不到源文件
  • VS重构重命名的快捷键

推荐文章

  • Windows Server 2012 如何开启虚拟内存
  • Windows Server 2012 提供程序NtpClient:在尝试联系它8次以后没有收到来自手动配置的对等机ntp1.cloud.aliyuncs.com,0x1的有效响应
  • 随笔:Visual Studio Community 2015安装程序的ISO光盘镜像下载地址
  • .NET项目在生成时如何将静态资源文件复制到输出目录
  • ASP.NET MVC中MvcHtmlString类的两个疑问:是什么以及怎么使用?

友情链接

  • Passingwind的博客
  • 码友网

知识共享许可协议 CC BY-SA 4.0本站作品采用知识共享许可协议:署名-相同方式共享 4.0 国际(CC BY-SA 4.0)。
闽ICP备15003702号
闽公网安备 35020302035102号