Show MySQL service startup error log

This commit is contained in:
王鹏
2026-10-09 10:41:10 +08:00
parent 2f98295cde
commit 37d266ad2a
2 changed files with 81 additions and 5 deletions

View File

@@ -331,7 +331,7 @@ def install_mysql(
existing_executable = find_mysql_executable()
if existing_executable is not None:
on_output(f"检测到已安装的 MySQL,跳过 Server 安装: {existing_executable}")
ensure_mysql_service_running(on_output)
ensure_mysql_service_running(on_output, mysql_executable=existing_executable)
configure_mysql_weak_password_policy(existing_executable, on_output)
return existing_executable
console = find_mysql_installer_console()
@@ -389,7 +389,7 @@ def install_mysql(
raise RuntimeError(f"MySQL Server 安装失败,退出码: {server_exit_code}。{suffix}")
on_output("MySQL Server 安装任务已完成,正在检测命令行工具...")
executable = wait_for_mysql_executable()
ensure_mysql_service_running(on_output)
ensure_mysql_service_running(on_output, mysql_executable=executable)
configure_mysql_weak_password_policy(executable, on_output)
return executable
@@ -536,6 +536,7 @@ def query_mysql_service_state(service_name: str = "MySQL80") -> int | None:
def ensure_mysql_service_running(
on_output: Callable[[str], None],
service_name: str = "MySQL80",
mysql_executable: Path | None = None,
elevated_runner: Callable[[list[str]], int] | None = None,
attempts: int = 60,
retry_delay: float = 1.0,
@@ -557,17 +558,66 @@ def ensure_mysql_service_running(
if exit_code not in {0, 1056}:
raise RuntimeError(f"无法启动 Windows 服务 {service_name},退出码: {exit_code}。")
for _attempt in range(max(attempts, 1)):
for attempt in range(max(attempts, 1)):
state = query_mysql_service_state(service_name)
if state == 4:
on_output(f"Windows 服务 {service_name} 已启动。")
return
if state == 1 and attempt >= 2:
detail = mysql_service_failure_detail(mysql_executable, state)
raise RuntimeError(f"Windows 服务 {service_name} 启动后已停止。{detail}")
time.sleep(retry_delay)
detail = mysql_service_failure_detail(mysql_executable, state)
raise RuntimeError(
f"Windows 服务 {service_name} 启动超时,请检查 MySQL 数据目录和错误日志。"
f"Windows 服务 {service_name} 启动超时。{detail}"
)
def find_mysql_error_log(mysql_executable: Path | None = None) -> Path | None:
data_roots: list[Path] = []
program_data = os.environ.get("PROGRAMDATA", "").strip()
if program_data:
data_roots.append(Path(program_data) / "MySQL" / "MySQL Server 8.0" / "Data")
if mysql_executable is not None:
data_roots.append(Path(mysql_executable).parent.parent / "data")
logs: list[Path] = []
for data_root in data_roots:
try:
logs.extend(path for path in data_root.glob("*.err") if path.is_file())
except OSError:
continue
if not logs:
return None
try:
return max(logs, key=lambda path: path.stat().st_mtime)
except OSError:
return logs[0]
def mysql_service_failure_detail(
mysql_executable: Path | None,
state: int | None,
) -> str:
state_names = {
1: "STOPPED",
2: "START_PENDING",
3: "STOP_PENDING",
4: "RUNNING",
5: "CONTINUE_PENDING",
6: "PAUSE_PENDING",
7: "PAUSED",
}
state_text = state_names.get(state, "UNKNOWN")
error_log = find_mysql_error_log(mysql_executable)
if error_log is None:
return f" 当前服务状态: {state_text}。未找到 MySQL .err 错误日志。"
detail = _read_install_log_tail(error_log, max_lines=16)
if not detail:
return f" 当前服务状态: {state_text}。错误日志: {error_log}"
return f" 当前服务状态: {state_text}。错误日志: {error_log}\n{detail}"
def _run_mysql_sql(mysql_executable: Path, sql: str, password: str) -> subprocess.CompletedProcess[str]:
environment = os.environ.copy()
environment["MYSQL_PWD"] = password