mysql连接超时的最佳解决方案是什么? [英] what is the best solutions to mysql connection timeout?
问题描述
我正在Go中编写一个小型Web应用程序,该应用程序使用mysql来存储数据.
I am writing a small web app in Go, which uses mysql to store data.
如果Web服务器在一段时间(> 8小时)后未收到任何请求,则会出现间歇性mysql错误:
I got intermittent mysql error if the web sever didn't get any request after some amount of time(> 8 hours):
[mysql] 2017/02/08 16:31:56 packets.go:33: unexpected EOF
[mysql] 2017/02/08 16:31:56 packets.go:130: write tcp 127.0.0.1:49188->127.0.0.1:3306: write: broken pipe
我在github上找到了一些相关的讨论( issue 529 ,问题257 和
I found some related discussion on github(issue 529, issue 257 and issue 446). From what I understand, mysql db would close the connection if timeout is reached.
我尝试将某些人建议的 SetMaxOpenConns
设置为9,将 SetMaxIdleConns
设置为0.但是,这立即引发了异常.(但是,如果我将 SetMaxIdleConns
设置为大于0,则不会立即引发异常)
I tried to set SetMaxOpenConns
to 9 and SetMaxIdleConns
to 0 as some people recommended. However, this threw exception immediately. (But if I set SetMaxIdleConns
larger than 0, there was no immediate exception thrown)
我还尝试将 SetConnMaxLifetime
设置为5分钟.5分钟后也抛出异常.
I also tried to set SetConnMaxLifetime
to 5 mins. This threw exception too after 5 mins.
现在我正在尝试以下代码:
Now I am trying the code below:
db.SetConnMaxLifetime(0)
db.SetMaxOpenConns(10)
db.SetMaxIdleConns(5)
它已经运行了20分钟.现在说还为时过早.(更新:这也不起作用)
It has been running for 20 mins. It's still too early to tell. (UPDATE: this doesn't work either)
这是配置:
- 驱动程序:go-sql-driver V1.3.
- go版本:go1.7.1 darwin/amd64
- mysql:最新来自Docker中心
- rkt版本:1.18
- CoreOS:1284.0.0
推荐答案
也许您可以启动心跳Goroutine以避免超时.
Perhaps you can start a heartbeat Goroutine to avoid timeout.
这篇关于mysql连接超时的最佳解决方案是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!