Hide MySQL password setup console windows

This commit is contained in:
王鹏
2026-10-09 10:31:41 +08:00
parent 46ad9270c7
commit ef5869985a
2 changed files with 33 additions and 2 deletions

View File

@@ -244,7 +244,8 @@ def configure_mysql_weak_password_policy(
) -> None:
on_output("正在连接 MySQL,并永久启用 6 位弱密码策略...")
variables_result: subprocess.CompletedProcess[str] | None = None
for attempt in range(max(attempts, 1)):
total_attempts = max(attempts, 1)
for attempt in range(total_attempts):
variables_result = _run_mysql_sql(
mysql_executable,
"SHOW VARIABLES LIKE 'validate_password%';",
@@ -252,7 +253,12 @@ def configure_mysql_weak_password_policy(
)
if variables_result.returncode == 0:
break
if attempt + 1 < max(attempts, 1):
error_text = f"{variables_result.stderr}\n{variables_result.stdout}".lower()
if "access denied" in error_text or "error 1045" in error_text:
break
if attempt + 1 < total_attempts:
if attempt == 0 or (attempt + 1) % 5 == 0:
on_output(f"MySQL 服务尚未就绪,正在后台重试({attempt + 1}/{total_attempts})...")
time.sleep(retry_delay)
assert variables_result is not None
@@ -493,6 +499,7 @@ def _read_install_log_tail(log_path: Path, max_lines: int = 20) -> str:
def _run_mysql_sql(mysql_executable: Path, sql: str, password: str) -> subprocess.CompletedProcess[str]:
environment = os.environ.copy()
environment["MYSQL_PWD"] = password
creation_flags = getattr(subprocess, "CREATE_NO_WINDOW", 0) if os.name == "nt" else 0
return subprocess.run(
[
str(mysql_executable),
@@ -508,6 +515,7 @@ def _run_mysql_sql(mysql_executable: Path, sql: str, password: str) -> subproces
timeout=15,
shell=False,
env=environment,
creationflags=creation_flags,
)

View File

@@ -1,6 +1,8 @@
import hashlib
import io
import json
import os
import subprocess
import tempfile
import unittest
import zipfile
@@ -350,9 +352,30 @@ class InstallerTests(unittest.TestCase):
self.assertIn("ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'", sql)
self.assertEqual(configure_call.kwargs["env"]["MYSQL_PWD"], MYSQL_INITIAL_ROOT_PASSWORD)
self.assertEqual(run.call_args_list[2].kwargs["env"]["MYSQL_PWD"], MYSQL_ROOT_PASSWORD)
expected_creation_flags = getattr(subprocess, "CREATE_NO_WINDOW", 0) if os.name == "nt" else 0
self.assertTrue(all(call.kwargs["creationflags"] == expected_creation_flags for call in run.call_args_list))
self.assertNotIn(MYSQL_ROOT_PASSWORD, configure_call.args[0])
self.assertTrue(any("永久允许 6 位弱密码" in message for message in messages))
def test_mysql_password_configuration_stops_retrying_on_access_denied(self):
denied = CompletedProcess(
[],
1,
stdout="",
stderr="ERROR 1045 (28000): Access denied for user 'root'@'localhost'",
)
with patch("deploy_tool.installer.subprocess.run", return_value=denied) as run:
with self.assertRaisesRegex(RuntimeError, "Access denied"):
configure_mysql_weak_password_policy(
Path("mysql.exe"),
lambda _message: None,
attempts=30,
retry_delay=0,
)
self.assertEqual(run.call_count, 1)
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="")