{"record":{"id":"7373ce8c801f0b14","repo":"vitessio/vitess","slug":"failed-to-initialize-mysqld-v","errorCode":null,"errorMessage":"failed to initialize mysqld: %v","messagePattern":"failed to initialize mysqld: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/vt/mysqlctl/mysqld.go","lineNumber":1342,"sourceCode":"\t// the root user.\n\tif err = mysqld.startNoWait(cnf); err != nil {\n\t\tlog.Error(fmt.Sprintf(\"failed starting mysqld: %v\\n%v\", err, readTailOfMysqldErrorLog(cnf.ErrorLogPath)))\n\t\treturn err\n\t}\n\n\t// Wait for mysqld to be ready, using root credentials, as no\n\t// user is created yet.\n\tparams := &mysql.ConnParams{\n\t\tUname:      \"root\",\n\t\tUnixSocket: cnf.SocketFile,\n\t}\n\tif err = mysqld.wait(ctx, cnf, params); err != nil {\n\t\tlog.Error(fmt.Sprintf(\"failed starting mysqld in time: %v\\n%v\", err, readTailOfMysqldErrorLog(cnf.ErrorLogPath)))\n\t\treturn err\n\t}\n\tif initDBSQLFile == \"\" { // default to built-in\n\t\tif err := mysqld.executeMysqlScript(ctx, params, config.DefaultInitDB); err != nil {\n\t\t\treturn fmt.Errorf(\"failed to initialize mysqld: %v\", err)\n\t\t}\n\t\t// Execute clone-specific init SQL if enabled\n\t\tif mysqlCloneEnabled {\n\t\t\tif err := mysqld.executeMysqlScript(ctx, params, config.InitClone); err != nil {\n\t\t\t\treturn fmt.Errorf(\"failed to initialize clone support: %v\", err)\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t}\n\n\t// else, user specified an init db file\n\tsqlFile, err := os.Open(initDBSQLFile)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"can't open init-db-sql-file (%v): %v\", initDBSQLFile, err)\n\t}\n\tdefer sqlFile.Close()\n\tscript, err := io.ReadAll(sqlFile)\n\tif err != nil {","sourceCodeStart":1324,"sourceCodeEnd":1360,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/vt/mysqlctl/mysqld.go#L1324-L1360","documentation":"After starting mysqld for the first time, Vitess runs its built-in init SQL script (config.DefaultInitDB) through executeMysqlScript to set up the initial database state. If that script fails to execute, the startup (Init/Start flow) fails with 'failed to initialize mysqld'. The underlying executeMysqlScript error (often an execCmd wrap) is embedded.","triggerScenarios":"Mysqld.Start/Init with no init_db_sql_file configured executes config.DefaultInitDB via executeMysqlScript; the mysql client invocation exits non-zero (server not fully up, SQL syntax error in the built-in script, connection refused).","commonSituations":"mysqld version differences making built-in init SQL invalid; socket file not ready despite wait() succeeding; MySQL error-log tail (printed before this error) showing startup problems; AppArmor/SELinux blocking the mysql client.","solutions":["Check the wrapped inner error for the mysql client's actual output (SQL error or connection failure).","Inspect the mysqld error log to confirm the server is fully started before init runs.","Supply a custom init_db_sql_file adapted to your MySQL flavor/version if the built-in script is incompatible.","Retry after fixing transient causes — a partially initialized data dir may need wiping (rm -rf <datadir>) before re-running Init."],"exampleFix":"// before: built-in script fails on MySQL 8\nmysqlctl init -root ... \n// after: provide a compatible init file\nmysqlctl init -init_db_sql_file /path/to/mysql8_init.sql ...","handlingStrategy":"retry","validationCode":"params, err := mysqld.GetMysqlConnectionParams()\nif err != nil { return err }\nconn, err := mysql.Connect(ctx, params) // confirm server is fully up before init SQL\nif err != nil { return fmt.Errorf(\"mysqld not ready: %w\", err) }\nconn.Close()","typeGuard":null,"tryCatchPattern":"if err := mysqld.Start(ctx, cnf, mysqldArgs, params); err != nil && strings.Contains(err.Error(), \"failed to initialize mysqld\") {\n    // check inner output; wipe half-initialized datadir; retry once\n    os.RemoveAll(cnf.DataDir)\n    err = mysqld.Start(ctx, cnf, mysqldArgs, params)\n}","preventionTips":["Tail the mysqld error log first — the cause is almost always printed there.","Match init SQL to the MySQL flavor/version in use.","Supply a custom -init_db_sql_file for non-default MySQL builds.","Automate datadir cleanup between init retries."],"tags":["mysql","init","startup","sql-script","mysqld"],"backgroundTag":"init-script-failed","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}