{"record":{"id":"cfdae4f8e2e9c3e1","repo":"vitessio/vitess","slug":"failed-to-create-database-v","errorCode":null,"errorMessage":"failed to create database: %v","messagePattern":"failed to create database: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/vt/vtctl/grpcvtctldserver/server.go","lineNumber":3027,"sourceCode":"\t// will most likely be a timeout, and our context will be\n\t// expired, so the rebuild will fail anyway)\n\twgReplicas.Wait()\n\tif err := rec.Error(); err != nil {\n\t\treturn err\n\t}\n\n\t// Create database if necessary on the primary. replicas will get it too through\n\t// replication. Since the user called InitShardPrimary, they've told us to\n\t// assume that whatever data is on all the replicas is what they intended.\n\t// If the database doesn't exist, it means the user intends for these tablets\n\t// to begin serving with no data (i.e. first time initialization).\n\tcreateDB := \"CREATE DATABASE IF NOT EXISTS \" + sqlescape.EscapeID(topoproto.TabletDbName(primaryElectTabletInfo.Tablet))\n\tif _, err := tmc.ExecuteFetchAsDba(ctx, primaryElectTabletInfo.Tablet, false, &tabletmanagerdatapb.ExecuteFetchAsDbaRequest{\n\t\tQuery:        []byte(createDB),\n\t\tMaxRows:      1,\n\t\tReloadSchema: true,\n\t}); err != nil {\n\t\treturn fmt.Errorf(\"failed to create database: %v\", err)\n\t}\n\t// Refresh the state to force the tabletserver to reconnect after db has been created.\n\tif err := tmc.RefreshState(ctx, primaryElectTabletInfo.Tablet); err != nil {\n\t\tlog.Warn(fmt.Sprintf(\"RefreshState failed: %v\", err))\n\t}\n\n\treturn nil\n}\n\n// LaunchSchemaMigration is part of the vtctlservicepb.VtctldServer interface.\nfunc (s *VtctldServer) LaunchSchemaMigration(ctx context.Context, req *vtctldatapb.LaunchSchemaMigrationRequest) (resp *vtctldatapb.LaunchSchemaMigrationResponse, err error) {\n\tspan, ctx := trace.NewSpan(ctx, \"VtctldServer.LaunchSchemaMigration\")\n\tdefer span.Finish()\n\n\tdefer panicHandler(&err)\n\n\tspan.Annotate(\"keyspace\", req.Keyspace)\n\tspan.Annotate(\"uuid\", req.Uuid)","sourceCodeStart":3009,"sourceCodeEnd":3045,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/vt/vtctl/grpcvtctldserver/server.go#L3009-L3045","documentation":"During InitShardPrimary (legacy path), the new primary is asked (via ExecuteFetchAsDba) to run CREATE DATABASE IF NOT EXISTS for its db name. If that SQL fails on the tablet, this error is returned and the shard is left without an initialized primary database.","triggerScenarios":"tmc.ExecuteFetchAsDba issuing `CREATE DATABASE IF NOT EXISTS <db>` fails — mysqld down, permissions insufficient, disk full, or invalid db name characters.","commonSituations":"Fresh tablet whose datadir is not initialized; mysqld running with restrictive grants for the vt_dba user; filesystem full on the primary host.","solutions":["Check mysqld is running on the primary and the vt_dba account has CREATE privileges","Free disk space if mysqld errored with disk-full","Create the database manually on mysql and retry the operation","Verify tablet db name configuration (init_db_name_override) matches the keyspace expectation"],"exampleFix":"// before: dba user lacks CREATE privilege\nmysql> GRANT ALL ON *.* TO 'vt_dba'@'localhost';\n// after: retry reparent/init\nvtctldclient PlannedReparentShard ks/shard","handlingStrategy":"validation","validationCode":"// verify mysqld reachable and dba can create DBs before init\nif err := probeMysql(primary); err != nil { return err }\nif !dbaHasPrivilege(primary, \"CREATE\") {\n    return errors.New(\"vt_dba lacks CREATE privilege\")\n}","typeGuard":null,"tryCatchPattern":"// catch create-db failure and remediate grants/disk\nif strings.Contains(err.Error(), \"failed to create database\") {\n    fixDbaGrants(primary)\n    return retryInitPrimary(ctx, req)\n}","preventionTips":["Provision tablets with correct db name (init_db_name_override)","Keep vt_dba privileges consistent across all mysql instances","Monitor disk usage on primary hosts","Include CREATE DATABASE check in tablet bootstrap automation"],"tags":["vtctld","mysql","primary","database-init"],"backgroundTag":"create-database-failed","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}