Show MySQL service startup error log
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -26,6 +26,8 @@ from deploy_tool.installer import (
|
||||
default_mysql_install_log_path,
|
||||
default_mysql_server_install_log_path,
|
||||
ensure_mysql_service_running,
|
||||
find_mysql_error_log,
|
||||
mysql_service_failure_detail,
|
||||
query_mysql_service_state,
|
||||
run_elevated_command_with_log,
|
||||
TOMCAT_VERSION,
|
||||
@@ -422,7 +424,7 @@ class InstallerTests(unittest.TestCase):
|
||||
executable = install_mysql(messages.append)
|
||||
|
||||
self.assertEqual(executable, mysql)
|
||||
ensure_service.assert_called_once_with(messages.append)
|
||||
ensure_service.assert_called_once_with(messages.append, mysql_executable=mysql)
|
||||
configure.assert_called_once_with(mysql, messages.append)
|
||||
find_console.assert_not_called()
|
||||
self.assertTrue(any("跳过 Server 安装" in message for message in messages))
|
||||
@@ -457,6 +459,30 @@ class InstallerTests(unittest.TestCase):
|
||||
self.assertEqual(commands, [["sc.exe", "start", "MySQL80"]])
|
||||
self.assertTrue(any("已启动" in message for message in messages))
|
||||
|
||||
def test_reports_mysql_error_log_when_service_stops_after_start(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
program_data = Path(tmp)
|
||||
data_dir = program_data / "MySQL" / "MySQL Server 8.0" / "Data"
|
||||
data_dir.mkdir(parents=True)
|
||||
error_log = data_dir / "host.err"
|
||||
error_log.write_text("[ERROR] Data Dictionary initialization failed\n", encoding="utf-8")
|
||||
|
||||
with (
|
||||
patch.dict("os.environ", {"PROGRAMDATA": str(program_data)}),
|
||||
patch("deploy_tool.installer.query_mysql_service_state", side_effect=[1, 2, 1, 1]),
|
||||
patch("deploy_tool.installer.time.sleep"),
|
||||
):
|
||||
with self.assertRaisesRegex(RuntimeError, "Data Dictionary initialization failed"):
|
||||
ensure_mysql_service_running(
|
||||
lambda _message: None,
|
||||
mysql_executable=Path("mysql.exe"),
|
||||
elevated_runner=lambda _command: 0,
|
||||
)
|
||||
self.assertEqual(find_mysql_error_log(Path("mysql.exe")), error_log)
|
||||
detail = mysql_service_failure_detail(Path("mysql.exe"), 1)
|
||||
self.assertIn("STOPPED", detail)
|
||||
self.assertIn(str(error_log), detail)
|
||||
|
||||
def test_mysql_without_validation_component_still_sets_and_verifies_password(self):
|
||||
no_component = CompletedProcess([], 0, stdout="", stderr="")
|
||||
success = CompletedProcess([], 0, stdout="1\n", stderr="")
|
||||
|
||||
Reference in New Issue
Block a user