googleapis/mcp-toolbox · error

sql.Open: %w

Error message

sql.Open: %w

What it means

This error wraps a failure from database/sql's sql.Open when opening the MySQL connection pool. sql.Open validates the DSN produced by FormatDSN(); since the go-sql-driver defers connection to first use, a failure here is almost always a malformed DSN (bad config parameter, invalid net address format) or unknown driver registration, not a network problem.

Source

Thrown at internal/sources/mysql/mysql.go:232

	for k, v := range queryParams {
		if v == "" {
			continue // skip empty values
		}
		params[k] = v
	}
	config.Params = params

	userAgent, err := util.UserAgentFromContext(ctx)
	if err != nil {
		return nil, err
	}
	config.ConnectionAttributes = fmt.Sprintf("program_name:%s", userAgent)
	dsn := config.FormatDSN()

	// Interact with the driver directly as you normally would
	pool, err := sql.Open("mysql", dsn)
	if err != nil {
		return nil, fmt.Errorf("sql.Open: %w", err)
	}
	return pool, nil
}

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Inspect the wrapped error for the specific DSN parse message and fix the offending config field.
  2. Escape special characters in user/password (or set allowNativePasswords=true and pass an empty password via the DSN-safe form).
  3. Verify host and port formatting (hostname:port; IPv6 as [::1]:3306).
  4. Log or print the formatted DSN locally (with secrets redacted) using go-sql-driver/mysql's mysql.Config.FormatDSN to see exactly what is generated.
  5. Confirm the mysql driver import (blank import of the driver package) is present so the 'mysql' driver is registered.

Example fix

// before
user: 'p@ss word'
// after (escape DSN-special characters, or enable native passwords)
user: 'appuser'
password: 'p@ss word'
allowNativePasswords: true
Defensive patterns

Strategy: validation

Validate before calling

func validateMySQLConfig(user, pass, host, port string) error {
    // DSN-special characters must be escaped or handled via allowNativePasswords
    for name, v := range map[string]string{"user": user, "password": pass, "host": host, "port": port} {
        if strings.ContainsAny(v, "@:/(") {
            return fmt.Errorf("%s contains DSN-special characters (@ : / ( ) that must be escaped", name)
        }
    }
    if _, err := strconv.Atoi(port); err != nil {
        return fmt.Errorf("port %q is not numeric", port)
    }
    return nil
}

Prevention

When it happens

Trigger: Initializing the mysql source with config values that produce an invalid DSN — e.g. malformed host/port characters, illegal characters in user/password/query parameters (unescaped @, /, or ()), or an invalid ConnectionAttributes value.

Common situations: Passwords containing special DSN characters (@, :, /, (, )) that were not escaped with AllowNativePasswords or url escaping, IPv6 literal hosts in brackets mis-formatted, empty required fields leaving a broken DSN, or query params injected with invalid keys.

Related errors


AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05). Data as JSON: /api/errors/415a8e32b5343df5. Report an issue: GitHub.