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
mysqldormysql) 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
- For CentOS 7+ (using systemd):
- (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 (usinglsoffor detailed process info):
Option 2 (usingsudo lsof -i :3306netstatiflsofisn't installed):
Both commands will show you the PID (Process ID) and name of the process using the port.sudo netstat -tulpn | grep :3306 - Stop all processes using port 3306:
To kill a single process, replace<PID>with the number you found:
To kill all processes using 3306 in one go:sudo kill -9 <PID>sudo kill -9 $(sudo lsof -t -i :3306) - Verify the port is free by running the
lsofornetstatcommand again—if there's no output, the port is ready for your Docker container.
内容的提问来源于stack exchange,提问作者Joe
相关产品推荐
相关产品推荐

