Files
pqs-9100_client/build/extraResources/mysql/kill-running-port.bat
2025-10-16 20:17:38 +08:00

116 lines
3.1 KiB
Batchfile
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

@echo off
chcp 65001 >nul
color 0C
title 清理NPQS9100 MySQL端口占用
cls
echo ==========================================
echo 清理 NPQS9100 MySQL 端口占用
echo ==========================================
echo.
REM 获取MySQL目录脚本所在目录
set MYSQL_HOME=%~dp0
cd /d "%MYSQL_HOME%"
REM 读取实际运行的端口
set PORT_FILE=.running-port
set MYSQL_PORT=
if exist "%PORT_FILE%" (
set /p MYSQL_PORT=<"%PORT_FILE%"
echo [√] 检测到运行记录:端口 %MYSQL_PORT%
echo.
) else (
echo [!] 未找到运行记录文件,使用默认端口 3306
echo.
set MYSQL_PORT=3306
)
echo [1] 检查端口 %MYSQL_PORT% 占用情况...
echo.
REM 查找占用该端口的连接
netstat -ano | findstr ":%MYSQL_PORT%" > "%TEMP%\mysql_port.txt"
if %errorlevel% equ 0 (
echo 发现以下端口 %MYSQL_PORT% 连接:
echo ----------------------------------------
type "%TEMP%\mysql_port.txt"
echo ----------------------------------------
echo.
echo [2] 提取进程ID并结束进程...
echo.
REM 提取所有LISTENING状态的PID
for /f "tokens=5" %%a in ('netstat -ano ^| findstr ":%MYSQL_PORT%" ^| findstr "LISTENING"') do (
if not "%%a"=="0" (
echo 正在结束进程 PID: %%a
taskkill /F /PID %%a 2>nul
if errorlevel 1 (
echo [失败] 无法结束进程 %%a
) else (
echo [成功] 已结束进程 %%a
)
)
)
REM 如果还有其他状态的连接,也尝试结束
for /f "tokens=5" %%a in ('netstat -ano ^| findstr ":%MYSQL_PORT%" ^| findstr /V "LISTENING"') do (
if not "%%a"=="0" (
echo 正在结束进程 PID: %%a
taskkill /F /PID %%a 2>nul
if errorlevel 1 (
echo [已结束或无权限] 进程 %%a
) else (
echo [成功] 已结束进程 %%a
)
)
)
echo.
echo [3] 清理完成
REM 等待2秒让进程完全释放
timeout /t 2 /nobreak >nul
echo.
echo [4] 再次检查端口 %MYSQL_PORT%...
netstat -ano | findstr ":%MYSQL_PORT%"
if errorlevel 1 (
echo [√] 端口 %MYSQL_PORT% 已完全释放
REM 删除端口记录文件
if exist "%PORT_FILE%" (
del "%PORT_FILE%"
echo [√] 已清理端口记录文件
)
) else (
echo [!] 仍有连接存在可能是TIME_WAIT状态会自动释放
)
) else (
echo [√] 没有发现占用端口 %MYSQL_PORT% 的进程
REM 删除端口记录文件
if exist "%PORT_FILE%" (
del "%PORT_FILE%"
echo [√] 已清理端口记录文件
)
)
REM 清理临时文件
if exist "%TEMP%\mysql_port.txt" del "%TEMP%\mysql_port.txt"
echo.
echo ==========================================
echo 清理完成
echo ==========================================
echo.
echo 说明:
echo - 此脚本只清理 NPQS9100 应用使用的 MySQL 端口
echo - 不会影响您电脑上的其他 MySQL 服务
echo.
pause