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

@@ -16,6 +16,7 @@ from pathlib import Path
from typing import BinaryIO, Callable
from .scanner import ScanResult
from .mysql_recovery import MYSQL_RECOVERY_HELPERS
JDK_PACKAGES = {
@@ -525,6 +526,15 @@ def _read_install_log_tail(log_path: Path, max_lines: int = 20) -> str:
return "\n".join(lines[-max_lines:])
def _read_mysql_recovery_summary(log_path: Path) -> str:
lines = _read_install_log_tail(log_path, max_lines=300).splitlines()
causes = [line for line in lines if any(marker in line.lower() for marker in (
"[error]", "not found", "recovery failed:", "not empty", "existing entries:",
"configuration backup:", "data directory:", "my.ini:",
))]
return "\n".join(causes[:14] or lines[-20:])
def query_mysql_service_state(service_name: str = "MySQL80") -> int | None:
if os.name != "nt":
return 4
@@ -573,6 +583,11 @@ def ensure_mysql_service_running(
service_log,
mysql_server_executable=server_executable,
)
if exit_code == 2:
raise RuntimeError(
f"MySQL 初始化或配置失败。详细日志: {service_log}\n"
f"{_read_mysql_recovery_summary(service_log)}"
)
if exit_code not in {0, 1056}:
on_output(
f"Windows 服务启动命令返回 {exit_code},服务可能仍在后台恢复,继续等待运行状态..."
@@ -589,7 +604,7 @@ def ensure_mysql_service_running(
)
time.sleep(retry_delay)
detail = mysql_service_failure_detail(mysql_executable, state)
diagnostic = _read_install_log_tail(default_mysql_service_log_path(), max_lines=24)
diagnostic = _read_mysql_recovery_summary(default_mysql_service_log_path())
diagnostic_text = f"\n启动诊断:\n{diagnostic}" if diagnostic else ""
raise RuntimeError(
f"Windows 服务 {service_name} 启动超时,启动命令退出码: {exit_code}。"
@@ -604,62 +619,78 @@ def build_mysql_service_start_script(
escaped_service_name = service_name.replace("'", "''")
escaped_mysql_executable = str(mysql_server_executable or "").replace("'", "''")
return (
"$ErrorActionPreference='Continue';"
f"$name='{escaped_service_name}';"
f"$mysqlExe='{escaped_mysql_executable}';"
"$startError='';"
"try { Start-Service -Name $name -ErrorAction Stop } "
"catch { $startError=($_ | Out-String); Write-Output $startError };"
"Start-Sleep -Seconds 3;"
"$service=Get-CimInstance Win32_Service -Filter (\"Name='\"+$name+\"'\");"
"$service | Select-Object Name,State,StartName,PathName,ExitCode,ProcessId | "
"Format-List | Out-String | Write-Output;"
"$ini=Join-Path $env:ProgramData 'MySQL\\MySQL Server 8.0\\my.ini';"
"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}"
"else{Write-Output 'No .err log found with administrator privileges.'};"
"if($mysqlExe -and (Test-Path -LiteralPath $mysqlExe)){"
"Write-Output ('mysqld.exe: '+$mysqlExe);"
"Write-Output 'mysqld version:'; & $mysqlExe --version 2>&1 | Out-String | Write-Output;"
"Write-Output 'mysqld configuration validation:';"
"& $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 | "
"Where-Object {$_.ProviderName -eq 'Service Control Manager' -and $_.Message -match $name} | "
"Select-Object -First 8 TimeCreated,Id,LevelDisplayName,Message | Format-List | Out-String | Write-Output;"
"Write-Output 'Recent MySQL/Application events:';"
"Get-WinEvent -FilterHashtable @{LogName='Application';StartTime=$since} -ErrorAction SilentlyContinue | "
"Where-Object {$_.ProviderName -match 'MySQL|Application Error' -or $_.Message -match 'mysqld|MySQL80'} | "
"Select-Object -First 8 TimeCreated,ProviderName,Id,LevelDisplayName,Message | "
"Format-List | Out-String | Write-Output;"
"if($service.State -eq 'Running'){exit 0}else{exit 1}"
MYSQL_RECOVERY_HELPERS
+ f"\n$name='{escaped_service_name}';\n"
+ f"$mysqlExe='{escaped_mysql_executable}';\n"
+ r'''
$ErrorActionPreference = 'Continue'
$data = ''
$recoveryFailed = $false
try {
$service = Get-CimInstance Win32_Service -Filter ("Name='"+$name+"'") -ErrorAction Stop
if (-not $service) { throw ('Windows service not found: ' + $name) }
$service | Select-Object Name,State,StartName,PathName,ExitCode,ProcessId |
Format-List | Out-String | Write-Output
if ($service.State -eq 'Running') { exit 0 }
$layout = Get-MySqlServiceLayout $service
$mysqlExe = $layout.Server
$ini = $layout.Ini
$data = $layout.Data
Write-Output ('mysqld.exe: ' + $mysqlExe)
Write-Output ('my.ini: ' + $ini)
Write-Output ('Data directory: ' + $data)
if ($service.State -eq 'Stopped' -and $service.ProcessId -eq 0) {
$initialized = Initialize-EmptyMySqlData $service $layout
# Use the account actually registered for this service.
$account = $service.StartName
if ($account -eq 'NT AUTHORITY\NetworkService') { $sid = 'S-1-5-20' }
elseif ($account -eq 'LocalSystem') { $sid = 'S-1-5-18' }
else {
$identity = New-Object Security.Principal.NTAccount($account)
$sid = $identity.Translate([Security.Principal.SecurityIdentifier]).Value
}
$grantCode = Invoke-MySqlRecoveryCommand 'icacls.exe' @(
$data, '/grant', ('*' + $sid + ':(OI)(CI)M'), '/T', '/C'
)
if ($grantCode -ne 0) { throw ('Data directory permission repair failed: ' + $grantCode) }
Write-Output 'mysqld configuration validation:'
$validation = Invoke-MySqlRecoveryCommand $mysqlExe @(
('--defaults-file=' + $ini), '--validate-config', '--console'
)
if ($validation -ne 0) { throw ('MySQL configuration validation failed: ' + $validation) }
try { Start-Service -Name $name -ErrorAction Stop }
catch { Write-Output ('Start-Service: ' + $_.Exception.Message) }
}
for ($attempt = 0; $attempt -lt 12; $attempt++) {
$service = Get-CimInstance Win32_Service -Filter ("Name='"+$name+"'")
if ($service.State -eq 'Running') {
Write-Output 'MySQL service is running.'
exit 0
}
if ($service.State -eq 'Stopped') { break }
Start-Sleep -Seconds 1
}
Write-Output ('Service state after recovery: ' + $service.State)
} catch {
$recoveryFailed = $true
Write-Output ('MySQL recovery failed: ' + $_.Exception.Message)
}
if ($data -and (Test-Path -LiteralPath $data)) {
$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
} else { Write-Output 'No .err log found with administrator privileges.' }
}
$since = (Get-Date).AddMinutes(-10)
if ($recoveryFailed) { exit 2 }
Write-Output 'Recent Windows service events:'
Get-WinEvent -FilterHashtable @{LogName='System';StartTime=$since} -ErrorAction SilentlyContinue |
Where-Object {$_.ProviderName -eq 'Service Control Manager' -and $_.Message -match $name} |
Select-Object -First 3 TimeCreated,Id,Message | Format-List | Out-String | Write-Output
exit 1
'''
)

View File

@@ -0,0 +1,138 @@
"""PowerShell helpers for recovering a fresh Windows MySQL installation.
Embedded as Python text so the helpers ship inside the single-file executable.
Only an empty data directory is eligible for automatic initialization.
"""
MYSQL_RECOVERY_HELPERS = r'''
function Read-MySqlOptionFile([string]$Path) {
$bytes = [IO.File]::ReadAllBytes($Path)
$encoding = New-Object Text.UTF8Encoding($false, $true)
if ($bytes.Length -ge 2 -and $bytes[0] -eq 255 -and $bytes[1] -eq 254) {
$encoding = [Text.Encoding]::Unicode
} elseif ($bytes.Length -ge 2 -and $bytes[0] -eq 254 -and $bytes[1] -eq 255) {
$encoding = [Text.Encoding]::BigEndianUnicode
} elseif ($bytes.Length -ge 3 -and $bytes[0] -eq 239 -and $bytes[1] -eq 187 -and $bytes[2] -eq 191) {
$encoding = New-Object Text.UTF8Encoding($true, $true)
}
try { $text = $encoding.GetString($bytes).TrimStart([char]0xFEFF) }
catch {
$encoding = [Text.Encoding]::Default
$text = $encoding.GetString($bytes)
}
return @{ Text = $text; Encoding = $encoding }
}
function Get-MySqlSettings([string]$Text, [string]$ServiceName) {
if ($Text -match '(?m)^\s*!include') {
throw 'Automatic recovery does not support included option files. Check my.ini and its includes.'
}
$settings = @{}
$section = ''
foreach ($line in ($Text -split '\r?\n')) {
if ($line -match '^\s*\[([^]]+)\]') { $section = $Matches[1]; continue }
if ($section -notin @('mysqld', 'server', 'mysqld-8.0', $ServiceName)) { continue }
if ($line -match '^\s*([\w-]+)\s*=\s*(.*?)\s*$') {
$key = $Matches[1].Replace('_', '-').ToLowerInvariant()
$value = $Matches[2]
if ($value -match '^"([^"]*)"\s*(?:[#;].*)?$' -or $value -match "^'([^']*)'\s*(?:[#;].*)?$") {
$value = $Matches[1]
} else { $value = ($value -replace '\s+[#;].*$', '').Trim() }
$settings[$key] = $value
}
}
return $settings
}
function Get-MySqlServiceLayout($Service) {
$command = $Service.PathName
if ($command -notmatch '^\s*"([^"]+\\mysqld\.exe)"|^\s*(\S+\\mysqld\.exe)') {
throw 'Cannot resolve mysqld.exe from the Windows service command.'
}
$serverExe = if ($Matches[1]) { $Matches[1] } else { $Matches[2] }
if ($command -notmatch '(?i)--defaults-file(?:=|\s+)(?:"([^"]+)"|(\S+))') {
throw 'Cannot resolve --defaults-file from the Windows service command.'
}
$iniPath = if ($Matches[1]) { $Matches[1] } else { $Matches[2] }
if (-not [IO.Path]::IsPathRooted($iniPath) -or -not (Test-Path -LiteralPath $iniPath -PathType Leaf)) {
throw ('MySQL option file is missing or not absolute: ' + $iniPath)
}
$document = Read-MySqlOptionFile $iniPath
$settings = Get-MySqlSettings $document.Text $Service.Name
$dataPath = $settings['datadir']
if (-not $dataPath -or -not [IO.Path]::IsPathRooted($dataPath)) {
throw 'my.ini must contain an absolute datadir for automatic recovery.'
}
return @{ Server = $serverExe; Ini = $iniPath; Data = [IO.Path]::GetFullPath($dataPath) }
}
function Repair-MySqlLogNames([string]$Text, [string]$ServiceName) {
$names = @{
'log-bin' = 'mysql-bin'; 'log-bin-index' = 'mysql-bin.index'
'log-error' = 'mysql-error.err'; 'pid-file' = 'mysql.pid'
'general-log-file' = 'mysql-general.log'; 'slow-query-log-file' = 'mysql-slow.log'
'relay-log' = 'mysql-relay-bin'; 'relay-log-index' = 'mysql-relay-bin.index'
}
$section = ''
$newline = if ($Text.Contains("`r`n")) { "`r`n" } else { "`n" }
$lines = foreach ($line in ($Text -split '\r?\n')) {
if ($line -match '^\s*\[([^]]+)\]') { $section = $Matches[1] }
if ($section -in @('mysqld', 'server', 'mysqld-8.0', $ServiceName) -and
$line -match '^\s*([\w-]+)(?:\s*=.*|\s*)$') {
$key = $Matches[1].Replace('_', '-').ToLowerInvariant()
if ($names.ContainsKey($key)) { $key + '=' + $names[$key]; continue }
}
$line
}
# Explicit names avoid host-name-derived paths, including on Chinese Windows.
return (($lines -join $newline).TrimEnd([char[]]"`r`n") + $newline +
'[mysqld]' + $newline + 'log-bin=mysql-bin' + $newline +
'log-bin-index=mysql-bin.index' + $newline + 'log-error=mysql-error.err' + $newline)
}
function Invoke-MySqlRecoveryCommand([string]$Executable, [string[]]$Arguments) {
# Convert native stderr records to plain messages instead of PowerShell stack traces.
& $Executable @Arguments 2>&1 | ForEach-Object { Write-Host $_.ToString() }
return $LASTEXITCODE
}
function Initialize-EmptyMySqlData($Service, $Layout) {
if ($Service.State -ne 'Stopped' -or [int]$Service.ProcessId -ne 0) {
throw 'MySQL must be fully stopped before initialization.'
}
$dataPath = $Layout.Data
if (-not (Test-Path -LiteralPath $dataPath -PathType Container)) {
throw ('Data directory does not exist: ' + $dataPath)
}
if ((Get-Item -LiteralPath $dataPath -ErrorAction Stop).Attributes -band [IO.FileAttributes]::ReparsePoint) {
throw 'Automatic initialization is disabled for linked data directories.'
}
$entries = @(Get-ChildItem -LiteralPath $dataPath -Force -ErrorAction Stop)
if ($entries.Count -gt 0) {
Write-Host ('Data directory is not empty; initialization and log renaming skipped: ' + $dataPath)
Write-Host ('Existing entries: ' + (($entries | Select-Object -First 12 -ExpandProperty Name) -join ', '))
return $false
}
$document = Read-MySqlOptionFile $Layout.Ini
$settings = Get-MySqlSettings $document.Text $Service.Name
# Changing an empty default directory must never initialize a different configured directory.
if ([IO.Path]::GetFullPath($settings['datadir']) -ne $dataPath) { throw 'datadir changed during recovery.' }
$backup = $Layout.Ini + '.autodeploy-' + [Guid]::NewGuid().ToString('N') + '.bak'
[IO.File]::Copy($Layout.Ini, $backup, $false)
Write-Host ('Configuration backup: ' + $backup)
$updated = Repair-MySqlLogNames $document.Text $Service.Name
[IO.File]::WriteAllText($Layout.Ini, $updated, $document.Encoding)
Write-Host 'Empty data directory: using ASCII log names and initializing MySQL.'
# Initialization precedes service startup. Disable binlog for this invocation only.
$code = Invoke-MySqlRecoveryCommand $Layout.Server @(
('--defaults-file=' + $Layout.Ini), ('--datadir=' + $dataPath),
'--initialize-insecure', '--skip-log-bin', '--console'
)
if ($code -ne 0) { throw ('MySQL initialization failed, exit code: ' + $code + '. Existing files were retained.') }
if (-not (Test-Path -LiteralPath (Join-Path $dataPath 'mysql.ibd'))) {
throw 'Initialization returned success but mysql.ibd is missing. Refusing to start the service.'
}
Write-Host 'MySQL data directory initialized successfully.'
return $true
}
'''