postgresql远程连接中断的处理
在网络上连接远程服务器postgresql时,不活动时间稍长就会自动断开连接,不利于操作。
琢磨了一下,服务器上使用以下网络配置时,解决了这个问题。
#man 7 tcp
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_intvl = 15
同时要注意postgresql.conf中以下几项设置:
#tcp_keepalives_idle = 0 # TCP_KEEPIDLE, in seconds; 0 selects the system default
#tcp_keepalives_interval = 0 # TCP_KEEPINTVL, in seconds;0 selects the system default
#tcp_keepalives_count = 0 # TCP_KEEPCNT;0 selects the system default
#tcp_keepalives_interval = 0 # TCP_KEEPINTVL, in seconds;0 selects the system default
#tcp_keepalives_count = 0 # TCP_KEEPCNT;0 selects the system default
系统是debian7,默认的超时设置太长,而外网网络状况不佳,所以导致这种问题。
这个配置的思路是让pg更频繁地发出探测数据包来保持tcp连接。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步