{"id":"c324598c419d94a5","repo":"go-sql-driver/mysql","slug":"invalid-max-allowed-packet-value-q-w","errorCode":null,"errorMessage":"invalid max_allowed_packet value (%q): %w","messagePattern":"invalid max_allowed_packet value \\(%q\\): %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"connector.go","lineNumber":188,"sourceCode":"\n\t// compression is enabled after auth, not right after sending handshake response.\n\tif mc.capabilities&clientCompress > 0 {\n\t\tmc.compress = true\n\t\tmc.compIO = newCompIO(mc)\n\t}\n\tif mc.cfg.MaxAllowedPacket > 0 {\n\t\tmc.maxAllowedPacket = mc.cfg.MaxAllowedPacket\n\t} else {\n\t\t// Get max allowed packet size\n\t\tmaxap, err := mc.getSystemVar(\"max_allowed_packet\")\n\t\tif err != nil {\n\t\t\tmc.Close()\n\t\t\treturn nil, err\n\t\t}\n\t\tn, err := strconv.Atoi(maxap)\n\t\tif err != nil {\n\t\t\tmc.Close()\n\t\t\treturn nil, fmt.Errorf(\"invalid max_allowed_packet value (%q): %w\", maxap, err)\n\t\t}\n\t\tmc.maxAllowedPacket = n - 1\n\t}\n\tif mc.maxAllowedPacket < maxPacketSize {\n\t\tmc.maxWriteSize = mc.maxAllowedPacket\n\t}\n\n\t// Charset: character_set_connection, character_set_client, character_set_results\n\tif len(mc.cfg.charsets) > 0 {\n\t\tfor _, cs := range mc.cfg.charsets {\n\t\t\t// ignore errors here - a charset may not exist\n\t\t\tif mc.cfg.Collation != \"\" {\n\t\t\t\terr = mc.exec(\"SET NAMES \" + cs + \" COLLATE \" + mc.cfg.Collation)\n\t\t\t} else {\n\t\t\t\terr = mc.exec(\"SET NAMES \" + cs)\n\t\t\t}\n\t\t\tif err == nil {\n\t\t\t\tbreak","sourceCodeStart":170,"sourceCodeEnd":206,"githubUrl":"https://github.com/go-sql-driver/mysql/blob/c426bd93799de0f0e094c8f0582872c529d0ed0a/connector.go#L170-L206","documentation":"Thrown in connector.go:188 when the server's max_allowed_packet system variable (queried right after auth, since the DSN did not set MaxAllowedPacket) returns a value that strconv.Atoi cannot parse as an integer. The %q is the raw server value and %w wraps the underlying strconv error. The driver needs this number to size writes, so a non-numeric reply aborts the connection.","triggerScenarios":"Opening a connection whose DSN does NOT specify maxAllowedPacket, where the server's response to SELECT @@max_allowed_packet (or equivalent) is non-numeric — e.g. NULL, an empty string, or garbage from a misbehaving proxy/server. The connection setup fails before any query runs.","commonSituations":"A proxy/load balancer that returns a malformed response for system-variable queries; a non-MySQL-compatible server that doesn't expose max_allowed_packet; server misconfiguration; connection pooling against a flaky backend that returns a partial handshake. Setting maxAllowedPacket in the DSN sidesteps the query entirely.","solutions":["Set maxAllowedPacket in the DSN (e.g. ?maxAllowedPacket=4194304) so the driver never queries the server for it.","Verify the server is a genuine MySQL and that SELECT @@max_allowed_packet returns a number via the mysql CLI.","Remove proxies/routers from the path that may mangle the system-variable response.","If the server genuinely can't answer, set maxAllowedPacket explicitly to a safe value for your workload."],"exampleFix":"// before: DSN omits maxAllowedPacket, driver queries the server\n// dsn := \"user:pass@tcp(host:3306)/db\"\n\n// after: set it explicitly to skip the server query\ndsn := \"user:pass@tcp(host:3306)/db?maxAllowedPacket=4194304\"\ndb, err := sql.Open(\"mysql\", dsn)","handlingStrategy":"validation","validationCode":"// set maxAllowedPacket in the DSN so the server is never queried for it\nfunc dsnWithMaxPacket(base string, bytes int) string {\n    sep := \"?\"\n    if strings.Contains(base, \"?\") {\n        sep = \"&\"\n    }\n    return fmt.Sprintf(\"%s%smaxAllowedPacket=%d\", base, sep, bytes)\n}","typeGuard":null,"tryCatchPattern":"db, err := sql.Open(\"mysql\", dsn)\nif err := db.PingContext(ctx); err != nil {\n    if strings.Contains(err.Error(), \"invalid max_allowed_packet value\") {\n        // add maxAllowedPacket=<n> to the DSN and retry\n    }\n}","preventionTips":["Always set maxAllowedPacket explicitly in the DSN.","Confirm SELECT @@max_allowed_packet returns a number via the mysql CLI when in doubt.","Keep proxies out of the post-auth system-variable exchange."],"tags":["connection","dsn","max-allowed-packet","handshake"],"analyzedSha":"c426bd93799de0f0e094c8f0582872c529d0ed0a","analyzedAt":"2026-08-04T21:52:59.219Z","schemaVersion":2}