bat判断3306端口号是否被占用

本文介绍了一个使用Cmd命令查询特定端口(如3306)被哪个进程占用的方法,并提供了如何在发现端口被占用时显示占用进程名称的详细步骤。此外,还分享了查询结束后进行MySQL安装的流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

::#####get netstat by port#####
:getnetbyport
if not "%~1"=="" (
    set PORT=%~1
    for /f "tokens=1,* delims=," %%a in ("!PORT!") do (
        set myport=%%a
        set PORT=%%b
        ::PORT==8888
        for /f "delims=" %%z in ('netstat -a -n -o^|find /i ":!myport! "') do (
            set tLine=%%z
            set tLine=!tLine:%%=$!
            call :getNetInfo "!tLine!" tProto tLocalAdd tForeignAdd tState tPID
            set tLine=!tLine:$=%%!            
            ::echo !tLine!
            for /f "tokens=1 delims= " %%j in ('tasklist /nh /fi "PID eq !tPID!"') do (
				echo 3306端口已占用[%%j]
				goto :eof
            )
        )
    )
)

echo 安装mysql
call :installMysql



goto :eof


::#####FUNCTION#####
:getNetInfo
::将netstat -ano的某一行分隔成不同的变量
::call :getNetInfo "<netstat output line>" tProto tLocalAdd tForeignAdd tState tPID
if not "%~1"=="" (
    for /f "tokens=1,2,3,4,5 delims= " %%i in ("%~1") do (
        set %2=%%i
        set %3=%%j
        set %4=%%k
        if "%%i"=="TCP" (
            set %5=%%l
            set %6=%%m
        ) else (
            set %5=
            set %6=%%l
        )
    )
)
goto :eof

 

转载于:https://my.oschina.net/u/2450094/blog/778889

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值