{"id":"05d8b1fadb24b4d7","repo":"go-sql-driver/mysql","slug":"invalid-dsn-missing-the-slash-separating-the-data","errorCode":null,"errorMessage":"invalid DSN: missing the slash separating the database name","messagePattern":"invalid DSN: missing the slash separating the database name","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"dsn.go","lineNumber":31,"sourceCode":"\t\"context\"\n\t\"crypto/rsa\"\n\t\"crypto/tls\"\n\t\"errors\"\n\t\"fmt\"\n\t\"maps\"\n\t\"math/big\"\n\t\"net\"\n\t\"net/url\"\n\t\"sort\"\n\t\"strconv\"\n\t\"strings\"\n\t\"time\"\n)\n\nvar (\n\terrInvalidDSNUnescaped       = errors.New(\"invalid DSN: did you forget to escape a param value?\")\n\terrInvalidDSNAddr            = errors.New(\"invalid DSN: network address not terminated (missing closing brace)\")\n\terrInvalidDSNNoSlash         = errors.New(\"invalid DSN: missing the slash separating the database name\")\n\terrInvalidDSNUnsafeCollation = errors.New(\"invalid DSN: interpolateParams can not be used with unsafe collations\")\n)\n\n// Config is a configuration parsed from a DSN string.\n// If a new Config is created instead of being parsed from a DSN string,\n// the NewConfig function should be used, which sets default values.\ntype Config struct {\n\t// non boolean fields\n\n\tUser                 string            // Username\n\tPasswd               string            // Password (requires User)\n\tNet                  string            // Network (e.g. \"tcp\", \"tcp6\", \"unix\". default: \"tcp\")\n\tAddr                 string            // Address (default: \"127.0.0.1:3306\" for \"tcp\" and \"/tmp/mysql.sock\" for \"unix\")\n\tDBName               string            // Database name\n\tParams               map[string]string // Connection parameters\n\tConnectionAttributes string            // Connection Attributes, comma-delimited string of user-defined \"key:value\" pairs\n\tCollation            string            // Connection collation. When set, this will be set in SET NAMES <charset> COLLATE <collation> query\n\tLoc                  *time.Location    // Location for time.Time values","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/go-sql-driver/mysql/blob/c426bd93799de0f0e094c8f0582872c529d0ed0a/dsn.go#L13-L49","documentation":"ParseDSN scans the DSN from the right looking for a '/' that separates the connection spec from the database name. If the string is non-empty yet contains no '/' at all, it returns errInvalidDSNNoSlash at dsn.go:473. The slash is mandatory even when the database name is empty.","triggerScenarios":"ParseDSN or sql.Open('mysql', 'user:pass@tcp(host:3306)') with no trailing slash; or any non-empty DSN string with zero '/' characters.","commonSituations":"Concatenating host/user segments but forgetting the '/dbname' suffix; an env var that got its trailing '/' trimmed; a config template that conditionally omits the database segment.","solutions":["Append '/' (optionally followed by the database name) to the DSN: 'user:pass@tcp(host:3306)/'.","Build the DSN from a *mysql.Config via cfg.FormatDSN() so the slash is always emitted correctly.","If the DSN comes from an env var, validate it contains a '/' before passing it to sql.Open."],"exampleFix":"// before\ndsn := \"user:pass@tcp(localhost:3306)\" // missing slash\n// after\ndsn := \"user:pass@tcp(localhost:3306)/\" // or .../mydb","handlingStrategy":"validation","validationCode":"// A valid mysql DSN must contain at least one '/'.\nfunc hasDSNSlash(dsn string) bool {\n    return strings.Contains(dsn, \"/\")\n}","typeGuard":null,"tryCatchPattern":"// errInvalidDSNNoSlash is unexported; match by message.\nif _, err := mysql.ParseDSN(dsn); err != nil && strings.Contains(err.Error(), \"missing the slash\") {\n    dsn = strings.TrimRight(dsn, \"\") + \"/\"\n}","preventionTips":["Always end the connection spec with '/' even when no database is selected.","Construct DSNs via cfg.FormatDSN() so the slash is guaranteed.","Validate env-provided DSNs in a config loader before they reach sql.Open."],"tags":["go","mysql","dsn","config"],"analyzedSha":"c426bd93799de0f0e094c8f0582872c529d0ed0a","analyzedAt":"2026-08-04T21:52:59.219Z","schemaVersion":2}