Go-sql-driver: 关闭错误的空闲连接:连接被对等方重置(mysql)

huangapple go评论155阅读模式
英文:

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)
	 }
   }
}

huangapple
  • 本文由 发表于 2021年12月15日 01:49:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/70353426.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定