[Go] 解決packets.go:36: read tcp 127.0.0.1:51139->127.0.0.1:3306: wsarecv: An established connection was aborted by the software in your host ma chine.
阿新 • • 發佈:2021-10-10
這是因為資料庫的超時時間比較短,連線被mysql服務關閉了
程式還在使用舊連線查詢資料庫
比如gorm
我們程式裡設定下時間舊可以了,時間比超時時間短一些
DB.DB().SetConnMaxLifetime(59 * time.Second)
DB, err = gorm.Open("mysql", dsn) if err != nil { log.Println(err) panic("資料庫連線失敗!") return err } DB.SingularTable(true) DB.LogMode(true) DB.DB().SetMaxIdleConns(10) DB.DB().SetMaxOpenConns(100) DB.DB().SetConnMaxLifetime(59 * time.Second)