阿里雲 1 核 2G 配置承載兩個企業官網,訪問量平平,卻幾乎天天出現「資料庫連線失敗」。經排查發現:MySQL 進程因記憶體耗盡被核心 OOM Killer 強制終止,導致 PID 文件遺失、服務無法自動重啟。
Sep 25 11:33:48 iZ28jcqqr7lZ kernel: Out of memory: Kill process 23201 (mysqld) score 53 or sacrifice child
Sep 25 11:33:48 iZ28jcqqr7lZ kernel: Killed process 23201, UID 500, (mysqld) total-vm:444168kB, anon-rss:102612kB, file-rss:120kB
遷移新環境後將表引擎改為 InnoDB,懷疑是 innodb_buffer_pool_size 設定過大(預設 128MB),在 2G 記憶體、無 Swap 的機器上與作業系統、其他進程搶佔記憶體,觸發 OOM。
mysql> USE db_name;
mysql> ALTER TABLE table_name ENGINE=MyISAM;
my.cnf 中合理設定 innodb_buffer_pool_size(建議 ≤ 物理記憶體 50-70%),並啟用 Swap 避免 OOM。vm.swappiness=1 與足夠 Swap 空間free -m 及 dmesg | grep -i oom 預警以上為實戰排查全記錄,希望能幫到遇到同類問題的運維同學。