針對高併發場景的 9 大核心參數調優實戰,含預設值、建議值、調整邏輯、監控指標與配置模板,與文章 48713 互補驗證。
| 參數 | 預設 | 建議 | 調整邏輯 | 監控 |
|---|---|---|---|---|
back_log | 50 | 500 | TCP 監聽隊列,SHOW PROCESSLIST 見大量 unauthenticated user 時調大 | Aborted_connects |
interactive_timeout | 28800 | 7200 | 互動連線空閒斷開,釋放殭屍連線 | Threads_connected |
key_buffer_size | 8M | 400M (2GB RAM) | MyISAM 索引緩衝,設物理記憶體 20~25%,過大致 Swap | Key_reads/Key_read_requests |
max_connections | 100 | 1024 | 最大並發,配合 open_files_limit、ulimit -n | Max_used_connections |
read_buffer_size | 128K | 16M | 全表掃描緩衝,大報表查詢受益 | Select_scan |
sort_buffer_size | 2M | 16M | 排序緩衝,每連線獨佔,慎防 OOM | Sort_merge_passes |
table_open_cache | 64 | 512 | 表快取,每表 2 fd,配合 open_files_limit | Opened_tables |
thread_cache_size | 0 | 80 | 執行緒快取,Threads_created/Connections 比值降低 | Threads_created |
wait_timeout | 28800 | 7200 | 非互動連線空閒斷開,與 interactive_timeout 同設 | Aborted_clients |
my.cnf 範本[mysqld]
back_log = 500
interactive_timeout = 7200
key_buffer_size = 400M
max_connections = 1024
read_buffer_size = 16M
sort_buffer_size = 16M
table_open_cache = 512
thread_cache_size = 80
wait_timeout = 7200
# InnoDB 專屬 (補充)
innodb_buffer_pool_size = 1G # 專用 DB 設 60~70% RAM
innodb_log_file_size = 256M
innodb_flush_log_at_trx_commit = 2
innodb_file_per_table = 1
systemctl restart mysqld。mysqladmin variables | grep -E 'back_log|key_buffer|max_connections|sort_buffer|table_open_cache|thread_cache|wait_timeout' 確認生效。sysbench --test=oltp --oltp-table-size=1000000 --mysql-db=test --mysql-user=root --mysql-password=xxx --num-threads=100 --max-time=300 run。參數調優無銀彈,唯有「監控→假設→調整→壓測→驗證」閉環才能持續收益。