golang-migrate/migrate · error

no database name

Error message

no database name

What it means

ErrNoDatabaseName in database/mysql is returned by WithInstance, Open and WithConnection when the database name resolved from the DSN or Config.DatabaseName is empty. MySQL needs the target schema to create/locate the schema_migrations table. The same sentinel also exists in cockroachdb, so the message may originate from either package.

Source

Thrown at database/mysql/mysql.go:35

	"sync/atomic"
	"time"

	"github.com/go-sql-driver/mysql"
	"github.com/golang-migrate/migrate/v4/database"
)

var _ database.Driver = (*Mysql)(nil) // explicit compile time type check

func init() {
	database.Register("mysql", &Mysql{})
}

var DefaultMigrationsTable = "schema_migrations"

var (
	ErrDatabaseDirty    = fmt.Errorf("database is dirty")
	ErrNilConfig        = fmt.Errorf("no config")
	ErrNoDatabaseName   = fmt.Errorf("no database name")
	ErrAppendPEM        = fmt.Errorf("failed to append PEM")
	ErrTLSCertKeyConfig = fmt.Errorf("to use TLS client authentication, both x-tls-cert and x-tls-key must not be empty")
)

type Config struct {
	MigrationsTable  string
	DatabaseName     string
	NoLock           bool
	StatementTimeout time.Duration
}

type Mysql struct {
	// mysql RELEASE_LOCK must be called from the same conn, so
	// just do everything over a single conn anyway.
	conn     *sql.Conn
	db       *sql.DB
	isLocked atomic.Bool

View on GitHub (pinned to 01a9643f14)

Solutions

  1. Include the schema in the DSN: user:pass@tcp(host:3306)/mydb
  2. Set Config.DatabaseName explicitly for WithInstance/WithConnection
  3. Fail fast in deployment config validation when the DB name env var is empty

Example fix

// before
m, err := mysql.Open("user:pass@tcp(localhost:3306)/")
// after
m, err := mysql.Open("user:pass@tcp(localhost:3306)/mydb")
Defensive patterns

Strategy: validation

Validate before calling

if cfg != nil && cfg.DatabaseName == "" {
    return fmt.Errorf("MYSQL_DATABASE must be set or included in the DSN path")
}
// for URL-based Open:
// ensure "tcp(host)/dbname" contains a non-empty db segment

Try / catch

if err != nil {
    if errors.Is(err, mysql.ErrNoDatabaseName) {
        return fmt.Errorf("mysql DSN/config missing database name")
    }
    return err
}

Prevention

When it happens

Trigger: DSN without a database name (e.g. user:pass@tcp(host)/), Config.DatabaseName == "" with WithInstance/WithConnection, Open on a URL whose path is empty.

Common situations: DSN templates where the DB env var is unset, connection strings copied from tools that use a connect-time database selection, renaming a schema without updating migrate config.

Related errors


AI-assisted analysis of golang-migrate/migrate@01a9643f14 (2026-09-02). Data as JSON: /api/errors/0ab945df3986e675. Report an issue: GitHub.