Resume MySQL setup without reinstalling server
This commit is contained in:
@@ -244,6 +244,7 @@ def configure_mysql_weak_password_policy(
|
||||
) -> None:
|
||||
on_output("正在连接 MySQL,并永久启用 6 位弱密码策略...")
|
||||
variables_result: subprocess.CompletedProcess[str] | None = None
|
||||
active_password = initial_password
|
||||
total_attempts = max(attempts, 1)
|
||||
for attempt in range(total_attempts):
|
||||
variables_result = _run_mysql_sql(
|
||||
@@ -255,6 +256,15 @@ def configure_mysql_weak_password_policy(
|
||||
break
|
||||
error_text = f"{variables_result.stderr}\n{variables_result.stdout}".lower()
|
||||
if "access denied" in error_text or "error 1045" in error_text:
|
||||
configured_result = _run_mysql_sql(
|
||||
mysql_executable,
|
||||
"SHOW VARIABLES LIKE 'validate_password%';",
|
||||
final_password,
|
||||
)
|
||||
if configured_result.returncode == 0:
|
||||
variables_result = configured_result
|
||||
active_password = final_password
|
||||
on_output("检测到 root 已使用默认密码,继续校验弱密码策略...")
|
||||
break
|
||||
if attempt + 1 < total_attempts:
|
||||
if attempt == 0 or (attempt + 1) % 5 == 0:
|
||||
@@ -296,7 +306,7 @@ def configure_mysql_weak_password_policy(
|
||||
configure_result = _run_mysql_sql(
|
||||
mysql_executable,
|
||||
";\n".join(statements) + ";\n",
|
||||
initial_password,
|
||||
active_password,
|
||||
)
|
||||
if configure_result.returncode != 0:
|
||||
detail = _process_error(configure_result)
|
||||
@@ -318,6 +328,11 @@ def install_mysql(
|
||||
open_url = opener or urllib.request.urlopen
|
||||
install_log = Path(log_path) if log_path is not None else default_mysql_install_log_path()
|
||||
server_install_log = install_log.with_name("mysql-server-install.log")
|
||||
existing_executable = find_mysql_executable()
|
||||
if existing_executable is not None:
|
||||
on_output(f"检测到已安装的 MySQL,跳过 Server 安装: {existing_executable}")
|
||||
configure_mysql_weak_password_policy(existing_executable, on_output)
|
||||
return existing_executable
|
||||
console = find_mysql_installer_console()
|
||||
if console is None:
|
||||
install_log.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
@@ -236,6 +236,7 @@ class InstallerTests(unittest.TestCase):
|
||||
install_log = Path(tmp) / "logs" / "mysql-install.log"
|
||||
with (
|
||||
patch("deploy_tool.installer.MYSQL_INSTALLER_SHA256", checksum),
|
||||
patch("deploy_tool.installer.find_mysql_executable", return_value=None),
|
||||
patch("deploy_tool.installer.find_mysql_installer_console", return_value=None),
|
||||
patch(
|
||||
"deploy_tool.installer.wait_for_mysql_installer_console",
|
||||
@@ -278,6 +279,7 @@ class InstallerTests(unittest.TestCase):
|
||||
"deploy_tool.installer.find_mysql_installer_console",
|
||||
return_value=Path("MySQLInstallerConsole.exe"),
|
||||
),
|
||||
patch("deploy_tool.installer.find_mysql_executable", return_value=None),
|
||||
patch("deploy_tool.installer.wait_for_mysql_executable", return_value=Path("mysql.exe")),
|
||||
patch("deploy_tool.installer.configure_mysql_weak_password_policy") as configure,
|
||||
):
|
||||
@@ -374,7 +376,49 @@ class InstallerTests(unittest.TestCase):
|
||||
retry_delay=0,
|
||||
)
|
||||
|
||||
self.assertEqual(run.call_count, 1)
|
||||
self.assertEqual(run.call_count, 2)
|
||||
|
||||
def test_mysql_password_configuration_accepts_already_configured_password(self):
|
||||
denied = CompletedProcess(
|
||||
[],
|
||||
1,
|
||||
stdout="",
|
||||
stderr="ERROR 1045 (28000): Access denied for user 'root'@'localhost'",
|
||||
)
|
||||
query_result = CompletedProcess([], 0, stdout="", stderr="")
|
||||
success = CompletedProcess([], 0, stdout="1\n", stderr="")
|
||||
|
||||
with patch(
|
||||
"deploy_tool.installer.subprocess.run",
|
||||
side_effect=[denied, query_result, success, success],
|
||||
) as run:
|
||||
messages = []
|
||||
configure_mysql_weak_password_policy(
|
||||
Path("mysql.exe"),
|
||||
messages.append,
|
||||
attempts=30,
|
||||
retry_delay=0,
|
||||
)
|
||||
|
||||
self.assertEqual(run.call_count, 4)
|
||||
self.assertEqual(run.call_args_list[1].kwargs["env"]["MYSQL_PWD"], MYSQL_ROOT_PASSWORD)
|
||||
self.assertEqual(run.call_args_list[2].kwargs["env"]["MYSQL_PWD"], MYSQL_ROOT_PASSWORD)
|
||||
self.assertTrue(any("已使用默认密码" in message for message in messages))
|
||||
|
||||
def test_existing_mysql_skips_server_reinstallation(self):
|
||||
mysql = Path(r"C:\Program Files\MySQL\MySQL Server 8.0\bin\mysql.exe")
|
||||
with (
|
||||
patch("deploy_tool.installer.find_mysql_executable", return_value=mysql),
|
||||
patch("deploy_tool.installer.configure_mysql_weak_password_policy") as configure,
|
||||
patch("deploy_tool.installer.find_mysql_installer_console") as find_console,
|
||||
):
|
||||
messages = []
|
||||
executable = install_mysql(messages.append)
|
||||
|
||||
self.assertEqual(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))
|
||||
|
||||
def test_mysql_without_validation_component_still_sets_and_verifies_password(self):
|
||||
no_component = CompletedProcess([], 0, stdout="", stderr="")
|
||||
|
||||
Reference in New Issue
Block a user