萬盛學電腦網

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

killall mysqld 後無法啟動:Binlog/索引檔案損壞、端口衝突三大故障與修復

背景

暴力 killall mysqld 或 kill -9 導致 MySQL 非正常關閉,再啟動報錯。

故障一:Binlog 檔案遺失/損壞

[ERROR] File './mysql-bin.000041' not found (Errcode: 13)
[ERROR] Failed to open log file
[ERROR] Can't init tc log
[ERROR] Aborting

**修復**:刪除 mysql-bin.index (索引檔),重啟自動重建:

cd /var/lib/mysql
rm -f mysql-bin.index
systemctl start mysqld

原理:.index 記錄所有 Binlog 檔案列表,啟動時逐一打開。遺失實體檔案但索引仍引用 → 報錯。刪除索引讓 MySQL 重新掃描目錄建立新索引。

故障二:磁碟空間不足 (Errcode: 28)

[ERROR] Disk is full writing './mysql-bin.~rec~' (Errcode: 28)

**修復**:清理舊 Binlog 或擴容磁碟:

cd /var/lib/mysql
rm -f mysql-bin.000*   # 非主庫或已備份可刪
# 或
mysql -e "PURGE BINARY LOGS BEFORE '2024-01-10';"  # 安全清理

故障三:端口衝突 (Address already in use)

[ERROR] Can't start server: Bind on TCP/IP port: Address already in use
[ERROR] Do you already have another mysqld server running on port: 43306?

**排查**:

netstat -tlnp | grep 43306
ss -tlnp | grep 43306
lsof -i :43306

**修復**:

  • 殺掉佔用進程:kill -9
  • 或修改 my.cnf 換端口:port = 3307
  • 或配置 bind-address = 127.0.0.1 限制介面

故障四:InnoDB 崩潰恢復卡住

[Note] Recovering after a crash using mysql-bin
[ERROR] Error in Log_event::read_log_event(): 'read error'

**修復**:
1. 確認磁碟空間充足
2. innodb_force_recovery = 1 啟動讀取數據導出
3. 重建實例 (極端情況)

預防守則

  1. **嚴禁 kill -9 mysqld**,正常關閉:systemctl stop mysqld / mysqladmin shutdown
  2. 配置 expire_logs_days 防 Binlog 爆盤
  3. 監控磁碟使用率,閾值 80% 告警
  4. 重要數據庫啟用 innodb_force_recovery 測試恢復流程

**暴力殺進程是萬惡之源,優雅關閉才是運維基本功。**

copyright © 萬盛學電腦網 all rights reserved