{"record":{"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":"exception","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/03d76c7e07908e255ce62d126d07ede3f2365d86/connector.go#L170-L206","documentation":"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.","triggerScenarios":"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.","commonSituations":"Pointing the driver at a MySQL-compatible-but-not-MySQL proxy/gateway that misreports system variables; explicitly zeroing maxAllowedPacket and hitting an unusual server.","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."],"exampleFix":"// before: driver reads system var, server returns garbage\n//   dsn: \"...?maxAllowedPacket=0\"\n//   -> invalid max_allowed_packet value (\"\"): strconv...\n\n// after: supply the value client-side\ndsn := \"user:pass@tcp(mysql:3306)/db?maxAllowedPacket=67108864\"","handlingStrategy":"validation","validationCode":"// Set maxAllowedPacket explicitly so the driver never has to parse the\n// server's system-variable value.\ndsn := \"user:pass@tcp(host:3306)/db?maxAllowedPacket=67108864\"","typeGuard":"func isInvalidMaxAllowedPacket(err error) bool {\n    return err != nil && strings.Contains(err.Error(), \"invalid max_allowed_packet value\")\n}","tryCatchPattern":"if isInvalidMaxAllowedPacket(err) {\n    // the %q field shows what the server returned; set an explicit\n    // maxAllowedPacket in the DSN to bypass the system-var read.\n}","preventionTips":["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."],"tags":["config","packet-size","handshake","compatibility"],"backgroundTag":null,"analyzedSha":"03d76c7e07908e255ce62d126d07ede3f2365d86","analyzedAt":"2026-08-07T10:39:17.340Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}