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

  1. Set an explicit maxAllowedPacket in the DSN (e.g. maxAllowedPacket=67108864) so the driver skips reading the server variable entirely.
  2. Ensure the endpoint is a standards-compliant MySQL/MariaDB server returning a numeric max_allowed_packet.
  3. Fix or bypass any proxy that rewrites the SHOW VARIABLES / system-variable response.
  4. 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

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


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 {
				break

View on GitHub (pinned to 03d76c7e07)