服務名稱取決於安裝包類型、Linux 發行版、MySQL 版本,無絕對標準。常見映射:
| 場景 | 服務名 | 命令示例 |
|---|---|---|
| 官方 RPM 包 (RHEL/CentOS 6/7, MySQL 5.6/5.7/8.0) | mysqld |
service mysqld start / systemctl start mysqld |
| 官方 DEB 包 (Ubuntu/Debian, MySQL 5.7/8.0) | mysql |
service mysql start / systemctl start mysql |
| MariaDB (大多數發行版) | mariadb 或 mysql |
systemctl start mariadb |
| Percona Server | mysql |
service mysql start |
| Docker 容器 | 無服務名 | docker start <container> |
| Windows | 服務顯示名 "MySQL" | net start MySQL80 / sc start MySQL80 |
# systemd 系統
systemctl list-unit-files | grep -i mysql
systemctl list-unit-files | grep -i mariadb
# SysVinit 系統
ls /etc/init.d/ | grep -i mysql
chkconfig --list | grep -i mysql
# 通用:查看進程名
ps -ef | grep mysqld
# 進程名通常是 mysqld,但服務腳本名可能不同
mysqld (daemon 進程名)。mysql (項目名)。.service 文件名即服務名,由打包者決定。SERVICE_NAME=$(systemctl list-unit-files | grep -E 'mysql|mariadb|mysqld' | head -1 | awk '{print $1}')
systemctl start "$SERVICE_NAME"
mysqladmin ping --silent && echo "Running" || echo "Stopped"
service mysql restart 可行而 service mysqld restart 失敗,說明你的系統 (可能是 Ubuntu/Debian/Percona) 註冊的服務名為 mysql。請以 systemctl list-unit-files 實際輸出為準,勿死記名稱。