MySQL 安裝後默認不自啟動,需根據系統初始化系統配置。現代 Linux(CentOS 7+/Ubuntu 16.04+)使用 Systemd,舊版用 SysVinit。
# 1. 確認服務單元名(通常為 mysqld 或 mysql)
systemctl list-unit-files | grep -i mysql
# 2. 設置開機自啟
systemctl enable mysqld
# 或
systemctl enable mysql
# 3. 立即啟動驗證
systemctl start mysqld
systemctl status mysqld
# 1. 複製啟動腳本(源碼安裝時)
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
# 2. 編輯腳本調整路徑
# basedir=/usr/local/mysql
# datadir=/data/mysql
# pid_file=/data/mysql/mysqld.pid
# 3. 加入啟動項
chkconfig --add mysqld
chkconfig --level 2345 mysqld on
# 4. 驗證
chkconfig --list mysqld
# 輸出應包含 2:on 3:on 4:on 5:on
# 容器內無 systemd,用重啟策略
docker run -d --restart=unless-stopped \
-e MYSQL_ROOT_PASSWORD=xxx \
-v /host/data:/var/lib/mysql \
mysql:8.0
無論哪種模式,確保 /etc/my.cnf 或 /etc/mysql/my.cnf 中 datadir、pid-file、socket、log-error 路徑與實際部署一致,否則啟動失敗。