萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 資料庫 >> MySQL 教學

MySQL 啟動失敗「PID file could not be found」:進程殘留導致的假性故障排查

執行 service mysqld stop 報錯 MySQL manager or server PID file could not be found! [FAILED],實則 MySQL 進程仍在運行,PID 文件丟失或不匹配導致腳本誤判。本文提供標準排查與修復流程。

現象確認

[root@centos var]# service mysqld stop
MySQL manager or server PID file could not be found!   [FAILED]

根因:進程卡死、PID 文件不同步

檢查進程:

[root@irxpert-test /]# ps aux | grep mysq
root      10274  0.0  0.0  68160  1336 ?        S    13:43   0:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/irxpert-test.pid
mysql     10353  0.0  1.0 344360 39464 ?        Sl   13:43   0:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/lib/mysql/irxpert-test.err --pid-file=/var/lib/mysql/irxpert-test.pid
root      11884  0.0  0.0  63384   760 pts/1    S+   15:44   0:00 grep mysq*

兩個 MySQL 進程(mysqld_safe 守護進程 + mysqld 主進程)仍在運行,但 PID 文件 /var/lib/mysql/irxpert-test.pid 可能被誤刪或內容不匹配。

標準修復流程

  1. 強制終止殘留進程:
    kill 10274   # mysqld_safe
    kill 10353   # mysqld
    # 頑固進程用 kill -9
    
  2. 清理舊 PID 文件(可選):
    rm -f /var/lib/mysql/irxpert-test.pid
    
  3. 正常啟動:
    service mysqld start
    # 或 systemctl start mysqld (CentOS 7+)
    Starting MySQL.                                          [  OK  ]
    

深度預防:為什麼會發生?

  • 強制殺進程:kill -9 導致 mysqld 無法清理 PID 文件
  • 磁盤滿/權限變更:PID 文件寫入失敗
  • 多實例衝突:同機多實例 PID 文件路徑配置錯誤
  • 系統升級/重啟:systemd 管理下舊 SysV 腳本衝突

最佳實踐檢查清單

  1. 停止服務優先用 service mysqld stop / systemctl stop mysqld,避免直接 kill
  2. 配置 pid-file 顯式指定路徑,確保目錄可寫、權限正確(mysql:mysql)
  3. 監控腳本檢測「進程存在但 PID 文件缺失」自動修復
  4. 遷移至 systemd 管理(CentOS 7+),利用 Type=notify 內建 PID 追蹤

排查腳本化(可納入巡檢)

#!/bin/bash
PID_FILE="/var/lib/mysql/$(hostname).pid"
if pgrep mysqld >/dev/null && [ ! -f "$PID_FILE" ]; then
    echo "[$(date)] MySQL running but PID file missing, fixing..."
    pkill -9 mysqld
    pkill -9 mysqld_safe
    rm -f "$PID_FILE"
    systemctl start mysqld
fi

PID 文件異常是運維高頻小故障,建立標準化處理 SOP,能將故障恢復時間從分鐘級壓縮到秒級。

copyright © 萬盛學電腦網 all rights reserved