param([switch] $Elevated) $ErrorActionPreference = 'Stop' if (-not $Elevated) { $arguments = @( '-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', ('"{0}"' -f $PSCommandPath), '-Elevated' ) $process = Start-Process -FilePath 'powershell.exe' -ArgumentList $arguments -Verb RunAs -Wait -PassThru exit $process.ExitCode } $xamppRoot = 'D:\Apps\Xammp' $myIni = Join-Path $xamppRoot 'mysql\bin\my.ini' $phpMyAdminConfig = Join-Path $xamppRoot 'phpMyAdmin\config.inc.php' $controlPanelConfig = Join-Path $xamppRoot 'xampp-control.ini' $mysqld = Join-Path $xamppRoot 'mysql\bin\mysqld.exe' $mysql = Join-Path $xamppRoot 'mysql\bin\mysql.exe' $logPath = Join-Path $PSScriptRoot '..\xampp-mariadb-setup.log' foreach ($path in @($myIni, $phpMyAdminConfig, $controlPanelConfig, $mysqld, $mysql)) { if (-not (Test-Path -LiteralPath $path)) { throw "Required XAMPP file not found: $path" } } if (-not (Test-Path -LiteralPath "$myIni.pre-port-3307")) { Copy-Item -LiteralPath $myIni -Destination "$myIni.pre-port-3307" } if (-not (Test-Path -LiteralPath "$phpMyAdminConfig.pre-port-3307")) { Copy-Item -LiteralPath $phpMyAdminConfig -Destination "$phpMyAdminConfig.pre-port-3307" } if (-not (Test-Path -LiteralPath "$controlPanelConfig.pre-port-3307")) { Copy-Item -LiteralPath $controlPanelConfig -Destination "$controlPanelConfig.pre-port-3307" } $myIniContents = [IO.File]::ReadAllText($myIni) $myIniContents = [Regex]::Replace($myIniContents, '(?m)^port\s*=\s*3306\s*$', 'port=3307') [IO.File]::WriteAllText($myIni, $myIniContents, [Text.UTF8Encoding]::new($false)) $phpMyAdminContents = [IO.File]::ReadAllText($phpMyAdminConfig) if ($phpMyAdminContents -match "\['port'\]") { $phpMyAdminContents = [Regex]::Replace( $phpMyAdminContents, "(?m)^\$cfg\['Servers'\]\[\$i\]\['port'\].*$", "`$cfg['Servers'][`$i]['port'] = '3307';" ) } else { $hostLine = "`$cfg['Servers'][`$i]['host'] = '127.0.0.1';" $phpMyAdminContents = $phpMyAdminContents.Replace( $hostLine, "$hostLine`r`n`$cfg['Servers'][`$i]['port'] = '3307';" ) } [IO.File]::WriteAllText($phpMyAdminConfig, $phpMyAdminContents, [Text.UTF8Encoding]::new($false)) $controlPanelContents = [IO.File]::ReadAllText($controlPanelConfig) if ($controlPanelContents -match '(?m)^\[ServicePorts\]$') { if ($controlPanelContents -match '(?m)^MySQL=') { $controlPanelContents = [Regex]::Replace($controlPanelContents, '(?m)^MySQL=.*$', 'MySQL=3307') } else { $controlPanelContents = [Regex]::Replace($controlPanelContents, '(?m)^\[ServicePorts\]$', "[ServicePorts]`r`nMySQL=3307") } } else { $controlPanelContents = $controlPanelContents.TrimEnd() + "`r`n[ServicePorts]`r`nMySQL=3307`r`n" } [IO.File]::WriteAllText($controlPanelConfig, $controlPanelContents, [Text.UTF8Encoding]::new($false)) $listener = Get-NetTCPConnection -LocalPort 3307 -State Listen -ErrorAction SilentlyContinue if (-not $listener) { Start-Process -FilePath $mysqld -ArgumentList @('--defaults-file=D:/Apps/Xammp/mysql/bin/my.ini', '--standalone') -WindowStyle Hidden } $deadline = [DateTime]::UtcNow.AddSeconds(30) do { Start-Sleep -Milliseconds 500 $listener = Get-NetTCPConnection -LocalPort 3307 -State Listen -ErrorAction SilentlyContinue } until ($listener -or [DateTime]::UtcNow -ge $deadline) if (-not $listener) { throw 'XAMPP MariaDB did not start on port 3307. Check mysql/data/mysql_error.log.' } & $mysql --protocol=TCP --host=127.0.0.1 --port=3307 --user=root --skip-column-names --execute='SELECT VERSION()' | Set-Content -LiteralPath $logPath if ($LASTEXITCODE -ne 0) { throw 'XAMPP MariaDB started but its local root connection failed.' } 'XAMPP MariaDB is listening on 127.0.0.1:3307.' | Add-Content -LiteralPath $logPath