HAProxy出现"远程主机强迫关闭了一个现有的连接 " 的错误及解决

 使用haproxy作为sql server 的负载均衡器。

 使用了文档中的示例配置项:

        timeout client 50s
        timeout server 50s

采用这个配置项,有时会出现“远程主机强迫关闭了一个现有的连接”的错误。

出现这个错误的原因是:一旦sql client超过50s没有给haproxy发送数据,则haproxy会关闭这个连接,此时sql client中的以为连接还是通的,此时发送数据就会出现异常。

 

在查看了HAProxy的官方文档,以及mysql和sql server的文档之后,将配置改为:

        timeout client 8h
        timeout server 8h

之后正常了。为什么设置为8小时呢?sql server没有查到类似wait timeout的资料,于是借鉴了mysql的配置项,mysql有一个名为wait timeout的配置项,它的默认时间为8小时,这个配置项的意思是当mysql发现与客户端的tcp连接如果超过8个小时不活动的话(收发数据),那么mysql就关闭这个连接。这个配置项跟http1.1中的keep alive timeout是类似的。

 

参考:

Apache: Keep-alive timeout

Amount of time the server will wait for subsequent requests on a persistent connection

 

Mysql: wait_timeout

The number of seconds the server waits for activity on an interactive connection before closing it.

 

HAProxy: timeout client

The inactivity timeout applies when the client is expected to acknowledge or send data.

posted @ 2015-12-12 14:37  何德海  阅读(2621)  评论(0编辑  收藏  举报