81 lines
3.8 KiB
PowerShell
81 lines
3.8 KiB
PowerShell
# Step1: verify/fix local MySQL root + create inquiry_robot; smoke 127 and intranet.
|
|
$ErrorActionPreference = "Continue"
|
|
$mysql = "C:\yutongda\tools\mysql-8.0.39-winx64\bin\mysql.exe"
|
|
$ops = "E:\wwwroot\ops"
|
|
$sec = Get-Content "$ops\inquiry_robot_db_secrets.json" -Raw | ConvertFrom-Json
|
|
$mysqlPass = [string]$sec.mysql_password
|
|
$intranet = (Get-NetIPAddress -AddressFamily IPv4 | Where-Object { $_.IPAddress -like '10.*' } | Select-Object -First 1 -ExpandProperty IPAddress)
|
|
Write-Output ("intranet=" + $intranet)
|
|
|
|
function Mysql-Ok([string]$h, [string]$u, [string]$p, [string]$db) {
|
|
$a = New-Object System.Collections.Generic.List[string]
|
|
[void]$a.Add("-h$h"); [void]$a.Add("-P3306"); [void]$a.Add("-u$u"); [void]$a.Add("-p$p")
|
|
[void]$a.Add("--connect-timeout=8"); [void]$a.Add("--batch"); [void]$a.Add("-N")
|
|
if ($db) { [void]$a.Add($db) }
|
|
[void]$a.Add("-e"); [void]$a.Add("SELECT 1")
|
|
$out = & $mysql $a.ToArray() 2>&1
|
|
$text = ($out | ForEach-Object {"$_"}) -join "`n"
|
|
if ($text -match "ERROR") { return $false }
|
|
$lines = $out | ForEach-Object {"$_".Trim()} | Where-Object { $_ -ne "" -and $_ -notmatch "Warning" }
|
|
return ($lines -contains "1")
|
|
}
|
|
|
|
$rootPass = $null
|
|
$sources = @()
|
|
if (Test-Path "$ops\local_mysql_root_secret.json") {
|
|
$sources += ,[pscustomobject]@{n="json"; p=[string](Get-Content "$ops\local_mysql_root_secret.json" -Raw | ConvertFrom-Json).root_password}
|
|
}
|
|
if (Test-Path "$ops\.mysql_root_stash.tmp") {
|
|
$sources += ,[pscustomobject]@{n="stash"; p=(Get-Content "$ops\.mysql_root_stash.tmp" -Raw).Trim()}
|
|
}
|
|
|
|
foreach ($s in $sources) {
|
|
Write-Output ("try_root src=$($s.n) len=$($s.p.Length)")
|
|
if (Mysql-Ok "127.0.0.1" "root" $s.p $null) {
|
|
$rootPass = $s.p
|
|
Write-Output "ROOT_OK_127"
|
|
break
|
|
}
|
|
if (Mysql-Ok "localhost" "root" $s.p $null) {
|
|
$rootPass = $s.p
|
|
Write-Output "ROOT_OK_LOCALHOST"
|
|
break
|
|
}
|
|
}
|
|
|
|
if (-not $rootPass) {
|
|
Write-Output "ROOT_FAIL; check service and redo skip-grant"
|
|
Get-Service YTD-MySQL | Format-List Name,Status | Out-String | Write-Output
|
|
# show bind
|
|
Select-String -Path "C:\yutongda\tools\mysql-8.0.39-winx64\my.ini" -Pattern "bind-address|port=" | ForEach-Object { $_.Line }
|
|
exit 2
|
|
}
|
|
|
|
$sql = @"
|
|
CREATE DATABASE IF NOT EXISTS ``inquiry_robot`` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
|
CREATE USER IF NOT EXISTS 'inquiry_robot'@'%' IDENTIFIED BY '$mysqlPass';
|
|
CREATE USER IF NOT EXISTS 'inquiry_robot'@'localhost' IDENTIFIED BY '$mysqlPass';
|
|
CREATE USER IF NOT EXISTS 'inquiry_robot'@'127.0.0.1' IDENTIFIED BY '$mysqlPass';
|
|
CREATE USER IF NOT EXISTS 'inquiry_robot'@'$intranet' IDENTIFIED BY '$mysqlPass';
|
|
ALTER USER 'inquiry_robot'@'%' IDENTIFIED BY '$mysqlPass';
|
|
ALTER USER 'inquiry_robot'@'localhost' IDENTIFIED BY '$mysqlPass';
|
|
ALTER USER 'inquiry_robot'@'127.0.0.1' IDENTIFIED BY '$mysqlPass';
|
|
ALTER USER 'inquiry_robot'@'$intranet' IDENTIFIED BY '$mysqlPass';
|
|
GRANT ALL PRIVILEGES ON ``inquiry_robot``.* TO 'inquiry_robot'@'%';
|
|
GRANT ALL PRIVILEGES ON ``inquiry_robot``.* TO 'inquiry_robot'@'localhost';
|
|
GRANT ALL PRIVILEGES ON ``inquiry_robot``.* TO 'inquiry_robot'@'127.0.0.1';
|
|
GRANT ALL PRIVILEGES ON ``inquiry_robot``.* TO 'inquiry_robot'@'$intranet';
|
|
FLUSH PRIVILEGES;
|
|
SELECT 'mysql_ok' AS s;
|
|
"@
|
|
$sqlPath = "$ops\sql\_run_ir_mysql.sql"
|
|
Set-Content $sqlPath -Value $sql -Encoding ascii
|
|
$o = & $mysql -h127.0.0.1 -P3306 -uroot "-p$rootPass" -e "source $($sqlPath -replace '\\','/')" 2>&1 | Out-String
|
|
Write-Output (($o -split "`r?`n" | Where-Object { $_ -notmatch "password|IDENTIFIED" }) -join "`n")
|
|
Remove-Item $sqlPath -Force -EA SilentlyContinue
|
|
if ($o -notmatch "mysql_ok") { exit 3 }
|
|
|
|
Write-Output ("smoke127=" + (Mysql-Ok "127.0.0.1" "inquiry_robot" $mysqlPass "inquiry_robot"))
|
|
Write-Output ("smokeIntranet=" + (Mysql-Ok $intranet "inquiry_robot" $mysqlPass "inquiry_robot"))
|
|
Write-Output "MYSQL_STEP_DONE"
|