Files
2026-12-02 15:06:48 +08:00

97 lines
4.1 KiB
PowerShell

$ErrorActionPreference = 'Stop'
$serviceName = 'MySQL80'
$mysql84Root = 'D:\develop\MySQL-upgrade\mysql-8.4.13.1-winx64'
$mysql84Server = Join-Path $mysql84Root 'bin\mysqld.exe'
$mysql84Admin = Join-Path $mysql84Root 'bin\mysqladmin.exe'
$mysql84Config = 'D:\develop\MySQL-upgrade\my84.ini'
$mysql84Data = 'D:\develop\MySQL-upgrade\data80'
$mysql84Logs = 'D:\develop\MySQL-upgrade\logs'
$mysql84Run = 'D:\develop\MySQL-upgrade\run'
$mysql84Uploads = 'D:\develop\MySQL-upgrade\uploads80'
$mysql57Server = 'D:\develop\MySQL-5.7.44\bin\mysqld.exe'
$mysql57Config = 'D:\develop\SQLData\my.ini'
$newBinaryPath = '"{0}" --defaults-file="{1}" {2}' -f $mysql84Server, $mysql84Config, $serviceName
$oldBinaryPath = '"{0}" --defaults-file="{1}" {2}' -f $mysql57Server, $mysql57Config, $serviceName
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = [Security.Principal.WindowsPrincipal]::new($identity)
if (-not $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
throw 'MYSQL84_SERVICE_PROMOTION_REQUIRES_ADMINISTRATOR'
}
foreach ($path in @($mysql84Server, $mysql84Admin, $mysql84Config, $mysql84Data)) {
if (-not (Test-Path -LiteralPath $path)) {
throw "MYSQL84_REQUIRED_PATH_NOT_FOUND: $path"
}
}
$service = Get-Service -Name $serviceName -ErrorAction Stop
if ($service.Status -ne 'Stopped') {
throw "MYSQL57_SERVICE_MUST_BE_STOPPED: $($service.Status)"
}
$manual84 = @(Get-CimInstance Win32_Process | Where-Object {
$_.Name -eq 'mysqld.exe' -and
$_.ExecutablePath -and
([IO.Path]::GetFullPath($_.ExecutablePath) -eq [IO.Path]::GetFullPath($mysql84Server)) -and
$_.CommandLine -like '*D:\develop\MySQL-upgrade\my84.ini*'
})
if ($manual84.Count -ne 2) {
throw "EXPECTED_RUNNING_MYSQL84_PROCESS_PAIR: found $($manual84.Count). Do not promote after a reboot or after MySQL 5.7 has accepted new writes."
}
$configuredFor84 = $false
try {
& $mysql84Admin --login-path=finance_migration --protocol=PIPE --socket=MySQL --get-server-public-key shutdown
if ($LASTEXITCODE -ne 0) {
throw "MYSQL84_GRACEFUL_SHUTDOWN_FAILED: $LASTEXITCODE"
}
for ($i = 0; $i -lt 30; $i++) {
if (-not (Get-NetTCPConnection -State Listen -LocalPort 3306 -ErrorAction SilentlyContinue)) {
break
}
Start-Sleep -Seconds 1
}
if (Get-NetTCPConnection -State Listen -LocalPort 3306 -ErrorAction SilentlyContinue) {
throw 'MYSQL84_PORT_3306_DID_NOT_CLOSE'
}
$networkServiceSid = '*S-1-5-20'
& icacls.exe $mysql84Root /grant "${networkServiceSid}:(OI)(CI)RX" | Out-Null
if ($LASTEXITCODE -ne 0) { throw 'MYSQL84_INSTALL_ACL_FAILED' }
foreach ($path in @($mysql84Data, $mysql84Logs, $mysql84Run, $mysql84Uploads)) {
& icacls.exe $path /grant "${networkServiceSid}:(OI)(CI)M" | Out-Null
if ($LASTEXITCODE -ne 0) { throw "MYSQL84_WRITABLE_ACL_FAILED: $path" }
}
& icacls.exe $mysql84Config /grant "${networkServiceSid}:R" | Out-Null
if ($LASTEXITCODE -ne 0) { throw 'MYSQL84_CONFIG_ACL_FAILED' }
& sc.exe config $serviceName binPath= $newBinaryPath DisplayName= 'MySQL 8.4 LTS' start= auto | Out-Null
if ($LASTEXITCODE -ne 0) { throw "MYSQL84_SERVICE_CONFIG_FAILED: $LASTEXITCODE" }
$configuredFor84 = $true
Start-Service -Name $serviceName
$service = Get-Service -Name $serviceName
$service.WaitForStatus('Running', [TimeSpan]::FromSeconds(60))
& $mysql84Admin --login-path=finance_migration --protocol=PIPE --socket=MySQL --get-server-public-key ping | Out-Null
if ($LASTEXITCODE -ne 0) { throw 'MYSQL84_SERVICE_HEALTH_CHECK_FAILED' }
Write-Output 'MYSQL84_SERVICE_PROMOTION_OK'
exit 0
} catch {
if ($configuredFor84) {
$current = Get-Service -Name $serviceName -ErrorAction SilentlyContinue
if ($current -and $current.Status -ne 'Stopped') {
Stop-Service -Name $serviceName -ErrorAction SilentlyContinue
}
& sc.exe config $serviceName binPath= $oldBinaryPath DisplayName= 'MySQL80' start= auto | Out-Null
}
Start-Service -Name $serviceName -ErrorAction SilentlyContinue
throw
}