go-sql-driver/mysql · error
invalid max_allowed_packet value
Error message
invalid max_allowed_packet value (%q): %w
What it means
Returned at connect time (connector.go:188) when, after the handshake, the driver queries the server's max_allowed_packet system variable and strconv.Atoi fails on the returned string. This path runs only when Config.MaxAllowedPacket == 0 (the connector otherwise uses the configured value at connector.go:176); the error wraps the underlying strconv error.
Solutions
- Set an explicit maxAllowedPacket in the DSN (e.g. maxAllowedPacket=67108864) so the driver skips reading the server variable entirely.
- Ensure the endpoint is a standards-compliant MySQL/MariaDB server returning a numeric max_allowed_packet.
- Fix or bypass any proxy that rewrites the SHOW VARIABLES / system-variable response.
- Inspect the actual returned value (the %q in the message) to see what the server sent.
Example fix
// before: driver reads system var, server returns garbage
// dsn: "...?maxAllowedPacket=0"
// -> invalid max_allowed_packet value (""): strconv...
// after: supply the value client-side
dsn := "user:pass@tcp(mysql:3306)/db?maxAllowedPacket=67108864" Defensive patterns
Strategy: validation
Validate before calling
// Set maxAllowedPacket explicitly so the driver never has to parse the // server's system-variable value. dsn := "user:pass@tcp(host:3306)/db?maxAllowedPacket=67108864"
Type guard
func isInvalidMaxAllowedPacket(err error) bool {
return err != nil && strings.Contains(err.Error(), "invalid max_allowed_packet value")
} Try / catch
if isInvalidMaxAllowedPacket(err) {
// the %q field shows what the server returned; set an explicit
// maxAllowedPacket in the DSN to bypass the system-var read.
} Prevention
- Always set maxAllowedPacket explicitly in production DSNs.
- Ensure the endpoint is a real MySQL/MariaDB server.
- Make any gateway fully transparent to system-variable responses.
When it happens
Trigger: Connecting with maxAllowedPacket=0 in the DSN (or a Config with MaxAllowedPacket left at 0) against a server whose max_allowed_packet system variable returns a non-numeric value — e.g. a non-MySQL-compatible service, a proxy that returns a textual/empty value, or a server fork with an unusual format.
Common situations: Pointing the driver at a MySQL-compatible-but-not-MySQL proxy/gateway that misreports system variables; explicitly zeroing maxAllowedPacket and hitting an unusual server.
Related errors
- MySQL server does not support required protocol 41+
- packet for query is too large. Try adjusting the…
- this authentication plugin is not supported
- this user requires mysql native password authentication
- invalid compressed packet: uncompressed length in header is
AI-assisted analysis of go-sql-driver/mysql@03d76c7e07 (2026-08-07).
Data as JSON: /api/errors/c324598c419d94a5.
Report an issue: GitHub.
Appendix: source
Thrown at connector.go:188
// compression is enabled after auth, not right after sending handshake response.
if mc.capabilities&clientCompress > 0 {
mc.compress = true
mc.compIO = newCompIO(mc)
}
if mc.cfg.MaxAllowedPacket > 0 {
mc.maxAllowedPacket = mc.cfg.MaxAllowedPacket
} else {
// Get max allowed packet size
maxap, err := mc.getSystemVar("max_allowed_packet")
if err != nil {
mc.Close()
return nil, err
}
n, err := strconv.Atoi(maxap)
if err != nil {
mc.Close()
return nil, fmt.Errorf("invalid max_allowed_packet value (%q): %w", maxap, err)
}
mc.maxAllowedPacket = n - 1
}
if mc.maxAllowedPacket < maxPacketSize {
mc.maxWriteSize = mc.maxAllowedPacket
}
// Charset: character_set_connection, character_set_client, character_set_results
if len(mc.cfg.charsets) > 0 {
for _, cs := range mc.cfg.charsets {
// ignore errors here - a charset may not exist
if mc.cfg.Collation != "" {
err = mc.exec("SET NAMES " + cs + " COLLATE " + mc.cfg.Collation)
} else {
err = mc.exec("SET NAMES " + cs)
}
if err == nil {
breakView on GitHub (pinned to 03d76c7e07)