59 lines
2.5 KiB
PowerShell
59 lines
2.5 KiB
PowerShell
# Re-find local MySQL root: try socket vs TCP; stash all working variants.
|
|
$ErrorActionPreference = "Continue"
|
|
$mysql = "C:\yutongda\tools\mysql-8.0.39-winx64\bin\mysql.exe"
|
|
$dbCred = Get-Content "C:\yutongda\secure\application-database-credentials.json" -Raw | ConvertFrom-Json
|
|
$sec = Get-Content "E:\wwwroot\ops\inquiry_robot_db_secrets.json" -Raw | ConvertFrom-Json
|
|
$envText = Get-Content "E:\wwwroot\.env" -Raw
|
|
|
|
$cands = New-Object System.Collections.Generic.List[object]
|
|
function Add-Cand([string]$src, [string]$pass) {
|
|
if ([string]::IsNullOrWhiteSpace($pass)) { return }
|
|
$cands.Add([pscustomobject]@{ src = $src; pass = $pass; len = $pass.Length })
|
|
}
|
|
Add-Cand "app.mysql_password" ([string]$dbCred.mysql_password)
|
|
Add-Cand "app.postgres_password" ([string]$dbCred.postgres_password)
|
|
Add-Cand "secrets.mysql_password" ([string]$sec.mysql_password)
|
|
Add-Cand "secrets.pg_password" ([string]$sec.pg_password)
|
|
if ($envText -match '(?m)^MYSQL_PASSWORD=(.+)$') { Add-Cand "env.MYSQL_PASSWORD" $Matches[1].Trim() }
|
|
if ($envText -match '(?m)^PG_PASSWORD=(.+)$') { Add-Cand "env.PG_PASSWORD" $Matches[1].Trim() }
|
|
if (Test-Path "E:\wwwroot\ops\.mysql_root_stash.tmp") {
|
|
Add-Cand "stash" ((Get-Content "E:\wwwroot\ops\.mysql_root_stash.tmp" -Raw).Trim())
|
|
}
|
|
|
|
$modes = @(
|
|
@{ name = "socket_no_h"; args = @("-uroot") },
|
|
@{ name = "tcp_127"; args = @("-h127.0.0.1","-P3306","-uroot") },
|
|
@{ name = "tcp_localhost"; args = @("-hlocalhost","-P3306","-uroot") }
|
|
)
|
|
|
|
Write-Output ("cand=" + $cands.Count)
|
|
$hit = $null
|
|
foreach ($c in $cands) {
|
|
foreach ($m in $modes) {
|
|
$argList = @()
|
|
$argList += $m.args
|
|
$argList += "-p$($c.pass)"
|
|
$argList += "--connect-timeout=5"
|
|
$argList += "-e"
|
|
$argList += "SELECT 'root_ok' AS r"
|
|
$out = & $mysql @argList 2>&1 | Out-String
|
|
if ($out -match "root_ok") {
|
|
Write-Output ("HIT src=" + $c.src + " len=" + $c.len + " mode=" + $m.name)
|
|
$hit = [pscustomobject]@{ pass = $c.pass; mode = $m.name; src = $c.src }
|
|
break
|
|
}
|
|
}
|
|
if ($hit) { break }
|
|
}
|
|
if (-not $hit) {
|
|
Write-Output "NO_HIT"
|
|
# show first error sample without password
|
|
$sample = & $mysql -uroot "-p$($cands[0].pass)" -e "SELECT 1" 2>&1 | Out-String
|
|
($sample -split "`r?`n" | Where-Object { $_ -match "ERROR" } | Select-Object -First 2) | ForEach-Object { $_ }
|
|
exit 1
|
|
}
|
|
|
|
Set-Content -Path "E:\wwwroot\ops\.mysql_root_stash.tmp" -Value $hit.pass -Encoding ascii -NoNewline
|
|
Set-Content -Path "E:\wwwroot\ops\.mysql_root_mode.txt" -Value $hit.mode -Encoding ascii
|
|
Write-Output "STASHED"
|