table_cachetable_open_cache (別名兼容舊名)控制表緩存最大條目數,每個客戶端連接至少訪問一個表,故與 max_connections 強相關。
.frm, .MYD, .MYI 或 .ibd),加入緩存| 引擎 | 數據文件描述符 | 索引文件描述符 | 備註 |
|---|---|---|---|
| MyISAM | 每線程獨享 | 全線程共享 | 首次打開佔 2 FD,後續僅 1 FD |
| InnoDB (共享表空間) | 單一 ibdata1 | 共享 | FD 佔用極低 |
| InnoDB (獨立表空間) | 每表 .ibd | 共享 | FD 佔用隨表數線性增長 |
mysql> SHOW GLOBAL STATUS LIKE 'open%_tables'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Open_tables | 450 | | Opened_tables | 1200 | +---------------+-------+
Open_tables:當前緩存表數Opened_tables:歷史緩存未命中次數 (每次未命中 +1)table_open_cache 過小,調大生產環境試運行一段時間 (含峰值期),將 table_open_cache 設為 Opened_tables 峰值 × 1.2,確保極端負載下仍有餘量。
FLUSH TABLES; -- 或 FLUSH TABLES WITH READ LOCK; -- 備份前確保一致性
會關閉所有打開的表,釋放文件描述符,緩存歸零。慎用於高併發業務高峰期。
# 檢查系統級限制 ulimit -n # MySQL 啟動用戶需足夠 FD 配額,建議 /etc/security/limits.conf: mysql soft nofile 65535 mysql hard nofile 65535
table_open_cache 過大可能觸及系統 open_files_limit 限制,導致 Can't open file 錯誤。