Recover empty MySQL data directories with stable log names

This commit is contained in:
王鹏
2026-10-09 11:16:54 +08:00
parent a2cf38f1a4
commit 822c518534
5 changed files with 530 additions and 58 deletions

View File

@@ -434,6 +434,35 @@ class InstallerTests(unittest.TestCase):
find_console.assert_not_called()
self.assertTrue(any("跳过 Server 安装" in message for message in messages))
def test_newly_initialized_mysql_sets_password_using_empty_root_password(self):
denied = CompletedProcess([], 1, stdout="", stderr="ERROR 1045: Access denied")
success = CompletedProcess([], 0, stdout="", stderr="")
with patch("deploy_tool.installer.subprocess.run", side_effect=[
denied, denied, success, success, success,
]) as run:
configure_mysql_weak_password_policy(Path("mysql.exe"), lambda _: None, attempts=1)
self.assertEqual(run.call_args_list[3].kwargs["env"]["MYSQL_PWD"], "")
self.assertIn("ALTER USER", run.call_args_list[3].kwargs["input"])
self.assertEqual(run.call_args_list[4].kwargs["env"]["MYSQL_PWD"], MYSQL_ROOT_PASSWORD)
def test_reports_initialization_cause_without_waiting_for_service_timeout(self):
with tempfile.TemporaryDirectory() as temporary:
log = Path(temporary) / "mysql-service-start.log"
log.write_text(
"[ERROR] binary log index not found\n" + "diagnostic detail\n" * 40
+ "MySQL recovery failed: MySQL initialization failed, exit code: 1\n",
encoding="utf-8",
)
with (
patch("deploy_tool.installer.query_mysql_service_state", return_value=1),
patch("deploy_tool.installer.default_mysql_service_log_path", return_value=log),
patch("deploy_tool.installer.run_mysql_service_start_diagnostic", return_value=2),
patch("deploy_tool.installer.time.sleep") as sleep,
):
with self.assertRaisesRegex(RuntimeError, "binary log index not found"):
ensure_mysql_service_running(lambda _: None)
sleep.assert_not_called()
def test_reads_mysql_windows_service_state_without_console_window(self):
running = CompletedProcess([], 0, stdout=b"STATE : 4 RUNNING\r\n", stderr=b"")
@@ -505,7 +534,7 @@ class InstallerTests(unittest.TestCase):
self.assertIn("--validate-config", script)
self.assertIn("initialize-insecure", script)
self.assertIn("S-1-5-20", script)
self.assertIn("Service state after permission repair", script)
self.assertIn("Service state after recovery", script)
self.assertIn("Get-WinEvent", script)
self.assertIn("mysqld.exe", script)