Windows通过命令检查和删除进程
在 Windows 系统中,你可以使用以下命令来检测哪个程序占用了 3306 端口(MySQL 默认端口):
方法 1:使用 netstat
命令
netstat -ano | findstr "3306"
说明:
netstat -ano
列出所有端口及其对应的进程 ID(PID)。findstr "3306"
筛选出包含 3306 的行。
输出示例:
TCP 0.0.0.0:3306 0.0.0.0:0 LISTENING 1234
TCP [::]:3306 [::]:0 LISTENING 1234
其中 1234 是占用 3306 端口的进程 PID。
方法 2:使用 tasklist
查找具体进程
tasklist | findstr "1234"
(将 1234
替换为 netstat
查到的 PID)
输出示例:
mysqld.exe 1234 Services 0 10,256 K
可以看到是 mysqld.exe(MySQL 服务)占用了 3306 端口。
方法 3:使用 PowerShell 命令
Get-Process -Id (Get-NetTCPConnection -LocalPort 3306).OwningProcess
说明:
Get-NetTCPConnection -LocalPort 3306
查找 3306 端口的连接信息。Get-Process -Id
根据 PID 获取进程名称。
如何关闭占用 3306 端口的进程?
如果确认可以关闭该进程(如 MySQL 服务),可以运行:
taskkill /PID 1234 /F
(替换 1234
为实际的 PID)
或者通过服务管理关闭 MySQL:
net stop mysql
总结
命令 | 用途 |
---|---|
netstat -ano | findstr "3306" |
查找占用 3306 端口的 PID |
tasklist | findstr "PID" |
根据 PID 查找进程名 |
taskkill /PID 1234 /F |
强制结束占用端口的进程 |
如果发现是 MySQL 占用了 3306 端口,可以:
- 修改 MySQL 的端口(修改
my.ini
配置文件)。 - 停止 MySQL 服务(
net stop mysql
)。
注:如果失败尝试使用管理员权限