#!/bin/sh
# /etc/rc.d/init.d/mysqld-auto
# chkconfig: 345 99 01
# description: Auto start MySQL on boot
MYSQLD_SAFE=/opt/mysql/bin/mysqld_safe
DATADIR=/opt/mysql/data
USER=mysql
case "$1" in
start)
$MYSQLD_SAFE --datadir=$DATADIR --user=$USER &
;;
stop)
mysqladmin -u root -p shutdown
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
chmod +x /etc/rc.d/init.d/mysqld-auto
chkconfig --add mysqld-auto
chkconfig mysqld-auto on
或手動建立連結:
ln -s /etc/rc.d/init.d/mysqld-auto /etc/rc.d/rc3.d/S99mysqld
systemctl enable mysqld
systemctl start mysqld
# /etc/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
After=network.target syslog.target
[Service]
Type=forking
User=mysql
Group=mysql
ExecStart=/opt/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf
ExecStop=/opt/mysql/bin/mysqladmin -u root -p shutdown
PIDFile=/opt/mysql/data/mysqld.pid
TimeoutSec=300
Restart=on-failure
RestartSec=5
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable mysqld
systemctl start mysqld
systemctl status mysqld
# 實例 1
systemctl enable mysqld@3306
# 實例 2
systemctl enable mysqld@3307
# 模板單元檔 /etc/systemd/system/mysqld@.service 使用 %i 區分 port/datadir
| 現象 | 原因 | 解決 |
|---|---|---|
| 啟動失敗 | datadir 權限、my.cnf 路徑錯誤 | 檢查 journalctl -u mysqld -xe |
| 停止超時 | InnoDB 刷盤慢 | 調大 TimeoutSec、檢查 innodb_max_dirty_pages_pct |
| 重複啟動 | 舊 SysV 腳本殘留 | chkconfig mysqld off / systemctl disable 舊服務 |