vitessio/vitess · error

error ensuring database exists: %v

Error message

error ensuring database exists: %v

What it means

After obtaining a DBA connection, CreateKs runs `CREATE DATABASE IF NOT EXISTS <dbname>` to materialize the keyspace's MySQL database. This error wraps a failure of that statement execution — the connection succeeded but the SQL failed.

Source

Thrown at go/vt/vtcombo/tablet_map.go:368

				// 2 replicas in order to ensure the primary cell has a primary and a replica
				replicas = 2
			}
			rdonlys := int(kpb.RdonlyCount)
			if rdonlys == 0 {
				rdonlys = 1
			}

			if ensureDatabase {
				// Create Database if not exist
				conn, err := mysqld.GetDbaConnection(context.TODO())
				if err != nil {
					return 0, fmt.Errorf("GetConnection failed: %v", err)
				}
				defer conn.Close()

				_, err = conn.ExecuteFetch("CREATE DATABASE IF NOT EXISTS `"+dbname+"`", 1, false)
				if err != nil {
					return 0, fmt.Errorf("error ensuring database exists: %v", err)
				}
			}
			if cell == tpb.Cells[0] {
				replicas--

				// create the primary
				if err := CreateTablet(ctx, env, ts, cell, uid, keyspace, shard, dbname, topodatapb.TabletType_PRIMARY, mysqld, dbcfgs.Clone(), srvTopoCounts); err != nil {
					return 0, err
				}
				uid++
			}

			for i := 0; i < replicas; i++ {
				// create a replica tablet
				if err := CreateTablet(ctx, env, ts, cell, uid, keyspace, shard, dbname, topodatapb.TabletType_REPLICA, mysqld, dbcfgs.Clone(), srvTopoCounts); err != nil {
					return 0, err
				}
				uid++

View on GitHub (pinned to 01a25a7d17)

Solutions

  1. Read the wrapped MySQL error in the message to identify the root cause (privilege vs read-only vs syntax).
  2. Grant the DBA user CREATE privilege: GRANT CREATE ON *.* TO 'vt_dba'@'localhost'.
  3. Confirm mysqld is writable (super_read_only=0) and not crashing.
  4. Use a keyspace name with only valid MySQL identifier characters.
  5. Retry after mysqld stabilizes if the connection was dropped.

Example fix

// before
ksName := "my keyspace!" // produces invalid backticked SQL
// after
ksName := "my_keyspace" // valid MySQL identifier
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure DBA user can create databases and mysqld is writable
var readOnly int
if err := dbaConn.QueryRow("SELECT @@super_read_only").Scan(&readOnly); err != nil || readOnly != 0 {
	return fmt.Errorf("mysqld is read-only or unreachable")
}
// validate keyspace name is a safe MySQL identifier
if !regexp.MustCompile(`^[A-Za-z0-9_]+$`).MatchString(ksName) {
	return fmt.Errorf("invalid keyspace name: %s", ksName)
}

Try / catch

err := CreateKs(...)
if err != nil && strings.Contains(err.Error(), "error ensuring database exists") {
	log.Errorf("CREATE DATABASE failed, check DBA privileges/read-only mode: %v", err)
}

Prevention

When it happens

Trigger: CreateKs executing CREATE DATABASE against a mysqld where the statement errors: DBA user lacking CREATE privilege, mysqld in read-only/super_read_only mode, connection dropped mid-query, or malformed dbname (backtick-injection via keyspace name).

Common situations: Read-only replica mistakenly used as target; restricted DBA account in production-like setups; invalid characters in the keyspace name breaking the backticked SQL; mysqld shutting down during vtcombo init.

Related errors


AI-assisted analysis of vitessio/vitess@01a25a7d17 (2026-09-01). Data as JSON: /api/errors/bca9adc42d284387. Report an issue: GitHub.