Repair incomplete MySQL server setup
This commit is contained in:
@@ -260,15 +260,20 @@ 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 已使用默认密码,继续校验弱密码策略...")
|
||||
for candidate_password, message in (
|
||||
(final_password, "检测到 root 已使用默认密码,继续校验弱密码策略..."),
|
||||
("", "检测到新初始化的无密码 root 账号,立即设置默认密码..."),
|
||||
):
|
||||
configured_result = _run_mysql_sql(
|
||||
mysql_executable,
|
||||
"SHOW VARIABLES LIKE 'validate_password%';",
|
||||
candidate_password,
|
||||
)
|
||||
if configured_result.returncode == 0:
|
||||
variables_result = configured_result
|
||||
active_password = candidate_password
|
||||
on_output(message)
|
||||
break
|
||||
break
|
||||
if attempt + 1 < total_attempts:
|
||||
if attempt == 0 or (attempt + 1) % 5 == 0:
|
||||
@@ -558,10 +563,15 @@ def ensure_mysql_service_running(
|
||||
else:
|
||||
service_log = default_mysql_service_log_path()
|
||||
on_output(f"MySQL 服务启动日志: {service_log}")
|
||||
server_executable = (
|
||||
Path(mysql_executable).with_name("mysqld.exe")
|
||||
if mysql_executable is not None
|
||||
else None
|
||||
)
|
||||
exit_code = run_mysql_service_start_diagnostic(
|
||||
service_name,
|
||||
service_log,
|
||||
mysql_executable=mysql_executable,
|
||||
mysql_server_executable=server_executable,
|
||||
)
|
||||
if exit_code not in {0, 1056}:
|
||||
on_output(
|
||||
@@ -589,10 +599,10 @@ def ensure_mysql_service_running(
|
||||
|
||||
def build_mysql_service_start_script(
|
||||
service_name: str = "MySQL80",
|
||||
mysql_executable: Path | None = None,
|
||||
mysql_server_executable: Path | None = None,
|
||||
) -> str:
|
||||
escaped_service_name = service_name.replace("'", "''")
|
||||
escaped_mysql_executable = str(mysql_executable or "").replace("'", "''")
|
||||
escaped_mysql_executable = str(mysql_server_executable or "").replace("'", "''")
|
||||
return (
|
||||
"$ErrorActionPreference='Continue';"
|
||||
f"$name='{escaped_service_name}';"
|
||||
@@ -608,6 +618,14 @@ def build_mysql_service_start_script(
|
||||
"Write-Output ('my.ini: '+$ini+' exists='+[bool](Test-Path -LiteralPath $ini));"
|
||||
"$data=Join-Path $env:ProgramData 'MySQL\\MySQL Server 8.0\\Data';"
|
||||
"Write-Output ('Data directory: '+$data+' exists='+[bool](Test-Path -LiteralPath $data));"
|
||||
"if(Test-Path -LiteralPath $data){"
|
||||
"Write-Output 'Granting MySQL service account access to the data directory...';"
|
||||
"& icacls.exe $data /grant '*S-1-5-20:(OI)(CI)M' /T /C 2>&1 | Out-String | Write-Output;"
|
||||
"try { Start-Service -Name $name -ErrorAction Stop } catch { Write-Output ($_ | Out-String) };"
|
||||
"Start-Sleep -Seconds 8;"
|
||||
"$service=Get-CimInstance Win32_Service -Filter (\"Name='\"+$name+\"'\");"
|
||||
"Write-Output ('Service state after permission repair: '+$service.State)"
|
||||
"};"
|
||||
"$err=Get-ChildItem -LiteralPath $data -Filter '*.err' -File -ErrorAction SilentlyContinue | "
|
||||
"Sort-Object LastWriteTime -Descending | Select-Object -First 1;"
|
||||
"if($err){Write-Output ('Error log: '+$err.FullName); Get-Content -LiteralPath $err.FullName -Tail 30}"
|
||||
@@ -619,6 +637,18 @@ def build_mysql_service_start_script(
|
||||
"& $mysqlExe (\"--defaults-file=\"+$ini) --validate-config --console 2>&1 | Out-String | Write-Output;"
|
||||
"Write-Output ('validate-config exit code: '+$LASTEXITCODE)"
|
||||
"};"
|
||||
"$systemDatabase=Join-Path $data 'mysql';"
|
||||
"if($service.State -ne 'Running' -and $mysqlExe -and "
|
||||
"(Test-Path -LiteralPath $mysqlExe) -and -not (Test-Path -LiteralPath $systemDatabase)){"
|
||||
"Write-Output 'MySQL system database is missing; attempting safe initialization without deleting data...';"
|
||||
"& $mysqlExe (\"--defaults-file=\"+$ini) --initialize-insecure --console 2>&1 | Out-String | Write-Output;"
|
||||
"Write-Output ('initialize-insecure exit code: '+$LASTEXITCODE);"
|
||||
"& icacls.exe $data /grant '*S-1-5-20:(OI)(CI)M' /T /C 2>&1 | Out-String | Write-Output;"
|
||||
"try { Start-Service -Name $name -ErrorAction Stop } catch { Write-Output ($_ | Out-String) };"
|
||||
"Start-Sleep -Seconds 12;"
|
||||
"$service=Get-CimInstance Win32_Service -Filter (\"Name='\"+$name+\"'\");"
|
||||
"Write-Output ('Service state after initialization: '+$service.State)"
|
||||
"};"
|
||||
"$since=(Get-Date).AddMinutes(-10);"
|
||||
"Write-Output 'Recent Windows service events:';"
|
||||
"Get-WinEvent -FilterHashtable @{LogName='System';StartTime=$since} -ErrorAction SilentlyContinue | "
|
||||
@@ -636,12 +666,12 @@ def build_mysql_service_start_script(
|
||||
def run_mysql_service_start_diagnostic(
|
||||
service_name: str,
|
||||
log_path: Path,
|
||||
mysql_executable: Path | None = None,
|
||||
mysql_server_executable: Path | None = None,
|
||||
) -> int:
|
||||
with tempfile.TemporaryDirectory(prefix="autodeploy-mysql-service-") as temporary:
|
||||
script_path = Path(temporary) / "start-mysql-service.ps1"
|
||||
script_path.write_text(
|
||||
build_mysql_service_start_script(service_name, mysql_executable),
|
||||
build_mysql_service_start_script(service_name, mysql_server_executable),
|
||||
encoding="utf-8-sig",
|
||||
)
|
||||
return run_elevated_command_with_log(
|
||||
|
||||
Reference in New Issue
Block a user