go-sql-driver/mysql · error

default addr for network '{cfg.Net}' unknown

Error message

default addr for network '{cfg.Net}' unknown

What it means

normalize() auto-fills a default address only for Net=='tcp' (127.0.0.1:3306) and Net=='unix' (/tmp/mysql.sock). For any other Net value with an empty Addr it returns 'default addr for network <net> unknown' at dsn.go:190. Net is case-sensitive, so 'TCP' or 'Unix' are treated as unknown networks.

Source

Thrown at dsn.go:190

func (cfg *Config) normalize() error {
	if cfg.InterpolateParams && cfg.Collation != "" && unsafeCollations[cfg.Collation] {
		return errInvalidDSNUnsafeCollation
	}

	// Set default network if empty
	if cfg.Net == "" {
		cfg.Net = "tcp"
	}

	// Set default address if empty
	if cfg.Addr == "" {
		switch cfg.Net {
		case "tcp":
			cfg.Addr = "127.0.0.1:3306"
		case "unix":
			cfg.Addr = "/tmp/mysql.sock"
		default:
			return errors.New("default addr for network '" + cfg.Net + "' unknown")
		}
	} else if cfg.Net == "tcp" {
		cfg.Addr = ensureHavePort(cfg.Addr)
	}

	if cfg.TLS == nil {
		switch cfg.TLSConfig {
		case "false", "":
			// don't set anything
		case "true":
			cfg.TLS = &tls.Config{}
		case "skip-verify":
			cfg.TLS = &tls.Config{InsecureSkipVerify: true}
		case "preferred":
			cfg.TLS = &tls.Config{InsecureSkipVerify: true}
			cfg.AllowFallbackToPlaintext = true
		default:
			cfg.TLS = getTLSConfigClone(cfg.TLSConfig)

View on GitHub (pinned to c426bd9379)

Solutions

  1. Use lowercase 'tcp' or 'unix' for Net.
  2. If you genuinely need a non-standard net label, set cfg.Addr explicitly (or put (addr) in the DSN) so normalize() does not need a default.
  3. Check Net spelling/case against the two accepted values before opening.

Example fix

// before
cfg := mysql.NewConfig()
cfg.Net = "TCP" // wrong case
cfg.DBName = "db"
// after
cfg.Net = "tcp" // or "unix"
Defensive patterns

Strategy: validation

Validate before calling

func validNet(net, addr string) bool {
    if addr != "" { return true }
    return net == "tcp" || net == "unix"
}

Try / catch

if _, err := mysql.ParseDSN(dsn); err != nil && strings.Contains(err.Error(), "default addr for network") {
    // fix Net case to tcp/unix, or set Addr explicitly
}

Prevention

When it happens

Trigger: Config{Net:'udp'}, Config{Net:'TCP'} (wrong case), or a DSN 'user@udp()/db' with no explicit address; any custom net label without an Addr.

Common situations: Case typo ('TCP' instead of 'tcp'); a custom DialFunc with a non-standard net label but Addr left blank; misreading docs that capitalize 'TCP'.

Related errors


AI-assisted analysis of go-sql-driver/mysql@c426bd9379 (2026-08-04). Data as JSON: /data/errors/6ecb4618293a146c.json. Report an issue: GitHub.