douwen3083 2016-10-11 15:24
浏览 902
已采纳

Golang MySQL错误-packet.go:33:意外的EOF

I am switching my entire code base from PHP to Go and during several processes that run, I randomly get this error:

[mysql] 2016/10/11 09:17:16 packets.go:33: unexpected EOF

Here is my db package that handles all connections to the database:

package db

import (
    "database/sql"
    _ "github.com/go-sql-driver/mysql"
    "pkg/db"
)

var connection *sql.DB
var err error

func GetConnection() *sql.DB {
    if connection != nil {
        fmt.Println("********** CHECKING PING")
        err = connection.Ping()
        if err == nil {
            fmt.Println("************ CONNECTION STILL ACTIVE")
            return connection
        } else {
            fmt.Println("********** PING ERROR: " + err.Error())
        }
    }

    connection, err = sql.Open("mysql", db.DEVUSER + ":" + db.DEVUSER_PASSWORD + "@tcp(localhost:3306)/main?parseTime=true")
    if err != nil {
        panic(err)
    }

    return connection
}

Is there anything I'm doing wrong with this db package that causes this error to be thrown? What exactly does this error mean? I make sure to return the current connection if there is one open so for multiple requests it uses the same connection object.

Here's an excerpt from the mysql packets.go:

// Read packet to buffer 'data'
func (mc *mysqlConn) readPacket() ([]byte, error) {
    var payload []byte
    for {
        // Read packet header
        data, err := mc.buf.readNext(4)
        if err != nil {
            errLog.Print(err)
            mc.Close()
            return nil, driver.ErrBadConn
        }

        // Packet Length [24 bit]
        pktLen := int(uint32(data[0]) | uint32(data[1])<<8 | uint32(data[2])<<16)

        if pktLen < 1 {
            errLog.Print(ErrMalformPkt)
            mc.Close()
            return nil, driver.ErrBadConn
        }

        // Check Packet Sync [8 bit]
        if data[3] != mc.sequence {
            if data[3] > mc.sequence {
                return nil, ErrPktSyncMul
            }
            return nil, ErrPktSync
        }
        mc.sequence++

        // Read packet body [pktLen bytes]
        data, err = mc.buf.readNext(pktLen)
        if err != nil {
            errLog.Print(err)
            mc.Close()
            return nil, driver.ErrBadConn
        }

        isLastPacket := (pktLen < maxPacketSize)

        // Zero allocations for non-splitting packets
        if isLastPacket && payload == nil {
            return data, nil
        }

        payload = append(payload, data...)

        if isLastPacket {
            return payload, nil
        }
    }
}

The first "errLog.Print(err)" is line 33 in the "Read packet header" section.

Any help is greatly appreciated!

I added a few log.Println to the connection package and let the process run, and right where I get this error, this is what the console prints:

********** CHECKING PING
************ CONNECTION STILL ACTIVE
[mysql] 2016/10/11 11:57:27 packets.go:33: unexpected EOF
********** CHECKING PING
************ CONNECTION STILL ACTIVE
  • 写回答

1条回答 默认 最新

  • duanjiao5723 2017-02-09 20:46
    关注

    Looks like the link to the github issue provided the fix. The fix, at least for my situation was setting the MaxIdleConnections to 0. I have kept a server up for 24 hours, running queries against it every several hours and have yet to reproduce the error.

    Thanks to @city for the link.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误