You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CentOS系统EC2实例停止MySQL及解决Docker MySQL 3306端口占用

MySQL & Port Management Troubleshooting on CentOS EC2 & Docker

Hey fellow developer, let's walk through solutions to your two questions step by step:

1. Stopping MySQL Service on a CentOS EC2 Instance

CentOS uses different service management tools depending on the version, so here are the common methods to stop MySQL:

  • First, confirm the exact MySQL service name (it's usually mysqld or mysql) by checking its status:
    sudo systemctl status mysqld
    # Or for older CentOS versions using init.d:
    sudo service mysql status
    
  • Stop the service with the matching command:
    • For CentOS 7+ (using systemd):
      sudo systemctl stop mysqld
      
    • For older CentOS versions (using init.d):
      sudo service mysql stop
      
  • (Optional) If you want to prevent MySQL from starting automatically on system boot:
    # Systemd
    sudo systemctl disable mysqld
    # Init.d
    sudo chkconfig mysql off
    

2. Fixing "Bind for 0.0.0.0:3306 failed: port is already allocated" Error

This error means another process is hogging port 3306. Here's how to clear it out:

  • First, find all processes using port 3306:
    Option 1 (using lsof for detailed process info):
    sudo lsof -i :3306
    
    Option 2 (using netstat if lsof isn't installed):
    sudo netstat -tulpn | grep :3306
    
    Both commands will show you the PID (Process ID) and name of the process using the port.
  • Stop all processes using port 3306:
    To kill a single process, replace <PID> with the number you found:
    sudo kill -9 <PID>
    
    To kill all processes using 3306 in one go:
    sudo kill -9 $(sudo lsof -t -i :3306)
    
  • Verify the port is free by running the lsof or netstat command again—if there's no output, the port is ready for your Docker container.

内容的提问来源于stack exchange,提问作者Joe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 07:02:43