{"record":{"id":"bca9adc42d284387","repo":"vitessio/vitess","slug":"error-ensuring-database-exists-v","errorCode":null,"errorMessage":"error ensuring database exists: %v","messagePattern":"error ensuring database exists: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/vt/vtcombo/tablet_map.go","lineNumber":368,"sourceCode":"\t\t\t\t// 2 replicas in order to ensure the primary cell has a primary and a replica\n\t\t\t\treplicas = 2\n\t\t\t}\n\t\t\trdonlys := int(kpb.RdonlyCount)\n\t\t\tif rdonlys == 0 {\n\t\t\t\trdonlys = 1\n\t\t\t}\n\n\t\t\tif ensureDatabase {\n\t\t\t\t// Create Database if not exist\n\t\t\t\tconn, err := mysqld.GetDbaConnection(context.TODO())\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn 0, fmt.Errorf(\"GetConnection failed: %v\", err)\n\t\t\t\t}\n\t\t\t\tdefer conn.Close()\n\n\t\t\t\t_, err = conn.ExecuteFetch(\"CREATE DATABASE IF NOT EXISTS `\"+dbname+\"`\", 1, false)\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn 0, fmt.Errorf(\"error ensuring database exists: %v\", err)\n\t\t\t\t}\n\t\t\t}\n\t\t\tif cell == tpb.Cells[0] {\n\t\t\t\treplicas--\n\n\t\t\t\t// create the primary\n\t\t\t\tif err := CreateTablet(ctx, env, ts, cell, uid, keyspace, shard, dbname, topodatapb.TabletType_PRIMARY, mysqld, dbcfgs.Clone(), srvTopoCounts); err != nil {\n\t\t\t\t\treturn 0, err\n\t\t\t\t}\n\t\t\t\tuid++\n\t\t\t}\n\n\t\t\tfor i := 0; i < replicas; i++ {\n\t\t\t\t// create a replica tablet\n\t\t\t\tif err := CreateTablet(ctx, env, ts, cell, uid, keyspace, shard, dbname, topodatapb.TabletType_REPLICA, mysqld, dbcfgs.Clone(), srvTopoCounts); err != nil {\n\t\t\t\t\treturn 0, err\n\t\t\t\t}\n\t\t\t\tuid++","sourceCodeStart":350,"sourceCodeEnd":386,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/vt/vtcombo/tablet_map.go#L350-L386","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Read the wrapped MySQL error in the message to identify the root cause (privilege vs read-only vs syntax).","Grant the DBA user CREATE privilege: GRANT CREATE ON *.* TO 'vt_dba'@'localhost'.","Confirm mysqld is writable (super_read_only=0) and not crashing.","Use a keyspace name with only valid MySQL identifier characters.","Retry after mysqld stabilizes if the connection was dropped."],"exampleFix":"// before\nksName := \"my keyspace!\" // produces invalid backticked SQL\n// after\nksName := \"my_keyspace\" // valid MySQL identifier","handlingStrategy":"try-catch","validationCode":"// ensure DBA user can create databases and mysqld is writable\nvar readOnly int\nif err := dbaConn.QueryRow(\"SELECT @@super_read_only\").Scan(&readOnly); err != nil || readOnly != 0 {\n\treturn fmt.Errorf(\"mysqld is read-only or unreachable\")\n}\n// validate keyspace name is a safe MySQL identifier\nif !regexp.MustCompile(`^[A-Za-z0-9_]+$`).MatchString(ksName) {\n\treturn fmt.Errorf(\"invalid keyspace name: %s\", ksName)\n}","typeGuard":null,"tryCatchPattern":"err := CreateKs(...)\nif err != nil && strings.Contains(err.Error(), \"error ensuring database exists\") {\n\tlog.Errorf(\"CREATE DATABASE failed, check DBA privileges/read-only mode: %v\", err)\n}","preventionTips":["Grant the DBA account CREATE privilege on *.*","Keep mysqld out of super_read_only during keyspace creation","Restrict keyspace names to valid MySQL identifier characters","Inspect the wrapped MySQL error before retrying"],"tags":["mysql","sql-error","vtcombo"],"backgroundTag":"create-database-failed","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}