go-sql-driver/mysql · error
invalid DSN: missing the slash separating the database name
Error message
invalid DSN: missing the slash separating the database name
What it means
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.
Source
Thrown at dsn.go:31
"context"
"crypto/rsa"
"crypto/tls"
"errors"
"fmt"
"maps"
"math/big"
"net"
"net/url"
"sort"
"strconv"
"strings"
"time"
)
var (
errInvalidDSNUnescaped = errors.New("invalid DSN: did you forget to escape a param value?")
errInvalidDSNAddr = errors.New("invalid DSN: network address not terminated (missing closing brace)")
errInvalidDSNNoSlash = errors.New("invalid DSN: missing the slash separating the database name")
errInvalidDSNUnsafeCollation = errors.New("invalid DSN: interpolateParams can not be used with unsafe collations")
)
// Config is a configuration parsed from a DSN string.
// If a new Config is created instead of being parsed from a DSN string,
// the NewConfig function should be used, which sets default values.
type Config struct {
// non boolean fields
User string // Username
Passwd string // Password (requires User)
Net string // Network (e.g. "tcp", "tcp6", "unix". default: "tcp")
Addr string // Address (default: "127.0.0.1:3306" for "tcp" and "/tmp/mysql.sock" for "unix")
DBName string // Database name
Params map[string]string // Connection parameters
ConnectionAttributes string // Connection Attributes, comma-delimited string of user-defined "key:value" pairs
Collation string // Connection collation. When set, this will be set in SET NAMES <charset> COLLATE <collation> query
Loc *time.Location // Location for time.Time valuesView on GitHub (pinned to c426bd9379)
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.
Example fix
// before dsn := "user:pass@tcp(localhost:3306)" // missing slash // after dsn := "user:pass@tcp(localhost:3306)/" // or .../mydb
Defensive patterns
Strategy: validation
Validate before calling
// A valid mysql DSN must contain at least one '/'.
func hasDSNSlash(dsn string) bool {
return strings.Contains(dsn, "/")
} Try / catch
// errInvalidDSNNoSlash is unexported; match by message.
if _, err := mysql.ParseDSN(dsn); err != nil && strings.Contains(err.Error(), "missing the slash") {
dsn = strings.TrimRight(dsn, "") + "/"
} Prevention
- 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.
When it happens
Trigger: ParseDSN or sql.Open('mysql', 'user:pass@tcp(host:3306)') with no trailing slash; or any non-empty DSN string with zero '/' characters.
Common situations: 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.
Related errors
- invalid DSN: did you forget to escape a param value?
- default addr for network '{cfg.Net}' unknown
- invalid value / unknown config name: {cfg.TLSConfig}
- invalid value / unknown server pub key name: {cfg.ServerPubK
- invalid bool value: {value}
AI-assisted analysis of go-sql-driver/mysql@c426bd9379 (2026-08-04).
Data as JSON: /data/errors/05d8b1fadb24b4d7.json.
Report an issue: GitHub.