英文:
Go-sql-driver: Closing bad idle connection: connection reset by peer (mysql)
问题
一个Go服务通常会收到错误消息“closing bad idle connection: connection reset by peer”
错误日志
[mysql] 2021/01/16 20:08:27 packets.go:122: closing bad idle connection: connection reset by peer
[mysql] 2021/01/16 20:13:27 packets.go:122: closing bad idle connection: connection reset by peer
配置
- 驱动程序版本:v1.5.0
- Go版本:go1.15.6 darwin/amd64
- 服务器版本:MySQL 5.7.29
有什么办法可以修复这个问题吗?
英文:
A go service usually get the error messages "closing bad idle connection: connection reset by peer"
Error log
[mysql] 2021/01/16 20:08:27 packets.go:122: closing bad idle connection: connection reset by peer
[mysql] 2021/01/16 20:13:27 packets.go:122: closing bad idle connection: connection reset by peer
Configuration
- Driver version: v1.5.0
- Go version: go1.15.6 darwin/amd64
- Server version: MySQL 5.7.29
Any idea, how to fix this?
答案1
得分: 1
如果您有一个长时间保持打开的数据库连接,您需要定期检查连接。
func checkPing() {
for {
time.Sleep(time.Second * 15)
err := DB.DB().Ping()
if err != nil {
log.Println(err)
}
}
}
如果您有一个长时间保持打开的数据库连接,您需要定期检查连接。以上是一个示例函数,它使用每隔15秒执行一次的循环来检查数据库连接的状态。如果连接出现错误,它将记录错误信息。
英文:
If you have a database connection that remains open for a long time, you need to check the connection periodically.
func checkPing() {
for {
time.Sleep(time.Second * 15)
err := DB.DB().Ping()
if err != nil {
log.Println(err)
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论