com.mongodb.MongoSocketReadTimeoutException: Timeout while receiving message

在window连接使用远程linux上的mongodb,经常出现这个错误,实在不能忍了,就去Google找了一下答案。
这是由于TCP的KeepAlive 机制导致的。可以了解一下TCP 中的 KeepAlive 机制

以下是mongoDB官方文档的答案。

如果在客户端和服务器之间或者没有其他合理原因的分片集群或副本集的成员之间遇到套接字错误,请检查TCP keepalive值(例如,tcp_keepalive_timeLinux系统上的 值)。常见的保持活动时间是 7200秒(2小时); 但是,不同的发行版和macOS可能有不同的设置。
对于MongoDB,您将获得更好的结果,保持期更短,大约为120秒(两分钟)。

如果是linux系统,可以使用以下命令查看保持活动设置

sysctl net.ipv4.tcp_keepalive_time

要更改该tcp_keepalive_time值,可以使用以下命令(value是你要设置的keepalive时间,单位是秒)

sudo sysctl -w net.ipv4.tcp_keepalive_time = <value>

对于网上的另一种答案,设置mongo的socketkeepAlive为true,这个方法已经过时了,默认为true。所以不需要手动设置。

项目突然跑不起来报这个错 Exception in monitor thread while connecting to server 10.18.21.79:27117 com.mongodb.MongoSocketReadException: Exception receiving message at com.mongodb.connection.InternalStreamConnection.translateReadException(InternalStreamConnection.java:536) at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:421) at com.mongodb.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:290) at com.mongodb.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255) at com.mongodb.connection.CommandHelper.sendAndReceive(CommandHelper.java:84) at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:34) at com.mongodb.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:91) at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:51) at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:127) at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114) at java.lang.Thread.run(Thread.java:748) Caused by: java.net.SocketException: Connection reset at java.net.SocketInputStream.read(SocketInputStream.java:210) at java.net.SocketInputStream.read(SocketInputStream.java:141) at com.mongodb.connection.SocketStream.read(SocketStream.java:84) at com.mongodb.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:547) at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:418)
最新发布
06-09
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值